Support PNG transparent image of PHP generated thumbnail class sharing,
Note: This feature relies on the GD2 graphics library
Recently to use PHP to generate thumbnails, find a bit online, found this article: PHP generated 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 * date:2014.11.13 * * class Resizeimage {/ /Picture type private $type; Actual width private $width; Actual height private $height; Change the width of the private $resize _width; The height of the change after the private $resize _height; Whether the drawing private $cut; source image private $srcimg; The target image address is private $dstimg; Temporary image created by private $im; function __construct ($imgPath, $width, $height, $isCut, $savePath) {$this->srcimg = $imgPath; $this->resize_width = $width; $this->resize_height = $height; $this->cut = $isCut; The type of picture $this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1); Initialize the image $this->initi_img (); Target image Address $this, dst_img ($savePath); --$this->width = Imagesx ($this->im); $this->height = Imagesy ($this->im); Generate Image $this->newimg (); Imagedestroy ($this->im); } Private Function Newimg (){//changes in image proportions $resize _ratio = ($this->resize_width)/($this->resize_height); The ratio of actual images $ratio = ($this->width)/($this->height); if ($this->cut) {//Crop $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 precedence imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $ This->resize_width, $this->resize_height, (($this->height) * $resize _ratio), $this->height); } else {//Width precedence imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this- Resize_height, $this->width, (($this->width)/$resize _ratio)); }} else {//Do not map if ($ratio >= $resize _ratio) {$newimg = Imagecreatetruecolor ($this->resiz E_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_heigh T, $this->width, $this->height); }} if ($this->type== "png") {Imagesavealpha ($newimg, true); Imagepng ($newimg, $this->dstimg); } else {imagejpeg ($newimg, $this->dstimg); }}//Initialize image Private 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 private 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; }}?>
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 Resizeimage ("img/test_1920_1200.jpg", [+], [+], False, "img/test_320_240.jpg"); PNG $pngResize = new Resizeimage ("Img/test_1024_746.png", "img/test_320_240.png"); >
Effect
http://www.bkjia.com/PHPjc/955404.html www.bkjia.com true http://www.bkjia.com/PHPjc/955404.html techarticle support PNG transparent image of PHP generated thumbnail class share, note: This feature relies on GD2 graphics library recently to use PHP to generate thumbnails, found on the internet, discovered this article: PHP generation diagram ...