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.