This article mainly introduces the PNG thumbnail function sharing for PHP to generate transparent background. if you need it, refer to the function used to generate a thumbnail in PHP in the WEB development notes, although the function can generate thumbnails, it has some drawbacks. 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 function is as follows:
<? PHP/** $ sourePic: source image path * $ smallFileName: thumbnail name * $ width: thumbnail width * $ heigh: Thumbnail height * Reprinted with www.chhua.com */function pngthumb ($ sourePic, $ smallFileName, $ width, $ heigh) {$ image = imagecreatefrompng ($ sourePic); // PNG imagesavealpha ($ image, true ); // It is very important that you do not lose the transparent color of $ sourePic images; $ BigWidth = imagesx ($ image); // The width of the larger image $ BigHeigh = imagesy ($ image ); // Large image height $ thumb = imagecreatetruecolor ($ width, $ heigh); imagealphablending ($ thumb, false); // it is heavy here Yes, it means that the color is not merged, and $ img Image color is replaced directly, including transparent color; imagesavealpha ($ thumb, true); // It is important here, do not lose the transparent color of $ thumb image; if (imagecopyresampled ($ thumb, $ image, $ width, $ heigh, $ BigWidth, $ BigHeigh )) {imagepng ($ thumb, $ smallFileName);} return $ smallFileName; // return the thumbnail path, indicating www.chhua.com} pngthumb ("a.png", "c.png", 300,300 ); // call?>