Home >Backend Development >PHP Tutorial >PHP generates thumbnail classes that support custom height and width. Also supports taking screenshots according to height and width_PHP tutorial
php tutorial generates thumbnail class, supports custom height and width. Also supports taking screenshots according to height and width
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")
// Plans 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");