php实现图片缩略图的方法

本文使用php实现的图片缩略图,支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化,需要的朋友可以参考下本段代码。

本段代码实现功能有这些:

支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化。

完整类的代码如下:

  1. <?php
  2. /**
  3. * 功能:php生成缩略图片的类
  4. */
  5. class ResizeImage{
  6. public $type;//图片类型
  7. public $width;//实际宽度
  8. public $height;//实际高度
  9. public $resize_width;//改变后的宽度
  10. public $resize_height;//改变后的高度
  11. public $cut;//是否裁图
  12. public $srcimg;//源图象
  13. public $dstimg;//目标图象地址
  14. public $im;//临时创建的图象
  15. public $quality;//图片质量
  16. function resizeimage($img,$wid,$hei,$c,$dstpath,$quality=100){
  17. $this->srcimg=$img;
  18. $this->resize_width=$wid;
  19. $this->resize_height=$hei;
  20. $this->cut=$c;
  21. $this->quality=$quality;
  22. $this->type=strtolower(substr(strrchr($this->srcimg,'.'),1));//图片的类型
  23. $this->initi_img();//初始化图象
  24. $this -> dst_img($dstpath);//目标图象地址
  25. @$this->width=imagesx($this->im);
  26. @$this->height=imagesy($this->im);
  27. $this->newimg();//生成图象
  28. @ImageDestroy($this->im);
  29. }
  30. function newimg(){
  31. $resize_ratio=($this->resize_width)/($this->resize_height);//改变后的图象的比例
  32. @$ratio=($this->width)/($this->height);//实际图象的比例
  33. if(($this->cut)=='1'){//裁图
  34. if($img_func==='imagepng'&&(str_replace('.','',PHP_VERSION)>=512)){ //针对php版本大于5.12参数变化后的处理情况
  35. $quality=9;
  36. }
  37. if($ratio>=$resize_ratio){//高度优先
  38. $newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);
  39. imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,(($this->height)*$resize_ratio),$this->height);
  40. imagejpeg($newimg,$this->dstimg,$this->quality);
  41. }
  42. if($ratio<$resize_ratio){//宽度优先
  43. $newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);
  44. imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,$this->width,(($this->width)/$resize_ratio));
  45. imagejpeg($newimg,$this->dstimg,$this->quality);
  46. }
  47. }else{//不裁图
  48. if($ratio>=$resize_ratio){
  49. $newimg=imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
  50. imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,($this->resize_width)/$ratio,$this->width,$this->height);
  51. imagejpeg($newimg,$this->dstimg,$this->quality);
  52. }
  53. if($ratio<$resize_ratio){
  54. @$newimg=imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  55. @imagecopyresampled($newimg,$this->im,0,0,0,0,($this->resize_height)*$ratio,$this->resize_height,$this->width,$this->height);
  56. @imagejpeg($newimg,$this->dstimg,$this->quality);
  57. }
  58. }
  59. }
  60. function initi_img(){//初始化图象
  61. if($this->type=='jpg' || $this->type=='jpeg'){
  62. $this->im=imagecreatefromjpeg($this->srcimg);
  63. }
  64. if($this->type=='gif'){
  65. $this->im=imagecreatefromgif($this->srcimg);
  66. }
  67. if($this->type=='png'){
  68. $this->im=imagecreatefrompng($this->srcimg);
  69. }
  70. if($this->type=='wbm'){
  71. @$this->im=imagecreatefromwbmp($this->srcimg);
  72. }
  73. if($this->type=='bmp'){
  74. $this->im=$this->ImageCreateFromBMP($this->srcimg);
  75. }
  76. }
  77. function dst_img($dstpath){//图象目标地址
  78. $full_length=strlen($this->srcimg);
  79. $type_length=strlen($this->type);
  80. $name_length=$full_length-$type_length;
  81. $name=substr($this->srcimg,0,$name_length-1);
  82. $this->dstimg=$dstpath;
  83. //echo $this->dstimg;
  84. }
  85. function ImageCreateFromBMP($filename){ //自定义函数处理bmp图片
  86. if(!$f1=fopen($filename,"rb"))returnFALSE;
  87. $FILE=unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset",fread($f1,14));
  88. if($FILE['file_type']!=19778)returnFALSE;
  89. $BMP=unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  90. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  91. '/Vvert_resolution/Vcolors_used/Vcolors_important',fread($f1,40));
  92. $BMP['colors']=pow(2,$BMP['bits_per_pixel']);
  93. if($BMP['size_bitmap']==0)$BMP['size_bitmap']=$FILE['file_size']-$FILE['bitmap_offset'];
  94. $BMP['bytes_per_pixel']=$BMP['bits_per_pixel']/8;
  95. $BMP['bytes_per_pixel2']=ceil($BMP['bytes_per_pixel']);
  96. $BMP['decal']=($BMP['width']*$BMP['bytes_per_pixel']/4);
  97. $BMP['decal']-=floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  98. $BMP['decal']=4-(4*$BMP['decal']);
  99. if($BMP['decal']==4)$BMP['decal']=0;
  100. $PALETTE=array();
  101. if($BMP['colors']<16777216)
  102. {
  103. $PALETTE=unpack('V'.$BMP['colors'],fread($f1,$BMP['colors']*4));
  104. }
  105. $IMG=fread($f1,$BMP['size_bitmap']);
  106. $VIDE=chr(0);
  107. $res=imagecreatetruecolor($BMP['width'],$BMP['height']);
  108. $P=0;
  109. $Y=$BMP['height']-1;
  110. while($Y>=0)
  111. {
  112. $X=0;
  113. while($X<$BMP['width'])
  114. {
  115. if($BMP['bits_per_pixel']==24)
  116. $COLOR=unpack("V",substr($IMG,$P,3).$VIDE);
  117. elseif($BMP['bits_per_pixel']==16)
  118. {
  119. $COLOR=unpack("n",substr($IMG,$P,2));
  120. $COLOR[1]=$PALETTE[$COLOR[1]+1];
  121. }
  122. elseif($BMP['bits_per_pixel']==8)
  123. {
  124. $COLOR=unpack("n",$VIDE.substr($IMG,$P,1));
  125. $COLOR[1]=$PALETTE[$COLOR[1]+1];
  126. }
  127. elseif($BMP['bits_per_pixel']==4)
  128. {
  129. $COLOR=unpack("n",$VIDE.substr($IMG,floor($P),1));
  130. if(($P*2)%2==0)$COLOR[1]=($COLOR[1]>>4);else$COLOR[1]=($COLOR[1]&0x0F);
  131. $COLOR[1]=$PALETTE[$COLOR[1]+1];
  132. }
  133. elseif($BMP['bits_per_pixel']==1)
  134. {
  135. $COLOR=unpack("n",$VIDE.substr($IMG,floor($P),1));
  136. if(($P*8)%8==0)$COLOR[1]=$COLOR[1]>>7;
  137. elseif(($P*8)%8==1)$COLOR[1]=($COLOR[1]&0x40)>>6;
  138. elseif(($P*8)%8==2)$COLOR[1]=($COLOR[1]&0x20)>>5;
  139. elseif(($P*8)%8==3)$COLOR[1]=($COLOR[1]&0x10)>>4;
  140. elseif(($P*8)%8==4)$COLOR[1]=($COLOR[1]&0x8)>>3;
  141. elseif(($P*8)%8==5)$COLOR[1]=($COLOR[1]&0x4)>>2;
  142. elseif(($P*8)%8==6)$COLOR[1]=($COLOR[1]&0x2)>>1;
  143. elseif(($P*8)%8==7)$COLOR[1]=($COLOR[1]&0x1);
  144. $COLOR[1]=$PALETTE[$COLOR[1]+1];
  145. }
  146. else
  147. returnFALSE;
  148. imagesetpixel($res,$X,$Y,$COLOR[1]);
  149. $X++;
  150. $P+=$BMP['bytes_per_pixel'];
  151. }
  152. $Y--;
  153. $P+=$BMP['decal'];
  154. }
  155. fclose($f1);
  156. return$res;
  157. }
  158. }
  159. ?>

使用方法很简单代码如下:

$resizeimage=new ResizeImage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');

以上代码是小编给大家分享的php实现图片缩略图的方法,希望对大家有所帮助!