Note: This feature relies on the GD2 graphics library
recently to use PHP to generate thumbnails, find a bit online, found this article: PHP Generate thumbnail image
After a trial, we found that there were several problems:
1. PNG images generated thumbnails are in JPG format
2. PNG images generated thumbnails without transparent (translucent) effect (filled with black background)
3, the code syntax is relatively old
So, on the basis of this version of the simple modification is optimized a bit.
PHP generated thumbnail class
<?PHP/** desc:resize Image (PNG, JPG, GIF) * Author: Ten years later Brother Lou (http://www.cnblogs.com/lurenjiashuo/) * date:2014. 11.13 * Base from:http://www.oschina.net/code/snippet_5189_2491*/ classResizeimage {//type of picture Private $type; //Actual width Private $width; //actual height Private $height; //the width after the change Private $resize _width; //the height after the change Private $resize _height; //whether or not to cut the map Private $cut; //Source Image Private $srcimg; //Destination Image Address Private $dstimg; //temporarily created images Private $im; function__construct ($imgPath,$width,$height,$isCut,$savePath) { $this->srcimg =$imgPath; $this->resize_width =$width; $this->resize_height =$height; $this->cut =$isCut; //type of picture $this->type =Strtolower(substr(STRRCHR($this->srcimg, "."), 1)); //Initializing an image $this-initi_img (); //Destination Image Address $this-Dst_img ($savePath); //-- $this->width = Imagesx ($this-im); $this->height = Imagesy ($this-im); //Creating Images $this-newimg (); Imagedestroy ($this-im); } Private functionnewimg () {//The proportions of the altered image $resize _ratio= ($this->resize_width)/($this-resize_height); //ratio of actual images $ratio= ($this->width)/($this-height); if($this-cut) { //Crop Chart $newimg= Imagecreatetruecolor ($this->resize_width,$this-resize_height); if($this->type== "PNG") {Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127)); } if($ratio>=$resize _ratio) { //High PriorityImagecopyresampled ($newimg,$this->im, 0, 0, 0, 0,$this->resize_width,$this->resize_height, (($this->height) *$resize _ratio),$this-height); } Else { //Width FirstImagecopyresampled ($newimg,$this->im, 0, 0, 0, 0,$this->resize_width,$this->resize_height,$this->width, (($this->width)/$resize _ratio)); } } Else { //do not cut the map if($ratio>=$resize _ratio) { $newimg= Imagecreatetruecolor ($this->resize_width, ($this->resize_width)/$ratio); if($this->type== "PNG") {Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127)); } imagecopyresampled ($newimg,$this->im, 0, 0, 0, 0,$this->resize_width, ($this->resize_width)/$ratio,$this->width,$this-height); } Else { $newimg= Imagecreatetruecolor (($this->resize_height) *$ratio,$this-resize_height); if($this->type== "PNG") {Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127)); } imagecopyresampled ($newimg,$this->im, 0, 0, 0, 0, ($this->resize_height) *$ratio,$this->resize_height,$this->width,$this-height); } } if($this->type== "PNG") {Imagesavealpha ($newimg,true); Imagepng ($newimg,$this-dstimg); } Else{imagejpeg ($newimg,$this-dstimg); } } //Initializing an image Private functioniniti_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 Private functionDst_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; } }?>
Use
When used, the constructor of the class is called directly, and the constructor is as follows:
$resizeimage new resizeimage ($imgPath, $width, $height, $isCut, $savePath);
Parameters
$imgPath: Original image Address
$width: Thumbnail width
$height: High thumbnail image
$isCut: Whether clipping, bool value
$savePath: Thumbnail address (can be the same as the original image address)
Example
<? PHP include "resizeimage.php"; // jpg $jpgResize New false, "img/test_320_240.jpg"); // PNG $pngResize New false, "Img/test_320_240.png");? >
Effect
PHP generate image thumbnails, PNG transparent support