Solve the problem of black background when generating png and gif transparent images by cutting thumbnails in PHP

Source: Internet
Author: User
Solve the problem of black background when generating png images by cropping thumbnails in PHP. when a gif image is transparent, the image is cut recently. as long as the GIF or PNG image is transparent, it will become a black background image after cutting .? There are two solutions :? 1. fill the background image with a white background .? $ White = imagecolorallocate ($ dstim, 255,255, solves the black background problem when generating png and gif transparent images by cropping thumbnails in PHP.

Recently, you have used cropping to upload an avatar. as long as the GIF or PNG image is transparent, the cropped image will become a black background image.

?

There are two solutions:

?

1. fill the background image with a white background.

?

$ White = imagecolorallocate ($ dstim, 255,255,255); imagefilledrectangle ($ dstim, 0,0, $ width, $ height, $ white); imagecolortransparent ($ dstim, $ white );

?

2. set the image to a transparent channel.

?

$ Img = imagecreatefrompng ($ src); imagesavealpha ($ img, true); // important here; $ thumb = imagecreatetruecolor (300,300); imagealphablending ($ thumb, false ); // This is very important. it means that the $ img Image color is replaced directly without merging colors, including transparent colors; imagesavealpha ($ thumb, true); // It is very important here, do not lose the transparent color of $ thumb image; imagecopyresampled ($ thumb, $ img, 300,300,300,300,); imagepng ($ thumb, "temp.png ");

?

Both methods are successfully tested.

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.