PHP生成缩略图类

  1 <?php
  2 /**
  3 * 功能:生成缩略图  
  4 * 作者:phpox  
  5 * 日期:Thu May 17 09:57:05 CST 2007  
  6 */
  7 class CreatThumImage {
  8     //公共变量   
  9     var $srcFile = "";     //原图   
 10     var $echoType;         //输出图片类型,link--不保存为文件;file--保存为文件   
 11     var $im = "";          //临时变量   
 12     var $srcW = "";        //原图宽   
 13     var $srcH = "";        //原图高   
 14 
 15     //设置变量及初始化   
 16     function SetVar($srcFile,$echoType) {
 17         if (!file_exists($srcFile)) {
 18             echo '源图片文件不存在!';
 19             exit();
 20         }
 21         $this->srcFile=$srcFile;
 22         $this->echoType=$echoType;
 23 
 24         $info = "";
 25 
 26         $data = GetImageSize($this->srcFile,$info);
 27 
 28         switch ($data[2]) {
 29             case 1:
 30                 if(!function_exists("imagecreatefromgif")){
 31                    echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
 32                    exit();
 33                 }
 34                 $this->im = ImageCreateFromGIF($this->srcFile);
 35                 break;
 36             case 2:
 37                 if(!function_exists("imagecreatefromjpeg")){
 38                     echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='Javascript:go(-1);'>返回</a>";
 39                     exit();
 40                 }
 41                 $this->im = ImageCreateFromJpeg($this->srcFile);
 42                 break;
 43             case 3:   
 44                 $this->im = ImageCreateFromPNG($this->srcFile);    
 45                 break;
 46       }
 47 
 48       $this->srcW=ImageSX($this->im);
 49 
 50       $this->srcH=ImageSY($this->im);
 51 
 52     }
 53 
 54 
 55     //生成扭曲型缩图   
 56     function Distortion($toFile,$toW,$toH) {
 57 
 58         $cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);
 59 
 60         return $this->EchoImage($cImg,$toFile);
 61 
 62         ImageDestroy($cImg);
 63 
 64     }
 65 
 66        
 67 
 68     //生成按比例缩放的缩图   
 69 
 70     function PRorate($toFile,$toW,$toH) {
 71 
 72         $toWH=$toW/$toH;
 73 
 74         $srcWH=$this->srcW/$this->srcH;
 75 
 76         if($toWH<=$srcWH) {
 77 
 78             $ftoW=$toW;
 79 
 80             $ftoH=$ftoW*($this->srcH/$this->srcW);
 81 
 82         } else {
 83 
 84               $ftoH=$toH;
 85 
 86               $ftoW=$ftoH*($this->srcW/$this->srcH);
 87 
 88         }
 89 
 90         if($this->srcW>$toW||$this->srcH>$toH) {
 91 
 92             $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
 93 
 94             return $this->EchoImage($cImg,$toFile);
 95 
 96             ImageDestroy($cImg);
 97 
 98         } else {
 99 
100             $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
101 
102             return $this->EchoImage($cImg,$toFile);
103 
104             ImageDestroy($cImg);
105 
106         }
107 
108     }
109 
110        
111 
112     //生成最小裁剪后的缩图   
113 
114     function Cut($toFile,$toW,$toH) {
115 
116           $toWH=$toW/$toH;
117 
118           $srcWH=$this->srcW/$this->srcH;
119 
120           if($toWH<=$srcWH) {
121 
122                $ctoH=$toH;
123 
124                $ctoW=$ctoH*($this->srcW/$this->srcH);
125 
126           } else {
127 
128               $ctoW=$toW;
129 
130               $ctoH=$ctoW*($this->srcH/$this->srcW);
131 
132           } 
133 
134         $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
135 
136         $cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
137 
138         return $this->EchoImage($cImg,$toFile);
139 
140         ImageDestroy($cImg);
141 
142         ImageDestroy($allImg);
143 
144     }
145 
146   
147 
148     //生成背景填充的缩图   
149 
150     function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255) {
151 
152         $toWH=$toW/$toH;
153 
154         $srcWH=$this->srcW/$this->srcH;
155 
156         if($toWH<=$srcWH) {
157 
158             $ftoW=$toW;
159 
160             $ftoH=$ftoW*($this->srcH/$this->srcW);
161 
162         } else {
163 
164               $ftoH=$toH;
165 
166               $ftoW=$ftoH*($this->srcW/$this->srcH);
167 
168         }
169 
170         if(function_exists("imagecreatetruecolor")) {
171 
172             @$cImg=ImageCreateTrueColor($toW,$toH);
173 
174             if(!$cImg) {
175 
176                 $cImg=ImageCreate($toW,$toH);
177 
178             }
179 
180         } else {
181 
182             $cImg=ImageCreate($toW,$toH);
183 
184         }
185 
186         $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3);     //填充的背景颜色   
187 
188         ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
189 
190         if($this->srcW>$toW||$this->srcH>$toH) {
191 
192             $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
193 
194              if($ftoW<$toW) {
195 
196                  ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
197 
198              } else if($ftoH<$toH) {
199 
200                  ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
201 
202              } else {
203 
204                  ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
205 
206              }
207 
208         } else {
209 
210              ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
211 
212         }
213 
214         return $this->EchoImage($cImg,$toFile);
215 
216         ImageDestroy($cImg);
217 
218     }
219 
220        
221 
222   
223 
224     function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH) {
225 
226         if(function_exists("imagecreatetruecolor")) {
227 
228             @$creatImg = ImageCreateTrueColor($creatW,$creatH);
229 
230             if($creatImg) {
231 
232                 ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
233 
234             } else {
235 
236                 $creatImg=ImageCreate($creatW,$creatH);
237 
238                 ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
239 
240             }
241 
242          } else {
243 
244             $creatImg=ImageCreate($creatW,$creatH);
245 
246             ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
247 
248          }
249 
250          return $creatImg;
251 
252     }
253 
254        
255 
256     //输出图片,link---只输出,不保存文件。file--保存为文件   
257 
258     function EchoImage($img,$to_File) {
259 
260         switch($this->echoType) {
261 
262             case "link":
263 
264                 if(function_exists('imagejpeg')) return ImageJpeg($img);
265 
266                 else return ImagePNG($img);
267 
268                 break;
269 
270             case "file":
271 
272                 if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
273 
274                 else return ImagePNG($img,$to_File);
275 
276                 break;
277 
278         }
279 
280     }
281 
282 }

 

posted @ 2013-04-21 18:49  百廢待興中  阅读(256)  评论(0编辑  收藏  举报