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

使用PHP生成图片的缩略图的方法

程序员文章站 2023-11-26 15:23:58
 功能:支持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图片缩略图类:phpthumb

该类除了缩放图片外,还能将图片转换成不同的格式输出(比如将gif格式的图片输出为png格式),其特色功能还包括色彩、特效等

官方网址:http://phpthumb.sourceforge.net/

    we'll be using phpthumb , the open source php script to generate thumbnails on the fly.

    download and extract phpthumb to somewhere in your website folder. now to use a rounded corner thumbnail of an image, simply use the img tag of html with src as

 <img src=”phpthumb.php?src=test.jpg&w=200&h=150&fltr[]=ric|20|20&f=png” /> 

 adjust the path to phpthumb according to where you place the phpthumb files.

 <html> 
 <head>
 </head> 
 <body> 
  <img src="phpthumb/phpthumb.php?src=../images/test.jpg&w=400&fltr[]=fram|3|2|cc9966|333333|cccccc" alt=""> 
 </body> 
 </html>

 

一般只需要下面的文件就可以了

其实phpthumb的功能比我原先想象的要强大一些,下面把他的其他一些有用的参数列一下:

src:目标图片的地址

w:输出图片的宽度

h:输出图片的高度(如果不指定他将按w参数等比缩放)

q:输出如果是jpg格式的,可以规定它的输出质量

bg:输出时的背景(如果需要)

sw、sh、sx、sy:局部输出,宽高、起始位置

f:输出格式,可以为jpeg、png、gif、ico

sfn:输出gif动画中的某一帧

fltr[]:滤镜,可以有很多效果,包括锐化、模糊、旋翻转、水印、边框、遮照、色彩调整等

使用演示:

 

//加载类库文件

 require_once 'path/to/thumblib.inc.php';

 //实例化类库,传入你要处理的图片的地址可以是网络地址,也可以是本地地址

 $thumb = phpthumbfactory::create('http://www.shlongyingjixie.com/');

 //把图片等比缩小到最大宽度 100px或者最高100px,当只输入一个参数的时候,是限制最宽的尺寸。

 $thumb->resize(100, 100);

 //把图片等比缩小到原来的百分数,比如50就是原来的50%。

 $thumb->resizepercent(50);

 //截取一个175px * 175px的图片,注意这个是截取,超出的部分直接裁切掉,不是强制改变尺寸。
 $thumb->adaptiveresize(175, 175);

 //从图片的中心计算,截取200px * 100px的图片。

 $thumb->cropfromcenter(200, 100);

 //截图,前两个参数分别是需要解出的图片的右上角的坐标x,y。 后面两个参数是需要解出的图片宽,高。

 $thumb->crop(100, 100, 300, 200);
 
 //把图片顺时针反转180度

 $thumb->rotateimagendegrees(180);

 //保存(生成)图片,你可以保存其他格式,详细参考文档

 $thumb->save( 'newpath/of/image.jpg ' );