php生成图片缩略图类程序

缩略图用得最多的是我们上传图片时生成一张小图,这样就可以很好的解决图片大小或影响网站整体美观的问题了,下面介绍的生成图片缩略图类都不支持图片上传,我们要先上传再调用此类来操作.

使用如下类就可以生成图片缩略图,代码如下:

  1. <?php
  2. class resizeimage
  3. {
  4. //图片类型
  5. var $type;
  6. //实际宽度
  7. var $width;
  8. //实际高度
  9. var $height;
  10. //改变后的宽度
  11. var $resize_width;
  12. //改变后的高度
  13. var $resize_height;
  14. //是否裁图
  15. var $cut;
  16. //源图象
  17. var $srcimg;
  18. //目标图象地址
  19. var $dstimg;
  20. //临时创建的图象
  21. var $im;
  22. function resizeimage($img, $wid, $hei,$c,$dstpath)
  23. {
  24. $this->srcimg = $img;
  25. $this->resize_width = $wid;
  26. $this->resize_height = $hei;
  27. $this->cut = $c;
  28. //图片的类型
  29. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
  30. //初始化图象
  31. $this->initi_img();
  32. //目标图象地址
  33. $this -> dst_img($dstpath);
  34. //--
  35. $this->width = imagesx($this->im);
  36. $this->height = imagesy($this->im);
  37. //生成图象
  38. $this->newimg();
  39. ImageDestroy ($this->im);
  40. }
  41. function newimg()
  42. {
  43. //改变后的图象的比例
  44. $resize_ratio = ($this->resize_width)/($this->resize_height);
  45. //实际图象的比例
  46. $ratio = ($this->width)/($this->height);
  47. if(($this->cut)=="1")
  48. //裁图
  49. {
  50. if($ratio>=$resize_ratio)
  51. //高度优先
  52. {
  53. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  54. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this-
  55. >resize_height, (($this->height)*$resize_ratio), $this->height);
  56. ImageJpeg ($newimg,$this->dstimg);
  57. }
  58. if($ratio<$resize_ratio)
  59. //宽度优先
  60. {
  61. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  62. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this-
  63. >resize_height, $this->width, (($this->width)/$resize_ratio));
  64. ImageJpeg ($newimg,$this->dstimg);
  65. }
  66. }
  67. else
  68. //不裁图
  69. {
  70. if($ratio>=$resize_ratio)
  71. {
  72. $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
  73. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this-
  74. >resize_width)/$ratio, $this->width, $this->height);
  75. ImageJpeg ($newimg,$this->dstimg);
  76. }
  77. if($ratio<$resize_ratio)
  78. {
  79. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  80. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio,
  81. //开源代码phpfensi.com
  82. $this->resize_height, $this->width, $this->height);
  83. ImageJpeg ($newimg,$this->dstimg);
  84. }
  85. }
  86. }
  87. //初始化图象
  88. function initi_img()
  89. {
  90. if($this->type=="jpg")
  91. {
  92. $this->im = imagecreatefromjpeg($this->srcimg);
  93. }
  94. if($this->type=="gif")
  95. {
  96. $this->im = imagecreatefromgif($this->srcimg);
  97. }
  98. if($this->type=="png")
  99. {
  100. $this->im = imagecreatefrompng($this->srcimg);
  101. }
  102. }
  103. //图象目标地址
  104. function dst_img($dstpath)
  105. {
  106. $full_length = strlen($this->srcimg);
  107. $type_length = strlen($this->type);
  108. $name_length = $full_length-$type_length;
  109. $name = substr($this->srcimg,0,$name_length-1);
  110. $this->dstimg = $dstpath;
  111. //echo $this->dstimg;
  112. }
  113. }
  114. ?>

类的调用方法:

$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");

就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高.

实例2:PHP缩略图 等比例无损压缩,可填充空白区域补充色,代码如下:

  1. <?php
  2. error_reporting( E_ALL );
  3. // 测试
  4. imagezoom('1.jpg', '2.jpg', 400, 300, '#FFFFFF');
  5. /*
  6. php缩略图函数:
  7. 等比例无损压缩,可填充补充色 author: 华仔
  8. 主持格式:
  9. bmp 、jpg 、gif、png
  10. param:
  11. @srcimage : 要缩小的图片
  12. @dstimage : 要保存的图片
  13. @dst_width: 缩小宽
  14. @dst_height: 缩小高
  15. @backgroundcolor: 补充色 如:#FFFFFF 支持 6位 不支持3位
  16. */
  17. function imagezoom( $srcimage, $dstimage, $dst_width, $dst_height, $backgroundcolor ) {
  18. // 中文件名乱码
  19. if ( PHP_OS == 'WINNT' ) {
  20. $srcimage = iconv('UTF-8', 'GBK', $srcimage);
  21. $dstimage = iconv('UTF-8', 'GBK', $dstimage);
  22. }
  23. $dstimg = imagecreatetruecolor( $dst_width, $dst_height );
  24. $color = imagecolorallocate($dstimg
  25. , hexdec(substr($backgroundcolor, 1, 2))
  26. , hexdec(substr($backgroundcolor, 3, 2))
  27. , hexdec(substr($backgroundcolor, 5, 2))
  28. );
  29. imagefill($dstimg, 0, 0, $color);
  30. if ( !$arr=getimagesize($srcimage) ) {
  31. echo "要生成缩略图的文件不存在";
  32. exit;
  33. }
  34. $src_width = $arr[0];
  35. $src_height = $arr[1];
  36. $srcimg = null;
  37. $method = getcreatemethod( $srcimage );
  38. if ( $method ) {
  39. eval( '$srcimg = ' . $method . ';' );
  40. }
  41. $dst_x = 0;
  42. $dst_y = 0;
  43. $dst_w = $dst_width;
  44. $dst_h = $dst_height;
  45. if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {
  46. $dst_w = $src_width * ( $dst_height / $src_height );
  47. $dst_x = ( $dst_width - $dst_w ) / 2;
  48. } elseif ( ($dst_width / $dst_height - $src_width / $src_height) < 0 ) {
  49. $dst_h = $src_height * ( $dst_width / $src_width );
  50. $dst_y = ( $dst_height - $dst_h ) / 2;
  51. }
  52. imagecopyresampled($dstimg, $srcimg, $dst_x
  53. , $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
  54. // 保存格式
  55. $arr = array(
  56. 'jpg' => 'imagejpeg'
  57. , 'jpeg' => 'imagejpeg'
  58. , 'png' => 'imagepng'
  59. , 'gif' => 'imagegif'
  60. , 'bmp' => 'imagebmp'
  61. );
  62. $suffix = strtolower( array_pop(explode('.', $dstimage ) ) );
  63. if (!in_array($suffix, array_keys($arr)) ) {
  64. echo "保存的文件名错误";
  65. exit;
  66. } else {
  67. eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' );
  68. }
  69. imagejpeg($dstimg, $dstimage);
  70. imagedestroy($dstimg);
  71. imagedestroy($srcimg);
  72. }
  73. function getcreatemethod( $file ) {
  74. $arr = array(
  75. '474946' => "imagecreatefromgif('$file')"
  76. , 'FFD8FF' => "imagecreatefromjpeg('$file')"
  77. , '424D' => "imagecreatefrombmp('$file')"
  78. , '89504E' => "imagecreatefrompng('$file')"
  79. );
  80. $fd = fopen( $file, "rb" );
  81. $data = fread( $fd, 3 );
  82. $data = str2hex( $data );
  83. if ( array_key_exists( $data, $arr ) ) {
  84. return $arr[$data];
  85. } elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) {
  86. return $arr[substr($data, 0, 4)];
  87. } else {
  88. return false;
  89. }
  90. }
  91. function str2hex( $str ) {
  92. $ret = "";
  93. for( $i = 0; $i < strlen( $str ) ; $i++ ) {
  94. $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) )
  95. : '0'. strval( dechex( ord($str[$i]) ) );
  96. }
  97. return strtoupper( $ret );
  98. }
  99. // BMP 创建函数 php本身无
  100. function imagecreatefrombmp($filename)
  101. {
  102. if (! $f1 = fopen($filename,"rb")) return FALSE;
  103. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  104. if ($FILE['file_type'] != 19778) return FALSE;
  105. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  106. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  107. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  108. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  109. if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  110. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  111. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  112. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  113. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  114. $BMP['decal'] = 4-(4*$BMP['decal']);
  115. if ($BMP['decal'] == 4) $BMP['decal'] = 0;
  116. $PALETTE = array();
  117. if ($BMP['colors'] < 16777216)
  118. {
  119. $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
  120. }
  121. $IMG = fread($f1,$BMP['size_bitmap']);
  122. $VIDE = chr(0);
  123. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  124. $P = 0;
  125. $Y = $BMP['height']-1;
  126. while ($Y >= 0)
  127. {
  128. $X=0;
  129. while ($X < $BMP['width'])
  130. {
  131. if ($BMP['bits_per_pixel'] == 24)
  132. $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
  133. elseif ($BMP['bits_per_pixel'] == 16)
  134. {
  135. $COLOR = unpack("n",substr($IMG,$P,2));
  136. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  137. }
  138. elseif ($BMP['bits_per_pixel'] == 8)
  139. {
  140. $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
  141. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  142. }
  143. elseif ($BMP['bits_per_pixel'] == 4)
  144. {
  145. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  146. if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
  147. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  148. }
  149. elseif ($BMP['bits_per_pixel'] == 1)
  150. {
  151. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  152. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  153. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  154. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  155. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  156. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  157. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  158. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  159. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  160. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  161. }
  162. else
  163. return FALSE;
  164. imagesetpixel($res,$X,$Y,$COLOR[1]);
  165. $X++;
  166. $P += $BMP['bytes_per_pixel'];
  167. }
  168. $Y--;
  169. $P+=$BMP['decal'];
  170. }
  171. fclose($f1);
  172. return $res;
  173. }
  174. // BMP 保存函数,php本身无
  175. function imagebmp ($im, $fn = false)
  176. {
  177. if (!$im) return false;
  178. if ($fn === false) $fn = 'php://output';
  179. $f = fopen ($fn, "w");
  180. if (!$f) return false;
  181. $biWidth = imagesx ($im);
  182. $biHeight = imagesy ($im);
  183. $biBPLine = $biWidth * 3;
  184. $biStride = ($biBPLine + 3) & ~3;
  185. $biSizeImage = $biStride * $biHeight;
  186. $bfOffBits = 54;
  187. $bfSize = $bfOffBits + $biSizeImage;
  188. fwrite ($f, 'BM', 2);
  189. fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));
  190. fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));
  191. $numpad = $biStride - $biBPLine;
  192. for ($y = $biHeight - 1; $y >= 0; --$y)
  193. {
  194. for ($x = 0; $x < $biWidth; ++$x)
  195. {
  196. $col = imagecolorat ($im, $x, $y);
  197. fwrite ($f, pack ('V', $col), 3);
  198. }
  199. for ($i = 0; $i < $numpad; ++$i)
  200. fwrite ($f, pack ('C', 0));
  201. }
  202. fclose ($f);
  203. return true;
  204. }
  205. ?>

总结,第一个类文件不如第二个好,因为第一个生成图之后图片会有点变模糊,后面这个生成类是高质量的.