2

Im wondering if I'm doing something wrong, or if this is as good a result as im going to get. Both PNGs on the left are 95x111. The image of the robot has a 5px or so padding of transparent pixels around it, but it seems to be causing problems when they merge?

enter image description here

 $avatar = imagecreatefrompng("../guy.png");
 $borderImg = imagecreatefrompng("../frame.png");

 imagealphablending( $borderImg, false );
 imagesavealpha( $borderImg, true );

 imagecopyresampled($avatar,$borderImg,  0, 0, 0, 0, 95, 111,95, 111);
 imagepng($avatar, $newfilenameBig); 

Ive tried every combo of imagealphablending and imagesavealpha I can think of. When I set $avatar to imagesavealpa= true, then it doesnt even show the image as all, just the frame. Doesn't that seem strange? Is this as far as i'm gonna get using PHP GD?

UPDATE: The desired result can be achieved when both images are created manually in PS using 24 bit mode. Is there a way to do this using imagecopy or similar?

Dave Chenell
  • 601
  • 1
  • 8
  • 23
  • 1
    see my answer here: http://stackoverflow.com/questions/6496119/odd-transparency-effect-when-merging-two-pngs-with-transparency-in-php-gd/6496552#6496552 and check if it solves the problem – dev-null-dweller Sep 27 '11 at 19:56
  • Thanks for the help. So if I understood correctly, I re-saved frame.png in PS using save for web (png24) and still no luck. Does imagemagik support merging two PNG-24 or are there any other solutions? Right now Im setting the avatar to the BG of a div using css and putting the frame over top of it. – Dave Chenell Sep 27 '11 at 20:08
  • Did you try turning on alphablending and savealpha for BOTH images? Right now your sample only shows you doing it for the borderImg. – Marc B Sep 27 '11 at 20:11
  • Yes when I turn those on for the avatar then the resulting image is just the border which is strange because that should work. Does that mean the alpha channel of the avatar image is somehow corrupt? I do alot of GD stuff like scaling and cropping before hand to create that image. Also I just found that when I save that avatar.png to my HD the border shows up as black. Maybe I should revisit how I create that. – Dave Chenell Sep 27 '11 at 20:16
  • On a tangential note - neat art. – Andreas Eriksson Sep 27 '11 at 20:49
  • Thanks! I came up with the conclusion that it wasn't meant to be for the time being. It seems only photoshop can create these 24bit PNG's and trying to install imagemagik was one of the most painful things I've ever tried. Perhaps I will try again soon. – Dave Chenell Sep 27 '11 at 23:56

1 Answers1

0

Try the following code its working fine.


    $width = 95;
    $height = 111;

    $base_image = imagecreatefrompng("../guy.png");
    $top_image = imagecreatefrompng("../frame.png");

    imagesavealpha($top_image, false);
    imagealphablending($top_image, false);
    imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
    imagepng($base_image, "merged.png");

sugunan
  • 4,408
  • 6
  • 41
  • 66