Solve the problem of black background when PHP clipping thumbnail generates png,gif transparent diagram

Source: Internet
Author: User
Solve the black background problem when PHP clipping thumbnails generate png,gif transparent graphs

Recently do the Avatar upload used to cut, as long as GIF or PNG is transparent, the cut will become a black background image.

?

There are 2 types of solutions:

?

1. Background map fills the white background.

?

$white = Imagecolorallocate ($dstim, 255,255,255); Imagefilledrectangle ($dstim, 0,0, $width, $height, $white); Imagecolortransparent ($dstim, $white);

?

2. Set the picture to follow the transparent channel.

?

$img = Imagecreatefrompng ($SRC); Imagesavealpha ($img, true);//important here; $thumb = Imagecreatetruecolor (300,300); 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; Imagecopyresampled ($thumb, $img, 0,0,0,0,300,300,300,300); Imagepng ($thumb, "temp.png");

?

All of the above 2 methods were tested successfully.

  • 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.