本段代码实现功能有这些:
支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化。
完整类的代码如下:
srcimg=$img;$this->resize_width=$wid;$this->resize_height=$hei;$this->cut=$c;$this->quality=$quality;$this->type=strtolower(substr(strrchr($this->srcimg,'.'),1));//图片的类型$this->initi_img();//初始化图象$this -> dst_img($dstpath);//目标图象地址@$this->width=imagesx($this->im);@$this->height=imagesy($this->im);$this->newimg();//生成图象@imagedestroy($this->im);}function newimg(){$resize_ratio=($this->resize_width)/($this->resize_height);//改变后的图象的比例@$ratio=($this->width)/($this->height);//实际图象的比例if(($this->cut)=='1'){//裁图if($img_func==='imagepng'&&(str_replace('.','',php_version)>=512)){ //针对php版本大于5.12参数变化后的处理情况$quality=9;}if($ratio>=$resize_ratio){//高度优先$newimg=imagecreatetruecolor($this->resize_width,$this->resize_height);imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,(($this->height)*$resize_ratio),$this->height);imagejpeg($newimg,$this->dstimg,$this->quality);}if($ratioresize_width,$this->resize_height);imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,$this->width,(($this->width)/$resize_ratio));imagejpeg($newimg,$this->dstimg,$this->quality);}}else{//不裁图if($ratio>=$resize_ratio){$newimg=imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);imagecopyresampled($newimg,$this->im,0,0,0,0,$this->resize_width,($this->resize_width)/$ratio,$this->width,$this->height);imagejpeg($newimg,$this->dstimg,$this->quality);}if($ratioresize_height)*$ratio,$this->resize_height);@imagecopyresampled($newimg,$this->im,0,0,0,0,($this->resize_height)*$ratio,$this->resize_height,$this->width,$this->height);@imagejpeg($newimg,$this->dstimg,$this->quality);}}}function initi_img(){//初始化图象if($this->type=='jpg' || $this->type=='jpeg'){$this->im=imagecreatefromjpeg($this->srcimg);}if($this->type=='gif'){$this->im=imagecreatefromgif($this->srcimg);}if($this->type=='png'){$this->im=imagecreatefrompng($this->srcimg);}if($this->type=='wbm'){@$this->im=imagecreatefromwbmp($this->srcimg);}if($this->type=='bmp'){$this->im=$this->imagecreatefrombmp($this->srcimg);}}function dst_img($dstpath){//图象目标地址$full_length=strlen($this->srcimg);$type_length=strlen($this->type);$name_length=$full_length-$type_length;$name=substr($this->srcimg,0,$name_length-1);$this->dstimg=$dstpath;//echo $this->dstimg;}function imagecreatefrombmp($filename){ //自定义函数处理bmp图片if(!$f1=fopen($filename,rb))returnfalse;$file=unpack(vfile_type/vfile_size/vreserved/vbitmap_offset,fread($f1,14));if($file['file_type']!=19778)returnfalse;$bmp=unpack('vheader_size/vwidth/vheight/vplanes/vbits_per_pixel'.'/vcompression/vsize_bitmap/vhoriz_resolution'.'/vvert_resolution/vcolors_used/vcolors_important',fread($f1,40));$bmp['colors']=pow(2,$bmp['bits_per_pixel']);if($bmp['size_bitmap']==0)$bmp['size_bitmap']=$file['file_size']-$file['bitmap_offset'];$bmp['bytes_per_pixel']=$bmp['bits_per_pixel']/8;$bmp['bytes_per_pixel2']=ceil($bmp['bytes_per_pixel']);$bmp['decal']=($bmp['width']*$bmp['bytes_per_pixel']/4);$bmp['decal']-=floor($bmp['width']*$bmp['bytes_per_pixel']/4);$bmp['decal']=4-(4*$bmp['decal']);if($bmp['decal']==4)$bmp['decal']=0;$palette=array();if($bmp['colors']=0){$x=0;while($x>4);else$color[1]=($color[1]&0x0f);$color[1]=$palette[$color[1]+1];}elseif($bmp['bits_per_pixel']==1){$color=unpack(n,$vide.substr($img,floor($p),1));if(($p*8)%8==0)$color[1]=$color[1]>>7;elseif(($p*8)%8==1)$color[1]=($color[1]&0x40)>>6;elseif(($p*8)%8==2)$color[1]=($color[1]&0x20)>>5;elseif(($p*8)%8==3)$color[1]=($color[1]&0x10)>>4;elseif(($p*8)%8==4)$color[1]=($color[1]&0x8)>>3;elseif(($p*8)%8==5)$color[1]=($color[1]&0x4)>>2;elseif(($p*8)%8==6)$color[1]=($color[1]&0x2)>>1;elseif(($p*8)%8==7)$color[1]=($color[1]&0x1);$color[1]=$palette[$color[1]+1];}elsereturnfalse;imagesetpixel($res,$x,$y,$color[1]);$x++;$p+=$bmp['bytes_per_pixel'];}$y--;$p+=$bmp['decal'];}fclose($f1);return$res;}}?>
使用方法很简单代码如下:
$resizeimage=new resizeimage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');
以上代码是小编给大家分享的php实现图片缩略图的方法,希望对大家有所帮助!