Before the web development note wrote a PHP generated thumbnail function, although that function can generate thumbnails, but there is a certain flaw in the production of PNG thumbnail, the background is black, and today wrote a function to make up for it. The code is simple, imagealphablending ($thumb, False), and Imagesavealpha ($thumb, true); It is important to save the alpha value of PNG, not to lose it.
The functions are as follows:
? PHP
*
* $sourePic: Original path
* $smallFileName: Small map name
* $width: Small map width
* $heigh: Small figure High
* Reproduced annotated www.chhua.com */
function Pngthumb ($sourePic, $smallFileName, $width, $heigh) {
$image =imagecreatefrompng ($sourePic); PNG
Imagesavealpha ($image, true);//It's very important that you do not lose the transparent color of the $sourepic image;
$BigWidth =imagesx ($image)//Large picture width
$BigHeigh =imagesy ($image),//Large picture height
$thumb = imagecreatetruecolor ($width , $heigh);
Imagealphablending ($thumb, false);//Here is important, meaning does not merge the color, directly replaces with the $IMG image color, including the transparent color;
Imagesavealpha ($thumb, true);//Here is important, meaning do not lose the transparent color of the $thumb image;
if (imagecopyresampled ($thumb, $image, 0,0,0,0, $width, $heigh, $BigWidth, $BigHeigh)) {
imagepng ($thumb, $ Smallfilename);}
return $smallFileName//returns small map path reprint annotated www.chhua.com
}
pngthumb ("A.png", "C.png", 300, 300);//Call
?>