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

php 图片缩略图的实现方法

程序员文章站 2022-04-01 21:45:35
...
功能:支持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