0

So I created a script from PHP Doc and some helpful threads/advice on SO and came out with this:

$filename = time();

$glassurl = $_GET['GlassImg'];
$frameurl = $_GET['FrameImg'];
$handleurl = "images/helpbutton.png";
$handleurl2 = "images/helpbutton.png";

$glassimg = imagecreatefromjpeg($glassurl);
$frameimg = imagecreatefromgif($frameurl);
$handleimg = imagecreatefrompng($handleurl);
$handleimg2 = imagecreatefrompng($handleurl2);

$frame_x = imagesx($frameimg);
$frame_y = imagesy($frameimg);
imagecopymerge($glassimg, $frameimg, 0, 0, 0, 0, $frame_x, $frame_y, 100);

imagecolortransparent($handleimg, imagecolorat($handleimg, 0, 0));
imagecolortransparent($handleimg2, imagecolorat($handleimg2, 0, 0));

$handle_x = imagesx($handleimg);
$handle_y = imagesy($handleimg);
imagecopymerge($glassimg, $handleimg, 460, 150, 0, 0, $handle_x, $handle_y, 100);


$handle2_x = imagesx($handleimg2);
$handle2_y = imagesy($handleimg2);
imagecopymerge($glassimg, $handleimg2, 5, 5, 0, 0, $handle2_x, $handle2_y, 100);

// Output and free from memory
imagepng($glassimg, "uploads/$filename.png");

imagedestroy($glassimg);
imagedestroy($frameimg);
imagedestroy($handleimg);
imagedestroy($handleimg2);

It pretty much works perfectly, however there is a slight issue. The helpbutton.png which is just a placeholder image to see that i can position images and merge them ends up with a solid black drop shadow that causes the image to look horrible.

This is what it should look like:

helpbutton.png

This is how it comes out once merged:

helpresult.png

I have looked into a lot of the image controls including blending and such, but none seem to affect it. Does anyone know how to control that drop shadow shouldn't be darkened in PHP?

hakre
  • 193,403
  • 52
  • 435
  • 836
Bohdi
  • 1,295
  • 4
  • 28
  • 62
  • it might be because you are uploading the image which is not a png image try uploading a png image – NullPoiиteя Oct 22 '12 at 12:35
  • which image is not a png? both the help buttons and the final image produced are. You mean the gif and the jpeg? – Bohdi Oct 22 '12 at 12:37
  • Also you have not added the original images to your post. Adding those would better explain your input which might be part of your issue. – hakre Oct 22 '12 at 12:40
  • I think it has to do with the imagecopymerge, have a look at this: http://stackoverflow.com/questions/3355993/phpgd-imagecopymerge-not-retaining-png-transparencies – Rick Calder Oct 22 '12 at 12:42
  • Tried using two pngs instead, had no affect. So resorted back to jpeg/gif. looking into hakres comment now. – Bohdi Oct 22 '12 at 13:25

1 Answers1

0

imagesavealpha($res, true) & imagealphablending($res, false) comes in handy, when you save png images with alpha channel (especially, when you opened from jpeg).

Edit: try this:

$glassimg = imagecreatefromjpeg($glassurl);
imagealphablending($glassimg, false);
imagesavealpha($glassimg, true)

$frameimg = imagecreatefromgif($frameurl);
imagealphablending($frameimg, false);
imagesavealpha($frameimg, true)

$handleimg = imagecreatefrompng($handleurl);
imagealphablending($handleimg, false);
imagesavealpha($handleimg, true)

$handleimg2 = imagecreatefrompng($handleurl2);
imagealphablending($handleimg2, false);
imagesavealpha($handleimg2, true)
pozs
  • 34,608
  • 5
  • 57
  • 63
  • I've tried this, and it made no difference... thank you though :) – Bohdi Oct 22 '12 at 12:51
  • did you applied on every image resource (just after opened) which are in `imagecopy` / `imagecopymerge` arguments, too? – pozs Oct 22 '12 at 12:55
  • I have tried this.. and unfortunately it still has a black drop shadow :/ seems to have no effect at all for some reason. thank you very much for the edit though. – Bohdi Oct 22 '12 at 13:22
  • Then, it will be you image files - i think. You should use images already with alpha channels saved, not manually set with `imagecolortransparent`. – pozs Oct 22 '12 at 13:25
  • But as I understand it, jpegs are the only image type that don't have alpha channels. I used a png instead of a jpeg, and the same issue occurs. And seeing as im saving the alpha setting (theoretically) it should work? – Bohdi Oct 22 '12 at 14:51
  • Yes, its not enough to convert it to png, when you save it with alpha channel, you'll see, these dots (that not transparent in your example) is not exactly the same color, you set to be the transparent color in php. – pozs Oct 22 '12 at 14:54