php生成缩略图类

,生成缩略图是当我们要上传图片时如果图片较大我们需要生成一张指定大小的小图以在列表页面显示,这样可节省资源也是现在常用的做法,下面我来分享一个php生成缩略图类,支持自定义高和宽,还支持按高和宽截图,php生成缩略图类的代码如下:

  1. class resizeimage
  2. {
  3. //图片类型
  4. var $type;
  5. //实际宽度
  6. var $width;
  7. //实际高度
  8. var $height;
  9. //改变后的宽度
  10. var $resize_width;
  11. //改变后的高度
  12. var $resize_height;
  13. //是否裁图
  14. var $cut;
  15. //源图象
  16. var $srcimg;
  17. //目标图象地址
  18. var $dstimg;
  19. //临时创建的图象
  20. var $im;
  21. function resizeimage($img, $wid, $hei,$c,$dstpath)
  22. {
  23. $this->srcimg = $img;
  24. $this->resize_width = $wid;
  25. $this->resize_height = $hei;
  26. $this->cut = $c;
  27. //图片的类型
  28. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
  29. //初始化图象
  30. $this->initi_img();
  31. //目标图象地址
  32. $this -> dst_img($dstpath);
  33. //--
  34. $this->width = imagesx($this->im);
  35. $this->height = imagesy($this->im);
  36. //生成图象
  37. $this->newimg();
  38. ImageDestroy ($this->im);
  39. }
  40. function newimg()
  41. {
  42. //改变后的图象的比例
  43. $resize_ratio = ($this->resize_width)/($this->resize_height);
  44. //实际图象的比例
  45. $ratio = ($this->width)/($this->height);
  46. if(($this->cut)=="1")
  47. //裁图
  48. {
  49. if($ratio>=$resize_ratio)
  50. //高度优先
  51. {
  52. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  53. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
  54. ImageJpeg ($newimg,$this->dstimg);
  55. }
  56. if($ratio<$resize_ratio)
  57. //宽度优先
  58. {
  59. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  60. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
  61. ImageJpeg ($newimg,$this->dstimg);
  62. }
  63. }
  64. else
  65. //不裁图
  66. {
  67. if($ratio>=$resize_ratio)
  68. {
  69. $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
  70. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
  71. ImageJpeg ($newimg,$this->dstimg);
  72. }
  73. if($ratio<$resize_ratio)
  74. {
  75. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  76. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
  77. ImageJpeg ($newimg,$this->dstimg);
  78. }
  79. }
  80. }
  81. //初始化图象
  82. function initi_img()
  83. {
  84. if($this->type=="jpg")
  85. {
  86. $this->im = imagecreatefromjpeg($this->srcimg);
  87. }
  88. if($this->type=="gif")
  89. {
  90. $this->im = imagecreatefromgif($this->srcimg);
  91. }
  92. if($this->type=="png")
  93. {
  94. $this->im = imagecreatefrompng($this->srcimg);
  95. }
  96. }
  97. //图象目标地址
  98. function dst_img($dstpath)
  99. {
  100. $full_length = strlen($this->srcimg);
  101. $type_length = strlen($this->type);
  102. $name_length = $full_length-$type_length;
  103. $name = substr($this->srcimg,0,$name_length-1);
  104. $this->dstimg = $dstpath;
  105. //echo $this->dstimg;
  106. }
  107. }
  108. $resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg");
  109. ?>
  110. //实例二,代码如下:
  111. <?php
  112. /**
  113. * 生成缩略图
  114. * @author yangzhiguo0903@163.com
  115. * @param string 源图绝对完整地址{带文件名及后缀名}
  116. * @param string 目标图绝对完整地址{带文件名及后缀名}
  117. * @param int 缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
  118. * @param int 缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
  119. * @param int 是否裁切{宽,高必须非0}
  120. * @param int/float 缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
  121. * @return boolean
  122. */
  123. function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
  124. {
  125. if(!is_file($src_img))
  126. {
  127. return false;
  128. }
  129. $ot = fileext($dst_img);
  130. $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
  131. $srcinfo = getimagesize($src_img);
  132. $src_w = $srcinfo[0];
  133. $src_h = $srcinfo[1];
  134. $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
  135. $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
  136. $dst_h = $height;
  137. $dst_w = $width;
  138. $x = $y = 0;
  139. /**
  140. * 缩略图不超过源图尺寸(前提是宽或高只有一个)
  141. */
  142. if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
  143. {
  144. $proportion = 1;
  145. }
  146. if($width> $src_w)
  147. {
  148. $dst_w = $width = $src_w;
  149. }
  150. if($height> $src_h)
  151. {
  152. $dst_h = $height = $src_h;
  153. }
  154. if(!$width && !$height && !$proportion)
  155. {
  156. return false;
  157. }
  158. if(!$proportion)
  159. {
  160. if($cut == 0)
  161. {
  162. if($dst_w && $dst_h)
  163. {
  164. if($dst_w/$src_w> $dst_h/$src_h)
  165. {
  166. $dst_w = $src_w * ($dst_h / $src_h);
  167. $x = 0 - ($dst_w - $width) / 2;
  168. }
  169. else
  170. {
  171. $dst_h = $src_h * ($dst_w / $src_w);
  172. $y = 0 - ($dst_h - $height) / 2;
  173. }
  174. }
  175. else if($dst_w xor $dst_h)
  176. {
  177. if($dst_w && !$dst_h) //有宽无高
  178. {
  179. $propor = $dst_w / $src_w;
  180. $height = $dst_h = $src_h * $propor;
  181. }
  182. else if(!$dst_w && $dst_h) //有高无宽
  183. {
  184. $propor = $dst_h / $src_h;
  185. $width = $dst_w = $src_w * $propor;
  186. }
  187. }
  188. }
  189. else
  190. {
  191. if(!$dst_h) //裁剪时无高
  192. {
  193. $height = $dst_h = $dst_w;
  194. }
  195. if(!$dst_w) //裁剪时无宽
  196. {
  197. $width = $dst_w = $dst_h;
  198. }
  199. $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
  200. $dst_w = (int)round($src_w * $propor);
  201. $dst_h = (int)round($src_h * $propor);
  202. $x = ($width - $dst_w) / 2;
  203. $y = ($height - $dst_h) / 2;
  204. }
  205. }
  206. else
  207. {
  208. $proportion = min($proportion, 1);
  209. $height = $dst_h = $src_h * $proportion;
  210. $width = $dst_w = $src_w * $proportion;
  211. }
  212. $src = $createfun($src_img);
  213. $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
  214. $white = imagecolorallocate($dst, 255, 255, 255);
  215. imagefill($dst, 0, 0, $white);
  216. if(function_exists('imagecopyresampled'))
  217. {
  218. imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  219. }
  220. else
  221. {
  222. imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  223. }
  224. $otfunc($dst, $dst_img);
  225. imagedestroy($dst);
  226. imagedestroy($src);
  227. return true;
  228. }
  229. //使用方法,代码如下:
  230. //开源代码phpfensi.com
  231. $src_img = "./ROSI_050_002.JPG";
  232. $dst_img = "./ROSI_050_002_thumb.jpg";
  233. $stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);
  234. if($stat){
  235. echo 'Resize Image Success!<br />';
  236. echo '<img src="'.$dst_img.'" />';
  237. }else{
  238. echo 'Resize Image Fail!';
  239. }