11

Does anybody have a script which can merge two PNG images?

With the following conditions:

  • Both images have transparent areas
  • The second image must have 50% opacity (it is overlaid over the first image)

Here is what I tried to do but without luck:

<?php

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    $cut = imagecreatetruecolor($src_w, $src_h);
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
}

$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150

$merged_image = imagecreatetruecolor(300, 300);
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

imagecopy($merged_image, $image1, 0, 0, 0, 0, 300, 300);
imagecopymerge_alpha($merged_image, $image2, 0, 0, 0, 0, 150, 150, 50);

header('Content-Type: image/png');
imagepng($merged_image);

?>

Edit:

  • First Image (left) and Second Image (right)

enter image description here enter image description here

  • This is how it should be (left) and the result of my code (right)

enter image description here enter image description here

  • The result of the solution proposed by dqhendricks

enter image description here

acoder
  • 513
  • 2
  • 10
  • 20
  • 2
    Explain *but without luck*. What does the code you posted produce? – lorenzo-s Feb 20 '12 at 18:14
  • 2
    Welcome to Stack Overflow! "It doesn't work" is *never* a good error description. Please describe what goes wrong, what error messages you get, etc. – Pekka Feb 20 '12 at 18:21
  • The first image looks transparent, the background of the second image - which should be transparent - becomes black (a semi-transparent black). – acoder Feb 20 '12 at 18:28
  • welcome. you're new so you might not know yet, but this is not a site to ask where to "get a script". it is used for figuring out programming problems. – dqhendricks Feb 20 '12 at 20:26

2 Answers2

10
$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150
imagecopymerge($image1, $image2, 0, 0, 75, 75, 150, 150, 50);

this should be all you need. $image1 should hold the merged image where image2 has been overlayed with 50% opacity. the last argument is the alpha of the merged copy.

http://php.net/manual/en/function.imagecopymerge.php

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • As per your suggestion i used imagecopymerge($image1, $image2, 0, 0, 0, 0, 150, 150, 50); i posted the result image in Issue Description. – acoder Feb 20 '12 at 22:09
  • can you run http://www.php.net/manual/en/function.imageistruecolor.php on both image1 and image 2 to verify that they are loaded as truecolor images? – dqhendricks Feb 20 '12 at 22:54
  • PHP Version 5.2.17 GD Version bundled (2.0.34 compatible) – acoder Feb 20 '12 at 23:08
  • Yes, the result of imageistruecolor is 1 for both image1 and image2. – acoder Feb 20 '12 at 23:10
  • 1
    @acoder apparently there is an open bug for this: https://bugs.php.net/bug.php?id=23815 – dqhendricks Feb 21 '12 at 05:51
  • @acoder I found a way to do this the other day. you need to set alpha blending and save alpha on the $cut image before doing the copies. – dqhendricks Mar 25 '12 at 23:19
2
$merged_image = imagecreatetruecolor(300, 300);
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

imagecopy($merged_image, $image1, 0, 0, 0, 0, 300, 300);
// after first time of "imagecopy" change "imagealphablending"
imagealphablending($merged_image, **true**);

imagecopy($merged_image, $image2, 0, 0, 0, 0, 300, 300);
Mike
  • 21
  • 1