This article describes how to generate a png thumbnail program with a transparent background in php. to process this in php, we only need to use imagealphablending ($ thumb, false); and imagesavealpha ($ thumb, true); then, let's look at the program.
When a PNG thumbnail is generated, the background is black. Today I wrote another function to make up for it. The code is very simple, that is, imagealphablending ($ thumb, false); and imagesavealpha ($ thumb, true); it is very important to save the alpha value of PNG and do not lose it.
| The Code is as follows: |
Copy code |
<? PHP
/*
* $ SourePic: source image path
* $ SmallFileName: Specifies the image name.
* $ Width: Specifies the width of a thumbnail.
* $ Heigh: Small Image Height
* Reprinted with www.hzhuti.com */
Function pngthumb ($ sourePic, $ smallFileName, $ width, $ heigh ){
$ Image = imagecreatefrompng ($ sourePic); // PNG
Imagesavealpha ($ image, true); // It is important that you do not lose the transparent color of $ sourePic;
$ BigWidth = imagesx ($ image); // The width of a large image.
$ BigHeigh = imagesy ($ image); // the height of a large image.
$ Thumb = imagecreatetruecolor ($ width, $ heigh );
Imagealphablending ($ thumb, false); // It is very important here, meaning that the color is not merged and is directly replaced with the $ img image color, including the transparent color;
Imagesavealpha ($ thumb, true); // This is very important, meaning do not lose the transparent color of the $ thumb image;
If (imagecopyresampled ($ thumb, $ image, $ width, $ heigh, $ BigWidth, $ BigHeigh )){
Imagepng ($ thumb, $ smallFileName );}
Return $ smallFileName; // return the thumbnail path}
Pngthumb ("a.png", "c.png", 300,300); // call
?> |
All of the above are built-in functions in php, and no third-party programs are used. If you need them, you can check them out.