Transparent PNG over JPG in PHP

What seems simple is not: (

I am trying to add something like a watermark (transparent png) in the 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 combine the images, the png is located in the right place, each one above and to the left of it is good (jpg is copied), but everything else is black.

I tried setting imagesavealpha and imagealphablending to false, there was no difference.

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

I searched on the net, I can not find a solution.

Any help is appreciated.

+3
source share
2 answers

Your $width and $height should be the size of the watermark, not the photo. What you say, you need to copy a watermark with a much larger size than it is. When it reads a part of an image that does not exist (coordinates outside), the result is opaque black, giving the result you see.

+3
source

Use imagecopymerge () instead of imagecopy()

U may also like imagesavealpha ()

+1
source

Source: https://habr.com/ru/post/1398252/


All Articles