這個類別可以對GIF格式的動態檔案進行調整。提取動態圖片幀檔案到一個臨時目錄。調整圖像的大小並提取和重建成動畫的GIF格式的新檔案。
@地瓜不讓傳送gif圖片 效果查看:http://www.codepearl.com/files/187.html
-
- //http://www.codepearl.com
- require_once "gifresizer.php";
- $gr = new gifresizer ;
- $gr->temp_dir = "codepearl";
- $gr->resize("codepearl.gif","codepearl_resized.gif",500,500);
- ?>
複製程式碼
- /**
- * http://www.codepearl.com
- * 調整動畫GIF 檔案的大小
- *
- * ///重要說明:該腳本需要一個臨時目錄,應在其中提取所有幀。
- * 建立一個權限等級為777的目錄,並將路徑寫入下面的$temp_dir變數中。
- *
- * 預設目錄是「frames」。
- */
-
- class gifresizer {
-
- public $temp_dir = "priframes" > private; pointer = 0 ;
- private $index = 0 ; 🎜> 預設$handle = 0;
- 預設$orgvars = array();
- 預設$encda ta = array();
- 預設$parsedfiles = array();
- 預設$originalwidth = 0;
- 私人$wr,$hr;
- 私人$props = array();
- 私人$decoding = false *
- * 類別的公共部分
- *
- * @orgfile - 原始檔案路徑
- * @newfile - 帶路徑的新檔案名稱
- * @width - 所需的圖片寬度
- * @height - 所需的影像高度
- *
- * GIF 解碼器功能。
- * 將 GIF 動畫解析為單幀。
- * /
- 函數調整大小($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 編碼器功能。
- * 將解析的 GIF 幀組合成一個動畫。
- */
- DecodeDecode($filename){
- $this->decoding = true;
- $this->clearvariables();
- $this->loadfile($filename);
- $this->get_gif_header(); $this->get_application_data();
- $this-> get_application_data();
- $this->get_image_block(0); 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; $newwidth , $newheight ) {
- $神秘 = " " ; $this -> 指標 = 0 ; 🎜 > $this -> imagedata = array ()
- $this->handle = 0;
- $this->index=0;
-
- $k=0 );
- $this->loadfile( $imagepart);
- $this->get_gif_header();
- $this->get_application_data(); > $this->get_graphics_extension(0);
- $this ->get_image_block(0); ta[ $this->index-1]))
- $gxdata = $this->encda ta[$this->index-1]["graphicsextension"];
- else
- $gxdata = null ;
- $ghdata=$this->imageinfo["gifheader"];
- $trcolor="";
- $hastransparency=($gxdata[3]&&1= =1);
- if( $hastransparency){
- $trcx = order($gxdata[6]);
-
- //全域顏色表到影像資料;
- $this->transfercolortable($this->imageinfo["gifheader"],$this->imagedata[$this->index -1][ “圖像資料”]);
-
- $imageblock = &$this->imagedata[$this->index-1]["imagedata"];
-
- //如果透明度存在則傳輸透明度索引
- if ($hastransparency){
- $haslocalcolortable = ((ord($imageblock[9])&128)==128);
- if($haslocalcolortable){
- //本地表存在。確定邊界並尋找它。
- $tablesize=(pow(2,(ord($imageblock[9])&7) 1)*3) 10;
- $this->orgvars[$this-> ;index-1]["transparent_color_index "] =
- ((strrpos(substr($this->imagedata[$this->index-1]["imagedata"],0,$tablesize),$trcolor )-10)/3);
- }else{
- //本地表不存在,查看全域表
- $tablesize=(pow(2,(ord($gxdata[10])&7) 1)*3) 10 ;
- $ this->orgvars[$this->index-1]["transparent_color_index"] =
- ((strrpos(substr($ghdata,0,$tablesize),$trcolor)-10) /3);
- }
- }
-
- // 將原始延遲時間、透明索引和處置值應用於圖形擴充
-
- if(!$this->imagedata[$this->; index-1 ]["graphicsextension"]) $this->imagedata[$this->index-1]["graphicsextension"] = chr(0x21).chr(0xf9).chr(0x04).chr(0x00 ).chr(0x00 ).chr(0x00).chr(0x00).chr(0x00);
-
- $imagedata = &$this->imagedata[$this->index-1][" GraphicsExtension"];
-
- $imagedata[3] = chr((ord($imagedata[3]) & 0xE3) | ($this->orgvars[$this->index-1][" dispose_method"] $imagedata[4] = chr(($this->orgvars[$this->index-1]["delay_time"] % 256));
- $imagedata[5] = chr(floor($ this->orgvars[$this->index-1]["delay_time"] / 256));
- if($hastransparency){
- $imagedata[6] = chr($this->orgvars[$ this->index-1]["transparent_color_index"]);
- }
- $imagedata[3] = chr(ord($imagedata [3])|$hastransparency);
-
- //應用計算出的左側和頂部偏移
- $imageblock[1] = chr(round(($this->orgvars[$this->index) -1]["offset_left"]*$this->wr ) % 256));
- $imageblock[2] = chr(floor(($this->orgvars[$this->index-1) ]["offset_left"]*$this->wr) / 256) );
- $imageblock[3] = chr(round(($this->orgvars[$this->index-1][ "offset_top"]*$this->hr) % 256));
- $imageblock[4] = chr(floor(($this->orgvars[$this->index-1]["offset_top) "]*$this->hr) / 256));
-
- if ($this->index==1){
- if(!isset($this->imageinfo["applicationdata"]) || !$this->imageinfo[" applicationdata"])
- $this- >imageinfo["applicationdata"]=chr(0x21).chr(0xff).chr(0x0b)."NETSCAPE2.0".chr(0x03).chr(0x01)。 chr(0x00).chr(0x00).chr(0x00);
- if(!isset($this->imageinfo["commentdata"]) || !$this->imageinfo["commentdata"])
- $this->imageinfo["commentdata"] = chr(0x21).chr(0xfe).chr(0x10)."PHPGIFRESIZER1.0".chr(0);
- $mystring .= $this ->orgvars [“gifheader”]。 $this->imageinfo["applicationdata"].$this->imageinfo["commentdata"];
- if(isset($this->orgvars["hasgx_type_0"]) && $this->orgvars ["hasgx_type_0" ]) $mystring .= $this->globaldata["graphicsextension_0"];
- if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars[" hasgx_type_1"]) $mystring . = $this->globaldata["graphicsextension"];
- }
-
- $mystring .= $imagedata 。 $imageblock;
- $k ;
- $this->closefile();
- }
-
- $mystring .= chr(0x3b);
-
- //對 gif 標題套用新的寬度和高度
- $mystring[6] = chr($newwidth % 256);
- $mystring[7] = chr(floor($newwidth / 256) ));
- $mystring[8] = chr($newheight % 256);
- $mystring[9] = chr(floor($newheight / 256));
- $mystring[11] = $ this->orgvars["background_color"];
- //if(file_exists($new_filename)){unlink($new_filename);}
- file_put_contents($new_filename,$mystring);
- }
-
- /**
- * 變數重設功能
- * 如果一個實例被多次使用,則需要它。相信我。
- */
- 私人函數clearvariables(){
- $this->pointer = 0;
- $this->index = 0;
- $this - >imagedata = array();
- $this->imageinfo = array();
- $this->handle = 0;
- $this->parsedfiles = array();
- }
-
- /**
- * 清除幀功能
- * 用於編碼後刪除幀。
- */
- 牛皮函數clearframes () {
- foreach($this->parsedfiles as $temp_frame){
- unlink($temp_frame);
- }
- }
-
- /**
- * 幀寫入器
- * 將 GIF 幀寫入檔案。
- */
- 私有函數writeframes($prepend){
- for($i=0;$iimagedata);$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。 ".str_pad($i,2,"0",STR_PAD_LEFT).".gif";
- }
- }
-
- /**
- * 調色盤傳輸裝置
- * 將全域色表(GCT)從影格傳送到動畫中的局部色表。
- */
- 私有函數transfercolortable( $src,&$dst){
- //src為gif頭,dst為圖像資料塊
- //如果全域色表存在,則傳輸
- if ((ord($src[10]) &128)==128){
- //Gif 標題全域顏色表長度
- $ghctl = pow(2,$this->readbits(ord($src[ 10]),5,3) 1)* 3;
- //從gif頭中剪下全域顏色表
- $ghgct = substr($src,13,$ghctl);
- //檢查圖片區塊顏色表長度
- if(( ord ($dst[9])&128)==128){
- //影像資料包含色表。最後一個顏色表長度,以便我們可以中斷虛擬顏色表
- $idctl = pow(2,$this ->readbits(ord($dst[9]),5,3) 1)*3;
- //設定顏色表標誌與長度
- $dst[9] = chr(ord($dst[9]) | (0x80 | (log($ghctl/3,2)-1)));
- //注入顏色表
- $dst = substr($dst,0,10).$ghgct. substr($dst,-1*strlen($dst) 10);
- }
- }else{
- //全域顏色表不存在。 >
- /**
- * GIF 解析器函數。
- * 以下函數是解析器的主要結構組件。
- */
- 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);
- }
-
- $this->imageinfo["gifheader"]=$ this->datapart(0,$this->指針);
- if($this->解碼){
- $this->orgvars["gifheader"]=$this->imageinfo["gifheader"] ;
- $this->originalwidth = ord($this->orgvars["gifheader"][ 7])*256 ord($this->orgvars["gifheader"][6]);
- $this ->originalheight = ord($this->orgvars["gifheader"][9]) *256 ord($this->orgvars["gifheader"][8]);
- $this->orgvars["background_color "]=$this->orgvars["gifheader"][11];
- }
-
- }
- //--------------------------------------------- ----------- ----------- ----------
- private function get_application_data(){
- $startdata = $this- >readbyte(2);
- if( $startdata==chr(0x21).chr (0xff)){
- $start = $this->pointer - 2;
- $this->p_forward($ this->readbyte_int());
- $this->read_data_stream( $this->readbyte_int());
- $this->imageinfo["applicationdata"] = $this->datapart($start,$ this->pointer-$start);
- } else{
- $this->p_rewind(2);
- }
- }
- //---------- -------------- --- ---------------------------------
- private function get_comment_data(){
- $startdata = $this ->readbyte(2);
- if($startdata==chr(0x21).chr(0xfe)){
- $start =chr(0x21).chr(0xfe)){
- $start = $this->指標- 2;
- $this- > ;read_data_stream($this->readbyte_int());
- $this->imageinfo["commentdata"] = $this->datapart($start, $this->pointer-$start);
- } else{
- $this->p_rewind(2);
- }
- }
- //--------- ---------- ---- ------------------------------------ --------------------
- 創傷函數 get_graphics_extension ($type){
- $startdata = $this->readbyte(2);
- if ($startdata==chr(0x21).chr(0xf9)){
- $start = $this->指標- 2;
- $this->p_forward($this->readbyte_int());
- $this->p_forward(1);
- if($type==2){
- $ this-> ;imagedata[$this->index]["graphicsextension"] = $this->datapart( $start,$this->pointer-$start);
- }else if($type==1) {
- $this->orgvars["hasgx_type_1"] = 1;
- $this-> globaldata["graphicsextension"] = $this->datapart($start,$this->pointer- $start);
- }else if($type==0 && $this->decoding==false){
- $this->encda[$this->index]["graphicsextension"] = $this- >datapart($start,$this->指標-$start);
- }else if ($type==0 && $this->decoding==true){
- $this->orgvars["hasgx_type_0 "] = 1;
- $this->globaldata["graphicsextension_0"] = $this- >datapart($start,$this->pointer-$start);
- }
- }else{
- $this->p_rewind(2);
- }
- }
- / /---------------- ------------ --------------------- -------------------------------
- 解剖函數get_image_block($type) {
- if($this- >checkbyte(0x2c)){
- $start = $this->pointer;
- $this->p_forward(9);
- if($this ->readbits (($mybyte=$this-> readbyte_int()),0,1)==1){
- $this->p_forward(pow(2,$this->readbits($mybyte,5,3) 1 )*3);
- }
- $this->p_forward(1);
- $this->read_data_stream($this->readbyte_int());
- $this->imagedata[$this ->index]["imagedata"] = $this->datapart($start,$this->pointer-$start);
-
- if ($type==0){
- $this- >orgvars["hasgx_type_0"] = 0 ;
- if(isset($this->globaldata["graphicsextension_0"]))
- $this ->imagedata[$this->index]["graphicsextension"]= $this->globaldata["graphicsextension_0" ];
- else
- $this->imagedata[$this->index ]["graphicsextension"]=null;
- unset($this->globaldata[" GraphicsExtension_0"]);
- } elseif($type==1){
- if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]==1 ){
- $this-> orgvars["hasgx_type_1"] = 0;
- $this-> ;imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension"] ;
- unset($this- >globaldata["graphicsextension"]); }else{
- $this->orgvars["hasgx_type_0"] = 0;
- $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata[" GraphicsExtension_0"];
- unset($this->globaldata["graphicsextension_0"]);
- }
- }
-
- $this->parse_image_data();
- $this ->index ;
-
- }
- }
- //---------------------------- -------- ------------------
- 原生函數parse_image_data(){
- $this->imagedata[$this->; 索引]["disposal_method"] = $ this->get_imagedata_bit("ext",3,3,3);
- $this->imagedata[$this->index]["user_input_flag"] = $this->get_imagedata_bit("ext",3,6 ,1);
- $this->imagedata[$this->index]["transparent_color_flag"] = $this->get_imagedata_bit("ext" ,3,7,1);
- $this->imagedata [$this->index]["delay_time"] = $this->dualbyteval($this->get_imagedata_byte("ext",4 ,2));
- $this->imagedata[$this->index] ["transparent_color_index"] = ord($this->get_imagedata_byte("ext",6,1));
- $this->imagedata[$this->index]["offset_left"] = $this->dualbyteval ($this->get_imagedata_byte("dat",1,2));
- $this->imagedata[$this->index]["offset_top"] = $this->dualbyteval($this->get_imagedata_byte( "dat",3,2));
- $this->imagedata [$this->index]["width"] = $this->dualbyteval($this->get_imagedata_byte("dat",5,2 ));
- $this->imagedata[$this ->index]["height"] = $this->dualbyteval($this->get_imagedata_byte("dat",7,2));
- $這->圖像數據[$這->; index]["local_color_table_flag"] = $this->get_imagedata_bit("dat",9,0,1);
- $this->imagedata[$this->index]["interlace_flag"] = $this-> get_imagedata_bit("dat",9,1,1);
- $this->imagedata[$this->index]["sort_flag"] = $this->get_imagedata_bit("dat" ,9,2,1) ;
- $this->imagedata[$this->index]["color_table_size"] = pow(2,$this->get_imagedata_bit("dat",9,5, 3) 1)*3;
- $this->imagedata[$this->index]["color_table"] = substr($this->imagedata[$this->index]["imagedata"],10,$this->imagedata[$this-> index]["color_table_size"]);
- $this->imagedata[$this->index]["lzw_code_size"] = ord( $this->get_imagedata_byte("dat",10,1));
- if($this->解碼){
- $this->orgvars[$this->index]["transparent_color_flag "] = $this->imagedata[$this->index]["transparent_color_flag"];
- $this->orgvars[$this->index]["transparent_color_index"] = $this-> ;imagedata[$this->index]["transparent_color_index"];
- $this->orgvars[$this ->index]["delay_time"] = $this->imagedata[$this-> ;index]["delay_time"];
- $this->orgvars[$this->index]["disposal_method"] = $this->imagedata[$this->index]["disposal_method" ];
- $this->orgvars[$this->index]["offset_left"] = $this->imagedata[$this->index ]["offset_left"];
- $this ->orgvars[$this->index]["offset_top"] = $this->imagedata[$this->index]["offset_top"];
- }
- }
- / /----------------------------------------- --------------- -------- ------
- private function get_imagedata_byte($type,$start,$length){
- if ($type=="ext")
- return substr( $this->imagedata[$this->index]["graphicsextension"],$start,$length);
- elseif($type==" dat")
- return substr($this->imagedata[ $this->;索引]["imagedata"],$start,$length);
- }
- //--------------------------------------------- ----------- ----------- ----------
- private function get_imagedata_bit($type,$byteindex,$bitstart,$bitlength ){
- if($type== "ext")
- return $this->readbits(ord(substr($this->imagedata[$this->index]["graphicsextension"],$byteindex, 1)),$bitstart,$bitlength);
- elseif($type= ="dat")
- return $this->readbits(ord(substr($this->imagedata[$this->index] ["imagedata"],$byteindex,1) ),$bitstart, $bitlength);
- }
- //--------------------- ----------- -------- ------------------
- 臍函數dualbyteval($s){
- $i = ord($s[1] )*256 ord($s[ 0]);
- return $i;
- }
- //----------- - 輔助函數-------- -------- -----
- 私有函數read_data_stream($first_length){
- $this->p_forward($first_length);
- $length=$this->readbyte_int() ;
- if( $length!=0) {
- while($length!=0){
- $this->p_forward($length);
- $length=$this->readbyte_int( );
- }
- }
- 回傳true;
- }
- //-------------- -------------------------- --------------- --------- -----------------
- 私有函數loadfile($filename ){
- $this->handle = fopen($檔名,"rb");
- $this->pointer = 0;
- }
- //-------- ------------------------ ------------------------
- private function closefile(){
- fclose($this->handle);
- $this ->handle=0;
- }
- //------------ -------------- --------- ----------------------
- 私有函數readbyte($byte_count){
- $data = fread( $this->handle,$byte_count) ;
- $this->pointer = $byte_count;
- return $data;
- }
- //--------------------- --- ------------------------------------ ----------- -----------
- 原生函數readbyte_int(){
- $data = fread($this->handle,1);
- $this->指標;
- return ord($data);
- }
- / /------------------------ ----------- -------------- ------
- 私有函式readbits($byte,$start,$length){
- $bin = str_pad(decbin($byte) ,8,"0",STR_PAD_LEFT);
- $ data = substr($bin,$start,$length);
- return bindec($data);
- }
- //--- -------------------------------------------------- --- -------------
- 蠟燭函數p_rewind($length){
- $this->pointer-=$length;
- fseek($this->handle ,$this ->指標);
- }
- //-------- ------------------------- --- -------------------------------
- 私有函數p_forward($length){
- $this ->指標=$length;
- fseek($this->句柄,$this->指標);
- }
- // ---------------- --- ------------------------------- -----
- private function datapart($start,$ length){
- fseek($this->handle,$start);
- $data = fread($this->handle, $length);
- fseek($this->句柄,$this- >指標) ;
- return $data;
- }
- //------------ ------------------ -------- -----------------------------------
- 蠟燭函數checkbyte( $byte){
- if (fgetc($this->handle)==chr($byte)){
- fseek($this->handle,$this->pointer);
- return true;
- }else{
- fseek($this->句柄,$this->指針);
- return false;
- }
- }
- // ------- -------- ----------------------------------- -----
- 陰道函數checkEOF(){
- if(fgetc($this->handle)===false){
- return true;
- }else{
- fseek ($this->句柄,$this->句柄,$this-> 句柄this->指標);
- return false;
- }
- }
- //------------------------- --- ------------------ ----------------------------- -----
- /**
- * 偵錯功能。
- * 將 GIF 動畫解析為單幀。
- * /
- 私有函式debug($string){
- echo "
- for($i=0;$i echo str_pad(dechex(ord($ string[$i])),2,"0",STR_PAD_LEFT)。 ";
- }
- //--------------------------------------------- ----------- ----------- ----------
- private function debuglen($var,$len){
- echo "
- for( $i=0;$i echo str_pad(dechex(ord($var[$i])),2,"0" ,STR_PAD_LEFT)。 " ";
- }
- echo "";
- }
- //----------------------- - ------------- ---------------------------------
- 原生函數debugstream($length){
- $this->debug($this->datapart($this->pointer,$length));
- }
- //------- - --------- ---------------------------------------- ------------- --------
- /**
- * GD Resizer Device
- * 調整動畫影格的大小
- */
- 私有函數resizeframes(){
- $k=0;
- foreach($this->parsedfiles as $img) {
- $src = imagecreatefromgif($img);
- $sw = $這->imagedata[$k]["width"];
- $sh = $this->imagedata[$k][ "高度"];
- $nw = round($sw * $this ->wr);
- $nh = round($sh * $this- >hr);
- $sprite = imagecreatetruecolor($西北,$nh);
- $trans = imagecolortransparent($sprite);
- imagealphablending($sprite, false);
- imagesavealpha($sprite, true);
- imagepalettecopy($sprite,$src);
- imagefill($sprite,0,0,imagecolortransparent($src));
- imagecolortransparent($sprite,imagecolortransparent($src));
- imagecopyresized($sprite,$src,0,0,0,0,$nw,$nh,$sw,$sh);
- imagegif($sprite,$img);
- imagedestroy($sprite);
- imagedestroy($src);
- $k ;
- }
- }
- }
- }
- }
- }
} } } } } | }
}