這篇文章主要介紹了如何使用php調整gif動畫圖片尺寸,大家可以參考使用
類的使用demo: 代碼如下: <?php require_once "roucheng.php"; $gr = new gifresizer; $gr->temp_dir = "keleyi"; $gr->resize("keleyi.gif","keleyi_resized.gif",500,500); ?> 類的源代碼,保存為roucheng.php文件: 代碼如下: <? /** * * Resizes Animated GIF Files * * ///IMPORTANT NOTE: The script needs a temporary directory where all the frames should be extracted. * Create a directory with a 777 permission level and write the path into $temp_dir variable below. * * Default directory is "frames". */ class gifresizer { public $temp_dir = "frames"; private $pointer = 0; private $index = 0; private $globaldata = array(); private $imagedata = array(); private $imageinfo = array(); private $handle = 0; private $orgvars = array(); private $encdata = array(); private $parsedfiles = array(); private $originalwidth = 0; private $originalheight = 0; private $wr,$hr; private $props = array(); private $decoding = false; /** * Public part of the class * * @orgfile - Original file path * @newfile - New filename with path * @width - Desired image width * @height - Desired image height */ function resize($orgfile,$newfile,$width,$height){ $this->decode($orgfile); $this->wr=$width/$this->originalwidth; $this->hr=$height/$this->originalheight; $this->resizeframes(); $this->encode($newfile,$width,$height); $this->clearframes(); } /** * GIF Decoder function. * Parses the GIF animation into single frames. */ private function decode($filename){ $this->decoding = true; $this->clearvariables(); $this->loadfile($filename); $this->get_gif_header(); $this->get_graphics_extension(0); $this->get_application_data(); $this->get_application_data(); $this->get_image_block(0); $this->get_graphics_extension(1); $this->get_comment_data(); $this->get_application_data(); $this->get_image_block(1); while(!$this->checkbyte(0x3b) && !$this->checkEOF()){ $this->get_comment_data(1); $this->get_graphics_extension(2); $this->get_image_block(2); } $this->writeframes(time()); $this->closefile(); $this->decoding = false; } /** * GIF Encoder function. * Combines the parsed GIF frames into one single animation. */ private function encode($new_filename,$newwidth,$newheight){ $mystring = ""; $this->pointer = 0; $this->imagedata = array(); $this->imageinfo = array(); $this->handle = 0; $this->index=0; $k=0; foreach($this->parsedfiles as $imagepart){ $this->loadfile($imagepart); $this->get_gif_header(); $this->get_application_data(); $this->get_comment_data(); $this->get_graphics_extension(0); $this->get_image_block(0); //get transparent color index and color if(isset($this->encdata[$this->index-1])) $gxdata = $this->encdata[$this->index-1]["graphicsextension"]; else $gxdata = null; $ghdata = $this->imageinfo["gifheader"]; $trcolor = ""; $hastransparency=($gxdata[3]&&1==1); if($hastransparency){ $trcx = ord($gxdata[6]); $trcolor = substr($ghdata,13+$trcx*3,3); } //global color table to image data; $this->transfercolortable($this->imageinfo["gifheader"],$this->imagedata[$this->index-1]["imagedata"]); $imageblock = &$this->imagedata[$this->index-1]["imagedata"]; //if transparency exists transfer transparency index if($hastransparency){ &n