Php-PHP source code for processing dynamic GIF images and GIF animations

Source: Internet
Author: User
Php implements processing of dynamic GIF images and GIF animations

1. [PHP] code
Class gifImages {public static $ fileName = null; public static $ desName = null; public static $ fileType = 'GIF'; public static function isExistFile ($ file) {if (is_file ($ file) {return self: $ fileName = & $ file;} print_r (error_get_last (); exit ;} /*** write text to the image * @ param $ fileNmae target file address * @ param $ text the text to be written * @ param $ desName the image address after Generation * @ param $ font * @ param $ size * @ param $ color the color to be written * @ param $ x coordinate x to be written * @ param $ y coordinate y to be written * /public function writeText ($ fileName, $ text, $ desName = '', $ font, $ size, $ weight = 100, $ color, $ x, $ y) {self: isFile ($ fileName ); $ desName = self: getDesName ($ desName); $ image = new Imagick (); $ draw = new ImagickDraw (); $ draw-> setTextEncoding ('utf-8'); $ draw-> setFont ($ font); // '/usr/share/fonts/bitstream-vera/VeraBd. ttf' $ draw-> setFontWeight ($ weight); $ draw-> setFillColor ($ color); $ draw-> setFontSize ($ size ); // 12 $ draw-> setGravity (1); $ draw-> setFillAlpha (1); $ image-> readImage ($ fileName); $ image-> resetIterator (); do {$ image-> annotateImage ($ draw, $ x, $ y, 0, $ text);} while ($ image-> nextImage ()); $ image-> setFormat (self: $ fileType); $ image-> writeImages ($ desName, true); $ draw-> clear (); $ image-> clear (); $ draw-> destroy (); $ image-> destroy (); return $ desName ;} /*** merge multiple images (one image can be written as a LOGO, or multiple images can be made into a dynamic GIF animation) * @ param $ fileNmae the original image can be multiple * @ param $ desName generated Image addresses * @ param $ delay if the GIF animation is generated, the time of each frame is 100 = 1 s. */public function mergeImage ($ fileName, $ desName, $ delay = 1) {}/*** change image size * @ param $ fileNmae original image address * @ param $ width * @ param $ height * @ param $ desName generated Image address */public function resizeImages ($ fileName, $ width, $ height, $ desName = '') {self: isFile ($ fileName); self: getDesName ($ desName); $ image = new Imagick (); $ image-> readImage ($ fileName); $ image-> resetIterator (); do {$ iw = $ image-> getImageWidth (); $ ih = $ image-> getImageHeight (); $ ratio = doubleval ($ iw)/doubleval ($ width); if ($ height * $ ratio <$ ih) {$ ratio = doubleval ($ ih)/doubleval ($ height) ;}$ arrSize = array (floor ($ iw/$ ratio ), floor ($ ih/$ ratio); $ image-> resizeImage ($ arrSize [0], $ arrSize [1], 1, 1 );} while ($ image-> nextImage (); $ image-> setFormat ("gif"); $ image-> writeImages (self ::$ desName, true ); $ image-> destroy (); return self: $ desName;} public static function getDesName ($ desName) {self: getType (self ::$ fileName ); if (empty ($ desName) {$ desName = md5 (time (). rand (0,100 )). '. '. self: $ fileType;} else {if (stripos (strtolower ($ desName ),'. ') = false) {$ desName. = ". ". self: $ fileType;} return self: $ desName = $ desName;} public static function getType ($ fileName) {$ size = getimagesize ($ fileName ); $ type = null; switch ($ size ['Mime ']) {case "image/gif": $ type = "gif"; break; case "image/jpeg ": $ type = "jpeg"; break; case "image/png": $ type = "png"; break; case "image/bmp": $ type = "bmp "; break; default: $ type = false;} unset ($ size); return self: $ fileType = $ type ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.