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