Transparent PNG over JPG in PHP

核能气质少年 提交于 2019-12-29 08:52:09

问题


What seems to be simple, isn't :(

I'm trying to add something like a watermark (transparent png) on an image (jpg). This is the code I'm using:

$width = 800; 
$height = 600; 
$bottom_image = imagecreatefromjpeg("portrait1.jpg"); 
$top_image = imagecreatefrompng("man2.png"); 
imagesavealpha($top_image, true); 
imagealphablending($top_image, true); 
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height); 
header('Content-type: image/png');
imagepng($bottom_image);

When I merge the images, the png is positioned at the right place, everythig above and left of it is good (jpg is copied), but everything else is black.

I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference.

You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php

I've searched around the net, I can't find a solution.

Any help is appreciated.


回答1:


Your $width and $height should be the dimensions of the watermark, not of the photo. What you're telling it to do is copy the watermark with a much bigger size than it is. When it reads part of an image that doesn't exist (coordinates out of bounds) the result is opaque black, giving the result you see.




回答2:


Use imagecopymerge() instead of imagecopy()

U may also like imagesavealpha()



来源:https://stackoverflow.com/questions/7985583/transparent-png-over-jpg-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!