Unusual result when merging two transparent images in php
Asked Answered
B

1

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?

Bethea answered 27/9, 2011 at 19:49 Comment(6)
see my answer here: #6496619 and check if it solves the problemHathorn
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.Bethea
Did you try turning on alphablending and savealpha for BOTH images? Right now your sample only shows you doing it for the borderImg.Torch
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.Bethea
On a tangential note - neat art.Rachellrachelle
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.Bethea
C
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");

Campney answered 23/3, 2012 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.