tpclass.php 4.8 KB

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