PHP implementation generate transparent background PNG thumbnail function share, PNG thumbnail _php tutorial

Source: Internet
Author: User
Tags transparent color

PHP implementation to generate transparent background PNG thumbnail function share, PNG thumbnail


Before the web development note wrote a PHP generated thumbnail function, although the function can generate thumbnails, but there are some flaws, in the generation of PNG thumbnails, 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 Image Width * $heigh: Small Image Height * reprinted annotated Www.chhua.com*/function Pngthumb ($sourePic, $ Smallfilename, $width, $heigh) {$image =imagecreatefrompng ($sourePic);//png Imagesavealpha ($image, true);//It's important here. It means not to lose the transparent color of the $sourepic image; $BigWidth =imagesx ($image);//Large Image width $bigheigh=imagesy ($image);//large Image Height $thumb = imagecreatetruecolor ($width, $heigh); Imagealphablending ($thumb, false);//This is important, meaning that you do not merge the colors directly with the $img image color, including the transparent color; Imagesavealpha ($thumb, true);//It's important here. It means not to 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 the small map path reprinted annotated Www.chhua.com} pngthumb ("A.png", "C.png", 300, 300);//Call?>


How transparent is the background for generating PNG thumbnails? -PHP Advanced Discussion

Do it with Photoshop, pull out the need and copy one layer, then delete the background layer and save as PNG format.

With the following PHP thumbnail function, for some JPG images can not generate thumbnails, and others can, this is why?

Attention

1: Should be judged after the good picture ready for the output or save the method of use Imagecreatefromjpeg

2:imagesy ($src _img), consider using list ($width, $height) = getimagesize ($src _img);

3: $ratio _h=1.0 * $new _height/$h//n= unknown, 1 times any n, the result is still N, why do this?

4: Your program is to receive external input dimensions, which causes the output or saved picture to distort, deform

5: $ratio = 1.0;
if ($ratio _w <=1 && $ratio _h <= 1) {
if ($ratio _w < $ratio _h) {}else {}//Note 4: Consider using the ternary algorithm

6: $inter _h= (int) ($new _height/$ratio);//php is not as sensitive to data types as Java and does not have to be forced to convert with int

7: After processing good remember to destroy the picture Imagedestroy ();

Finally: You can explain the effect you want

http://www.bkjia.com/PHPjc/840751.html www.bkjia.com true http://www.bkjia.com/PHPjc/840751.html techarticle PHP implementation to generate a transparent background png thumbnail function share, PNG thumbnail before the web development note wrote a PHP generated thumbnail function, although that function can generate thumbnails, but ...

  • Related Article

    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.