我一直在为一些简单的事情苦苦思索..
// ....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给了我这个:

万岁!
完美的阿尔法png合成物。
现在我想旋转它,所以我不使用$out=$pasteboard,而是这样做:
imagesavealpha($pasteboard,true);
//rotate it
$out = imagerotate($pasteboard,5,imagecolorexactalpha($pasteboard,255,255,255,50),0);
header('Content-type: image/png');
imagepng($out);不幸的是,我得到了这样的结果:

嘘!
我试过这样设置颜色:
imagerotate($pasteboard,5,0x00000000,0);最后一个属性如下:
imagerotate($pasteboard,5,0x00000000,1);新的空图像采样等...
没有骰子..。
有人能帮上忙吗?
发布于 2012-08-23 10:46:59
我之所以回答我的问题,只是因为我在网上尝试了10-15个建议,所有这些建议都提供了“近乎正确”的解决方案,但没有确切的答案,而且我现在已经在几个地方看到了这个问题,希望将来如果有人访问这个页面,最好将解决方案作为直接答案显示出来。
非常感谢@cristobal的帮助和努力,如果我还能给你投票的话我会的!
诀窍似乎是:
//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);生成这个(它有一个完美的alpha,即使你看不到白色页面):

真的不是我生命中最有趣的5个小时。希望它能阻止其他人经历同样的痛苦..
发布于 2012-08-23 08:05:00
使用上面相同的代码,并使用蓝色作为imagerotate操作中的第三个参数,它将用于在旋转后填充未覆盖的区域,即:
imagerotate($pasteboard, 5, 255);我们得到了下面的图像

我们看到蓝色区域是它填充的未覆盖区域,而黑色区域是图像的边缘阴影,GD似乎没有很好地处理旋转中使用的插值。
使用convert for imagemagick旋转相同的图像。下图中的commmand即$> convert -rotate 5 image.png image_rotated.png结果

显然,GD在旋转时不能很好地处理alpha颜色。
如果您可以使用exec或process来使用convert命令,那么您应该通过管道将这些图像操作传递给imagemagick。GD是一个简单的图像库,最近几年没有太多更新。或者尝试Imagemagick、Cairo或Gmagick,它们也有针对http://php.net/manual/en/book.image.php的pecl插件。
最后,有人做了一个使用GD http://www.exorithm.com/algorithm/view/rotate_image_alpha的函数,但结果并不美观,因为它是一个简单的线性插值:

摘自How to rotate an image in GD Image Library while keeping transparency?。也许,如果您将线性插值函数转换为双三次或四次,效果会更好。
发布于 2016-01-21 17:07:47
注意:这些答案对我不起作用,但这个答案起作用了。
$destimg = imagecreatefromjpeg("image.png");
$rotatedImage = imagerotate($destimg, 200, 0);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");https://stackoverflow.com/questions/12082472
复制相似问题