phpimage-processinggdgdlibgd2

Strange result of making one picture from to on PHP


In my project, I need to merge two pictures.

The first (img.png):

img http://uoops.ru/1/img.png

And (for example) second (photo.png):

img http://uoops.ru/1/photo.png

This is PHP code:

$photoImage = ImageCreateFromPNG("img.png");
ImageAlphaBlending($photoImage, true);

$logoImage = ImageCreateFromPNG("photo.png");
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
ImageCopy($photoImage, $logoImage, 1, 1, 0, 0, $logoW, $logoH);

ImagePNG($photoImage, "mrkr.png", 0);

As result I want to have this:

http://uoops.ru/1/result.png

But I have this:

http://uoops.ru/1/mrkr.png

How can I fix this?


Solution

  • $photoImage = ImageCreateFromPNG("img.png");
    
    $w = imagesx($photoImage);
    $h = imagesy($photoImage);
    $out = imagecreatetruecolor($w, $h);
    ImageAlphaBlending($out, true);
    imagefill($out, 0, 0, imagecolorallocatealpha($out, 0, 0, 0, 127));
    imagesavealpha($out, true);
    
    ImageCopy($out, $photoImage, 0, 0, 0, 0, $w, $h);
    
    $logoImage = ImageCreateFromPNG("photo.png");
    $logoW = ImageSX($logoImage);
    $logoH = ImageSY($logoImage);
    ImageCopy($out, $logoImage, 1, 1, 0, 0, $logoW, $logoH);
    ImagePNG($out, "mrkr.png", 0);
    

    this is not really optimized but should work