PHP generated thumbnails: PHP to generate smart thumbnails
Last Update:2017-02-28
Source: Internet
Author: User
<?php
Class Resizeimage {
Picture type
var $type;
Actual width
var $width;
Actual height
var $height;
The width after the change
var $resize _width;
Change the height of the post
var $resize _height;
Whether or not to cut the map
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 picture
$this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1);
Initializing images
$this->initi_img ();
Target Image Address
$this->dst_img ($dstpath);
$this->width = imagesx ($this->im);
$this->height = Imagesy ($this->im);
Build image
$this->newimg ();
Imagedestroy ($this->im);
}
function newimg () {
The proportions of the altered image
$resize _ratio = ($this->resize_width)/($this->resize_height);
The proportions of the actual image
$ratio = ($this->width)/($this->height);
if (($this->cut) = = "1")//Map
{
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 map
{
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-& Gt;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);
}
}
}
Initializing images
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);
}
}
Image Destination Address
function Dst_img ($dstpath) {This article links http://www.cxybl.com/html/wlbc/Php/20130326/37402.html