php imagerotate() ruins alpha on png?

爱⌒轻易说出口 提交于 2020-01-04 04:28:05

问题


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:

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:

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?


回答1:


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):

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




回答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

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

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:

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.




回答3:


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");


来源:https://stackoverflow.com/questions/12082472/php-imagerotate-ruins-alpha-on-png

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!