php 生成缩略图代码可自动填充空白处

网站大多数生成缩略图都是按比例操作的,今天我们来看看可以固定图片大小,不够的地方自动填充空白,有需要的朋友可以参考一下.

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. { //开源代码phpfensi.com
  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. ?>