欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现图片缩略图的方法

程序员文章站 2024-04-01 23:24:16
本段代码实现功能有这些: 支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质...

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

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

完整类的代码如下:

<?php
/**
* 功能:php生成缩略图片的类
*/
class resizeimage{
public $type;//图片类型
public $width;//实际宽度
public $height;//实际高度
public $resize_width;//改变后的宽度
public $resize_height;//改变后的高度
public $cut;//是否裁图
public $srcimg;//源图象 
public $dstimg;//目标图象地址
public $im;//临时创建的图象
public $quality;//图片质量
function resizeimage($img,$wid,$hei,$c,$dstpath,$quality=100){
$this->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($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->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($ratio<$resize_ratio){
@$newimg=imagecreatetruecolor(($this->resize_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']<16777216)
{
$palette=unpack('v'.$bmp['colors'],fread($f1,$bmp['colors']*4));
}
$img=fread($f1,$bmp['size_bitmap']);
$vide=chr(0);
$res=imagecreatetruecolor($bmp['width'],$bmp['height']);
$p=0;
$y=$bmp['height']-1;
while($y>=0)
{
$x=0;
while($x<$bmp['width'])
{
if($bmp['bits_per_pixel']==24)
$color=unpack("v",substr($img,$p,3).$vide);
elseif($bmp['bits_per_pixel']==16)
{
$color=unpack("n",substr($img,$p,2));
$color[1]=$palette[$color[1]+1];
}
elseif($bmp['bits_per_pixel']==8)
{
$color=unpack("n",$vide.substr($img,$p,1));
$color[1]=$palette[$color[1]+1];
}
elseif($bmp['bits_per_pixel']==4)
{
$color=unpack("n",$vide.substr($img,floor($p),1));
if(($p*2)%2==0)$color[1]=($color[1]>>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];
}
else
returnfalse;
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实现图片缩略图的方法,希望对大家有所帮助!