이 클래스는 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_resize.gif",
- ?>
코드 복사
-
- /**
- * http://www.codepearl.com
- * 애니메이션 GIF 파일 크기 조정
- *
- * ///중요 참고: 스크립트에는 모든 프레임을 추출해야 하는 임시 디렉터리가 필요합니다.
- * 권한 수준이 777인 디렉터리를 생성하고 아래 $temp_dir 변수에 경로를 씁니다.
- *
- * 기본 디렉터리는 "frames"입니다.
- */
-
- 클래스 gifresizer {
-
- public $temp_dir = "frames";
- private $pointer = 0;
- private $index = 0;
- 프라이빗 $globaldata = array();
- 프라이빗 $imagedata = array();
- 프라이빗 $imageinfo = array();
- 프라이빗 $handle = 0;
- 프라이빗 $orgvars = array();
- private $encdata = array();
- private $parsedfiles = array();
- private $originalwidth = 0;
- private $originalheight = 0;
- private $ wr,$hr;
- private $props = array();
- private $decoding = false;
-
- /**
- * 클래스의 공개 부분
- *
- * @orgfile - 원본 파일 경로
- * @newfile - 경로가 포함된 새 파일 이름
- * @width - 원하는 이미지 너비
- * @height - 원하는 이미지 높이
- */
- 함수 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 디코더 기능.
- * GIF 애니메이션을 단일 프레임으로 파싱합니다.
- */
- 개인 함수 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 인코더 기능.
- * 구문 분석된 GIF 프레임을 하나의 애니메이션으로 결합합니다.
- */
- 개인 함수 인코딩($ new_filename,$newwidth,$newheight){
- $mystring = "";
- $this->pointer = 0;
- $this->imagedata = array();
- $this- >imageinfo = array();
- $this->handle = 0;
- $this->index=0;
-
- $k=0;
- foreach($this ->파일을 $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);
-
- //투명 색상 인덱스 및 색상 가져오기
- 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);
- }
-
- //이미지 데이터에 대한 전역 색상표;
- $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][" graphicextension"];
-
- $imagedata[3] = chr((ord($imagedata[3]) & 0xE3) | ($this->orgvars[$this->index-1][" 처분 방법"] << 2));
- $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"];
- 取消設定($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->指針);
- 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 ;
- }
- }
- }
- }
- }
- }
} } } } } | }
}