Php generates image thumbnails and supports png transparency,
Note: This function depends on the GD2 graphics library.
I recently used php to generate thumbnails. I found this article on the Internet: generating image thumbnails in PHP.
After trying it out, I found several problems:
1. the thumbnails generated by png images are in jpg format.
2. the thumbnail generated by the png image does not have a transparent (translucent) effect (filled with a black background)
3. The code syntax is relatively old.
Therefore, based on this version, it is simply modified and optimized.
Thumbnails generated by PHP
<? Php/** desc: Resize Image (png, jpg, gif) * author: a decade later, brother Lu (http://www.cnblogs.com/lurenjiashuo/) * date: 2014.11.13 * base from: http://www.oschina.net/code/snippet_5189_2491 */class ResizeImage {// image type private $ type; // actual width private $ width; // actual height private $ height; // The width private $ resize_width after the change; // The height after the change is private $ resize_height; // whether to cut the image private $ cut; // The Source image private $ srcimg; // The target image address private $ dstim G; // temporarily created image private $ im; function _ construct ($ imgPath, $ width, $ height, $ isCut, $ savePath) {$ this-> srcimg = $ imgPath; $ this-> resize_width = $ width; $ this-> resize_height = $ height; $ this-> cut = $ isCut; // image type $ 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 = im Agesy ($ this-> im); // generate an image $ this-> newimg (); ImageDestroy ($ this-> im);} private function newimg () {// ratio of the changed image $ resize_ratio = ($ this-> resize_width)/($ this-> resize_height ); // ratio of the actual image $ ratio = ($ this-> width)/($ this-> height); if ($ this-> cut) {// cut image $ 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 priority imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, ($ this-> height) * $ resize_ratio), $ this-> height );} else {// width-first imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, $ this-> width, ($ this-> width)/$ resize_ratio);} else {// uncut graph 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_he Ight); 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);} // initialize the 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 target address: private function dst_img ($ dstpath) {$ full_length = strlen ($ this-> srcimg); $ type_length = strlen ($ this-> type ); $ name_length = $ full_length-$ typ E_length; $ name = substr ($ this-> srcimg, 0, $ name_length-1); $ this-> dstimg = $ dstpath ;}}?>
Use
You can directly call the class constructor when using it. The constructor is as follows:
$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);
Parameters
$ ImgPath: original image address
$ Width: thumbnail width
$ Height: thumbnail height
$ IsCut: whether to crop, bool Value
$ SavePath: thumbnail address (which can be the same as the original image address)
Example
<?php include "ResizeImage.php"; //jpg $jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg"); //png $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");?>
Effect