Home  >  Article  >  Backend Development  >  Generate thumbnail image class in php_PHP tutorial

Generate thumbnail image class in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:46:01832browse

Generating thumbnails is when we want to upload images. If the image is larger, we need to generate a small image of a specified size to display on the list page. This can save resources and is also a common practice now. Let me share a php generated Thumbnail class, supports custom height and width. Also supports taking screenshots according to height and width

The code is as follows Copy code

class resizeimage
{
//Image type
var $type;
//Actual width
var $width;
//Actual height
var $height;
//Changed width
var $resize_width;
//Height after change
var $resize_height;
//Whether to crop the image
var $cut;
//Source image
var $srcimg;
//Target image address
var $dstimg;
//Temporarily created image
var $im;
Function resizeimage($img, $wid, $hei,$c,$dstpath)

           $this->srcimg = $img;                                           $this->resize_width = $wid;                                              $this->resize_height = $hei;                                                  $this->cut = $c;                                 //Type of image

$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
//Initialize image
           $this->initi_img();                              //Target image address
          $this -> dst_img($dstpath);
           //--                                                                $this->width = imagesx($this->im); 
           $this->height = imagesy($this->im); 
//Generate image
$this->newimg();
ImageDestroy ($this->im);
}  
Function newimg()

//The proportion of the changed image
         $resize_ratio = ($this->resize_width)/($this->resize_height);
//Proportion of actual image
         $ratio = ($this->width)/($this->height);
If(($this->cut)=="1")
// Creed                                                                                    If($ratio>=$resize_ratio)
//High priority
                                                                     $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);
                                                                                                                                If($ratio<$resize_ratio)
//Width first
                                                                     $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);
                                                                                                                                                                                                                                                      else
//No cropping
                                                                                   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);
                                                                                                                                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); 
            } 
        } 
    } 
    //初始化图象 
    function initi_img() 
    { 
        if($this->type=="jpg") 
        { 
            $this->im = imagecreatefromjpeg($this->srcimg); 
        } 
        if($this->type=="gif") 
        { 
            $this->im = imagecreatefromgif($this->srcimg); 
        } 
        if($this->type=="png") 
        { 
            $this->im = imagecreatefrompng($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; 
    } 

$resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg"); 
?>

实例二

 代码如下 复制代码

/**
* Generate thumbnail
* @author yangzhiguo0903@163.com
* @param string The absolute complete address of the source image {with file name and suffix}
* @param string The absolute complete address of the target image {with file name and suffix}
* @param int Thumbnail width {0: At this time, the target height cannot be 0, and the target width is the source image width * (target height/source image height)}
* @param int Thumbnail height {0: At this time, the target width cannot be 0, and the target height is the source image height * (target width/source image width)}
* @param int Whether to crop {width and height must be non-0}
* @param int/float Scale {0: No scaling, 0 * @return boolean
​*/
function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
{
    if(!is_file($src_img))
    {
        return false;
    }
    $ot = fileext($dst_img);
    $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
    $srcinfo = getimagesize($src_img);
    $src_w = $srcinfo[0];
    $src_h = $srcinfo[1];
    $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
    $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

    $dst_h = $height;
    $dst_w = $width;
    $x = $y = 0;

    /**
* * The thumbnail should not exceed the size of the source image (provided there is only one width or height)
​​*/
    if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
    {
        $proportion = 1;
    }
    if($width> $src_w)
    {
        $dst_w = $width = $src_w;
    }
    if($height> $src_h)
    {
        $dst_h = $height = $src_h;
    }

    if(!$width && !$height && !$proportion)
    {
        return false;
    }
    if(!$proportion)
    {
        if($cut == 0)
        {
            if($dst_w && $dst_h)
            {
                if($dst_w/$src_w> $dst_h/$src_h)
                {
                    $dst_w = $src_w * ($dst_h / $src_h);
                    $x = 0 - ($dst_w - $width) / 2;
                }
                else
                {
                    $dst_h = $src_h * ($dst_w / $src_w);
                    $y = 0 - ($dst_h - $height) / 2;
                }
            }
            else if($dst_w xor $dst_h)
            {
                if($dst_w && !$dst_h)  //有宽无高
                {
                    $propor = $dst_w / $src_w;
                    $height = $dst_h  = $src_h * $propor;
                }
                else if(!$dst_w && $dst_h)  //有高无宽
                {
                    $propor = $dst_h / $src_h;
                    $width  = $dst_w = $src_w * $propor;
                }
            }
        }
        else
        {
            if(!$dst_h)  //裁剪时无高
            {
                $height = $dst_h = $dst_w;
            }
            if(!$dst_w)  //裁剪时无宽
            {
                $width = $dst_w = $dst_h;
            }
            $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
            $dst_w = (int)round($src_w * $propor);
            $dst_h = (int)round($src_h * $propor);
            $x = ($width - $dst_w) / 2;
            $y = ($height - $dst_h) / 2;
        }
    }
    else
    {
        $proportion = min($proportion, 1);
        $height = $dst_h = $src_h * $proportion;
        $width  = $dst_w = $src_w * $proportion;
    }

    $src = $createfun($src_img);
    $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
    $white = imagecolorallocate($dst, 255, 255, 255);
    imagefill($dst, 0, 0, $white);

    if(function_exists('imagecopyresampled'))
    {
        imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    else
    {
        imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    }
    $otfunc($dst, $dst_img);
    imagedestroy($dst);
    imagedestroy($src);
    return true;
}

使用方法

 代码如下
 代码如下 复制代码

$src_img = "./ROSI_050_002.JPG";
$dst_img = "./ROSI_050_002_thumb.jpg";
$stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);
if($stat){
 echo 'Resize Image Success!
';
 echo ''; 
}else{
 echo 'Resize Image Fail!'; 
}

复制代码
$src_img = "./ROSI_050_002.JPG"; $dst_img = "./ROSI_050_002_thumb.jpg";

$stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);

if($stat){  echo '';  }else{  echo 'Resize Image Fail!';  }
http://www.bkjia.com/PHPjc/632963.html
www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632963.htmlTechArticle生成缩略图是当我们要上传图片时如果图片较大我们需要生成一张指定大小的小图以在列表页面显示,这样可节省资源也是现在常用的做法...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn