开发者

Copy an Image with Drop Shadow and Tranparent Background

开发者 https://www.devze.com 2023-03-22 11:54 出处:网络
I tried to duplicate a PNG image, which has drop shadow (i.e. alpha channel) and transparent background. However, the resulting image paints the shadow and transparent background with black. I tried w

I tried to duplicate a PNG image, which has drop shadow (i.e. alpha channel) and transparent background. However, the resulting image paints the shadow and transparent background with black. I tried with imagecopy and imagecopymerge; neither yielded to valid results, which aren't the same with the original image.

Preview of开发者_如何学JAVA the images.

$src = imagecreatefrompng('img_box3-bg.png');

/* Using imagecopy. */
$dest = imagecreatetruecolor(116, 100);
imagecopy($dest, $src, 0, 0, 0, 0, 116, 100);
imagepng($dest, 'img_box3-bg.imagecopy.png');
imagedestroy($dest);

/* Using imagecopymerge. */
$dest2 = imagecreatetruecolor(116, 100);
imagecopymerge($dest2, $src, 0, 0, 0, 0, 116, 100, 100);
imagepng($dest2, 'img_box3-bg.imagecopymerge.png');
imagedestroy($dest2);

imagedestroy($src);

Help? Thanks beforehand.


Something like this:

$src = imagecreatefrompng('img_box3-bg.png');

/* Using imagecopy. */
$dest = imagecreatetruecolor(116, 100);

// this is new
imagesavealpha($dest, true);
$transparent = imagecolorallocatealpha($dest, 0, 0, 0, 127);
imagefill($dest, 0, 0, $transparent);

imagecopy($dest, $src, 0, 0, 0, 0, 116, 100);

header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号