PHP implementation to generate transparent background of PNG thumbnail function sharing _php instance

Source: Internet
Author: User

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
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.