类的使用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". */
/** * Variable Reset function * If a instance is used multiple times, it's needed. Trust me. */ private function clearvariables(){ $this- pointer = 0; $this- index = 0; $this- imagedata = array(); $this- imageinfo = array(); $this- handle = 0; $this- parsedfiles = array(); }
/** * Clear Frames function * For deleting the frames after encoding. */ private function clearframes(){ foreach($this- parsedfiles as $temp_frame){ unlink($temp_frame); } }
/** * Frame Writer * Writes the GIF frames into files. */ private function writeframes($prepend){ for($i=0;$i sizeof($this- imagedata);$i++){ file_put_contents($this- temp_dir."/frame_".$prepend."_".str_pad($i,2,"0",STR_PAD_LEFT).".gif",$this- imageinfo["gifheader"].$this- imagedata[$i]["graphicsextension"].$this- imagedata[$i]["imagedata"].chr(0x3b)); $this- parsedfiles[]=$this- temp_dir."/frame_".$prepend."_".str_pad($i,2,"0",STR_PAD_LEFT).".gif"; } }
/** * Color Palette Transfer Device * Transferring Global Color Table (GCT) from frames into Local Color Tables in animation. */ private function transfercolortable($src,&$dst){ //src is gif header,dst is image data block //if global color table exists,transfer it if((ord($src[10])&128)==128){ //Gif Header Global Color Table Length $ghctl = pow(2,$this- readbits(ord($src[10]),5,3)+1)*3; //cut global color table from gif header $ghgct = substr($src,13,$ghctl); //check image block color table length if((ord($dst[9])&128)==128){ //Image data contains color table. skip. }else{ //Image data needs a color table. //get last color table length so we can truncate the dummy color table $idctl = pow(2,$this- readbits(ord($dst[9]),5,3)+1)*3; //set color table flag and length $dst[9] = chr(ord($dst[9]) | (0x80 | (log($ghctl/3,2)-1))); //inject color table $dst = substr($dst,0,10).$ghgct.substr($dst,-1*strlen($dst)+10); } }else{ //global color table doesn't exist. skip. } }
/** * GIF Parser Functions. * Below functions are the main structure parser components. */ private function get_gif_header(){ $this- p_forward(10); if($this- readbits(($mybyte=$this- readbyte_int()),0,1)==1){ $this- p_forward(2); $this- p_forward(pow(2,$this- readbits($mybyte,5,3)+1)*3); }else{ $this- p_forward(2); }