3

I've been bashing my head agains something simple..

// ....all prev code is fine.... 
$pasteboard =imagecreatetruecolor($imgs['bg']["width"],$imgs['bg']["height"]);
imagealphablending($pasteboard, false);
imagecopyresampled($pasteboard, $imgs['bg']["img"],0,0,0,0,$imgs['bg']["width"],$imgs['bg']["width"],imagesx($imgs['bg']["img"]),imagesy($imgs['bg']["img"]));
imagecopyresampled($pasteboard, $imgs['photo']["img"],20,20,0,0,$imgs['photo']["width"],$imgs['photo']["width"],imagesx($imgs['photo']["img"]),imagesy($imgs['photo']["img"]));
imagesavealpha($pasteboard,true);
//send it out
$out = $pasteboard;

header('Content-type: image/png');
imagepng($out);
//then garbage collection

gives me this:

enter image description here

HORAY!

perfect alpha png composite...

Now I want to rotate it, so instead of the $out=$pasteboard i do this:

imagesavealpha($pasteboard,true);
//rotate it
$out = imagerotate($pasteboard,5,imagecolorexactalpha($pasteboard,255,255,255,50),0);

header('Content-type: image/png');
imagepng($out);

which sadly gives me this:

enter image description here

BOOOO!

Ive tried setting the color like:

imagerotate($pasteboard,5,0x00000000,0);

also the last attr like:

imagerotate($pasteboard,5,0x00000000,1);

new empty images sampled etc etc...

no dice....

Can anyone help?

Alex
  • 3,732
  • 5
  • 37
  • 59

3 Answers3

5

I'm answering my question simply because I've tried 10-15 suggestions i've seen allover the web all of which offering 'nearly' right solutions but nothing exact, Also I've seen this question posted a few places now, and hopefully if anyone reaches this page in future it would be best to show the solution as the direct answer.

MASSIVE thanks to @cristobal for the help and efforts, if I could vote you up any more I would !

The knack seems to be:

//rotate it
$pasteboard = imagerotate($pasteboard,5,0XFFFFFF00,0); //<-- here must be RRGGBBAA, also last attr set to 0
imagesavealpha($pasteboard, true); // <-- then to save it... dont ask me why..
//send it out
header('Content-type: image/png');
imagepng($pasteboard);

produces this (it has a perfect alpha even though you cant see against the white page):

enter image description here

REALLY not the most fun 5 hrs of my life... hopefully it will stop someone else going through the same pain..

Alex
  • 3,732
  • 5
  • 37
  • 59
2

Using the same code above and using a blue color for the third parameter in the imagerotate operation, which will be it used to fill the uncovered zone after the rotation i.e.:

imagerotate($pasteboard, 5, 255);

We get the following image

Rotated using blue as third parameter

we see the blue area is the uncovered zone which it fills, while the black color is the to be the border shadow from the image which GD does not seem to handle well along the interpolation used in the rotation.

The same image rotated using the convert for imagemagick. commmand i.e. $> convert -rotate 5 image.png image_rotated.png results in the image below

rotated using imagemagick

Clearly GD does not handle alpha colors well when rotating.

If you have access to use the convert commmand using exec or process, you should pipe those image operation to imagemagick instead. GD is a simple image library which has not been updated much the latest years. Otherwise try Imagemagick, Cairo or Gmagick which there are pecl plugins for too http://php.net/manual/en/book.image.php.

Last resort somebody made a function that which uses GD http://www.exorithm.com/algorithm/view/rotate_image_alpha for what you are looking after but the result is not pretty since its a simple linear interpolation:

linear interpolation

taken from How to rotate an image in GD Image Library while keeping transparency?. Perhaps if you convert the linear interpolation function to a Bicubic or Quad it will look better.

Community
  • 1
  • 1
cristobal
  • 462
  • 5
  • 14
0

Note these answers did not work for me but this did.

$destimg = imagecreatefromjpeg("image.png");
$rotatedImage = imagerotate($destimg, 200, 0);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");