GIF文件处理

  1 <?php
2 /*************************************
3 * GIF文件处理---H_gif.php(该类不是本人所写)
4 * 创建人: 网络copy
5 * 创建时间:2008-12-25 9:29
6 * 更新时间:2009-2-18 13:36
7 * Email:yin273642232@163.com QQ:273642232
8 *************************************/
9 //Start Class
10 Class H_gif {
11 public $GIF = "GIF89a"; //GIF header 6 bytes
12 public $VER = "GIF V2.06"; //Encoder version
13 public $BUF = Array();
14 public $LOP = 0;
15 public $DIS = 2;
16 public $COL = -1;
17 public $IMG = -1;
18 public $ERR = Array(
19 'ERR00' =>"Does not supported function for only one image!",
20 'ERR01' =>"Source is not a GIF image!",
21 'ERR02' =>"Unintelligible flag ",
22 'ERR03' =>"Could not make animation from animated GIF source"
23 );
24 public $H_desce; //描述
25 public $H_ROOT_DIR; //文件地址路径
26
27 public function __construct(){
28 //当实例化一个对象的时候,这个对象的这个方法首先被调用
29 return '';
30 }
31 public function __destruct(){
32 //当删除一个对象或对象操作终止的时候,调用该方法
33 return '';
34 }
35 public function __get($key){
36 //当试图读取一个并不存在的属性的时候被调用
37 return '['.$key.'] Variable not find';
38 }
39 public function __set($key,$val){
40 //当试图向一个并不存在的属性写入值的时候被调用
41 return '['.$key.'] Variable not find';
42 }
43 public function __call($key,$args){
44 //当试图调用一个对象并不存在的方法时,调用该方法
45 return '['.$key.'] Function not find';
46 }
47 public function __toString(){
48 //当打印一个对象的时候被调用
49 return $this -> H_desce();
50 }
51 public function __clone(){
52 //当对象被克隆时,被调用
53 return "clone";
54 }
55 public function H_desce(){
56 //返回描述
57 $this -> H_desce .= '类名:H_gif-Gif图像处理(摘自网络);';
58 $this -> H_desce .= '函数:H_set_gif($GIF_src,$GIF_dly,$GIF_lop,$GIF_dis,$GIF_red=0,$GIF_grn=0,$GIF_blu=0,$GIF_mod),返回:gif图,参数:$GIF_src-数据源.$GIF_dly-延时.$GIF_lop-循环次数.$GIF_dis-透明度.$GIF_red-R值.$GIF_grn-G值.$GIF_blu-B值.$GIF_mod-"bin/url";';
59 return $this -> H_desce;
60 }
61 //数据源 延时 循环次数 透明度 r g b "bin/url"
62 public function H_set_gif($GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,$GIF_red=0, $GIF_grn=0, $GIF_blu=0, $GIF_mod){
63 if(!is_array($GIF_src) && !is_array($GIF_tim)){
64 printf( "%s: %s",$this -> VER,$this -> ERR['ERR00']);
65 exit(0);
66 }
67 $this -> LOP = ($GIF_lop > -1 ) ? $GIF_lop : 0;
68 $this -> DIS = ($GIF_dis > -1 ) ? ( ( $GIF_dis < 3 ) ? $GIF_dis : 3 ) : 2;
69 $this -> COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1 ) ? ( $GIF_red | ( $GIF_grn << 8 ) | ( $GIF_blu << 16 ) ) : -1;
70 for($i = 0;$i < count($GIF_src);$i++ ){
71 if(strToLower($GIF_mod) == "url"){
72 $this -> BUF[] = fread(fopen($GIF_src[$i],"rb"),filesize($GIF_src[$i]));
73 }else if(strToLower($GIF_mod) == "bin"){
74 $this -> BUF[] = $GIF_src[$i];
75 }else{
76 printf("%s: %s ( %s )!",$this -> VER,$this -> ERR['ERR02'],$GIF_mod);
77 exit(0);
78 }
79 if(substr($this -> BUF[$i],0,6) != "GIF87a" && substr($this -> BUF[$i],0,6) != "GIF89a"){
80 printf("%s: %d %s",$this -> VER,$i,$this -> ERR['ERR01']);
81 exit(0);
82 }
83 for($j =(13+3*(2 << (ord($this -> BUF[$i]{10}) & 0x07))),$k = true; $k ; $j++){
84 switch($this -> BUF[$i]{$j}){
85 case "!" : {
86 if((substr($this -> BUF[$i],($j+3),8)) == "NETSCAPE"){
87 printf("%s: %s ( %s source )!",$this -> VER,$this -> ERR['ERR03'],($i+1));
88 exit(0);
89 }
90 break;
91 }
92 case ";" : {
93 $k = false;
94 break;
95 }
96 }//end switch
97 }//end for
98 }//end for
99 $this -> GIFAddHeader();
100 for($i=0;$i < count($this -> BUF);$i++){
101 $this -> GIFAddFrames($i,$GIF_dly[$i]);
102 }
103 $this -> GIFAddFooter();
104 }
105 public function GIFAddHeader() {
106 $cmap = 0;
107 if(ord($this -> BUF[0]{10}) & 0x80){
108 $cmap = 3*(2 << (ord($this -> BUF[0]{10}) & 0x07));
109 $this -> GIF .= substr($this -> BUF[0],6,7);
110 $this -> GIF .= substr($this -> BUF[0],13,$cmap);
111 $this -> GIF .= "!\377\13NETSCAPE2.0\3\1".$this -> GIFWord($this -> LOP)."\0";
112 }
113 }
114 public function GIFAddFrames($i,$d){
115 $Locals_str = 13+3*(2 << (ord($this -> BUF[$i]{10}) & 0x07));
116 $Locals_end = strlen($this -> BUF[$i])-$Locals_str-1;
117 $Locals_tmp = substr($this -> BUF[$i],$Locals_str,$Locals_end);
118 $Global_len = 2 << (ord($this -> BUF[0]{10}) & 0x07);
119 $Locals_len = 2 << (ord($this -> BUF[$i]{10}) & 0x07);
120 $Global_rgb = substr($this -> BUF[0],13,3*(2 << (ord($this -> BUF[0]{10})&0x07)));
121 $Locals_rgb = substr($this -> BUF[$i],13,3*(2 << (ord($this -> BUF[$i]{10}) & 0x07)));
122 $Locals_ext = "!\xF9\x04".chr(($this -> DIS << 2)+0).chr(($d >> 0) & 0xFF).chr(($d >> 8) & 0xFF)."\x0\x0";
123 if($this -> COL > -1 && ord($this -> BUF[$i]{10}) & 0x80){
124 for($j = 0;$j < (2 << (ord($this -> BUF[$i]{10}) & 0x07)); $j++){
125 if(ord($Locals_rgb{3*$j+0}) == ( $this -> COL >> 0) & 0xFF && ord($Locals_rgb{3*$j+1}) == ($this -> COL >> 8) & 0xFF && ord($Locals_rgb{3*$j+2}) == ($this -> COL >> 16) & 0xFF){
126 $Locals_ext = "!\xF9\x04".chr(($this -> DIS << 2)+1).chr(($d >> 0) & 0xFF).chr(($d >> 8) & 0xFF).chr($j)."\x0";
127 break;
128 }
129 }
130 }
131 switch($Locals_tmp{0}){
132 case "!" : {
133 $Locals_img = substr($Locals_tmp,8,10);
134 $Locals_tmp = substr($Locals_tmp,18,strlen($Locals_tmp)-18);
135 break;
136 }
137 case "," : {
138 $Locals_img = substr($Locals_tmp,0,10 );
139 $Locals_tmp = substr($Locals_tmp,10,strlen($Locals_tmp)-10);
140 break;
141 }
142 }
143 if(ord($this -> BUF[$i]{10}) & 0x80 && $this -> IMG > -1 ){
144 if($Global_len == $Locals_len){
145 if($this -> GIFBlockCompare($Global_rgb,$Locals_rgb,$Global_len)){
146 $this -> GIF .= ($Locals_ext.$Locals_img.$Locals_tmp);
147 }else{
148 $byte = ord($Locals_img{9});
149 $byte |= 0x80;
150 $byte &= 0xF8;
151 $byte |= (ord($this -> BUF[0]{10}) & 0x07);
152 $Locals_img{9} = chr($byte);
153 $this -> GIF .= ($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp);
154 }
155 }else{
156 $byte = ord($Locals_img{9});
157 $byte |= 0x80;
158 $byte &= 0xF8;
159 $byte |= (ord($this -> BUF[$i]{10}) & 0x07);
160 $Locals_img{9} = chr($byte);
161 $this -> GIF .= ($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp);
162 }
163 }else{
164 $this -> GIF .= ($Locals_ext.$Locals_img.$Locals_tmp);
165 }
166 $this -> IMG = 1;
167 }
168 public function GIFAddFooter(){
169 $this -> GIF .= ";";
170 }
171 public function GIFBlockCompare($GlobalBlock,$LocalBlock,$Len){
172 for($i = 0;$i < $Len;$i++){
173 if($GlobalBlock{3*$i+0} != $LocalBlock{3*$i+0} || $GlobalBlock{3*$i+1} != $LocalBlock{3*$i+1} || $GlobalBlock{3*$i+2} != $LocalBlock{3*$i+2}){
174 return (0);
175 }
176 }
177 return (1);
178 }
179 public function GIFWord($int){
180 return (chr($int & 0xFF).chr(($int >> 8) & 0xFF));
181 }
182 public function GetAnimation(){
183 return ($this -> GIF);
184 }
185 }
186 ?>

 

posted @ 2012-01-17 15:26  祥辉  阅读(284)  评论(0编辑  收藏  举报