Thumb.php 5.0 KB

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