How PHP generates thumbnail images with transparent png

Source: Internet
Author: User
Tags php tutorial
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.1 3 * Base from:http://www.oschina.net/code/snippet_5189_2491*/classResizeimage {//type of picturePrivate$type; //Actual widthPrivate$width; //actual heightPrivate$height; //the width after the changePrivate$resize _width; //the height after the changePrivate$resize _height; //whether or not to cut the mapPrivate$cut; //Source ImagePrivate$srcimg; //Destination Image AddressPrivate$dstimg; //temporarily created imagesPrivate$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); } Privatefunctionnewimg () {//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 mapif($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 imagePrivatefunctioniniti_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 AddressPrivatefunctionDst_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

 
  include "resizeimage.php"//jpg$jpgResizenewfalse, "img/test_320 _240.jpg "//png$pngResizenewfalse," Img/test_ 320_240.png "?>

Effect

The above describes how PHP generates a transparent PNG image thumbnail, including PHP generated thumbnail content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.