Png thumbnail program for generating transparent background in php

Source: Internet
Author: User
Tags transparent color

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.

 

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.