2012-08-22 25 views
2

我一直在抨击我的头agains简单的东西..php imagerotate()废除png上的阿尔法?

// ....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 

给了我这样的:

enter image description here

HORAY!

完美阿尔法png格式复合...

现在我想将它旋转,这样,而不是从$ = $纸板我这样做:

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

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

这可悲的是给了我这个:

enter image description here

BOOOO!

香港专业教育学院尝试设置颜色,如:

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

也是最后ATTR,如:采样等等等等

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

新的空图像...

没有骰子....

任何人都可以帮忙吗?

+0

尝试旋转它,然后再调用'imagesavealpha' – Musa

+0

谢谢@Musa也试过.. ..! – Alex

+0

ignore_transparent(第四个参数)仅在PHP 5.1中添加。用'echo phpversion();检查你使用的是什么版本的PHP;' – xconspirisist

回答

4

我回答我的问题只是因为我已经尝试了10-15条建议,我已经看到整个网络所有提供“几乎”正确的解决方案,但没有确切的,也见过这个问题发布了一些现在就放置一些地方,希望如果有人在将来能够访问此页面,最好将解决方案作为直接答案。

MASSIVE感谢@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); 

产生这样(它即使你不能看到对白色页面一个完美的阿尔法):

enter image description here

真的不是最好玩5小时我的生活...希望它会阻止别人经历同样的痛苦..

2

使用上述同样的代码,并使用蓝色在imagerotate操作的第三个参数,这将是它用于填充旋转即在未覆盖的区域:

imagerotate($pasteboard, 5, 255); 

我们获得下面的图片

Rotated using blue as third parameter

我们看到蓝色的区域是它填补未覆盖的区域,而黑色是从图像的边界阴影这似乎GD不沿在处理好旋转中使用的插补。

使用imagemagick转换器对同一图像进行旋转。条命令即$> convert -rotate 5 image.png image_rotated.png结果在图像中以下

rotated using imagemagick

显然旋转时GD不处理的α颜色好。

如果你有机会使用execprocess使用convert条命令,你应该管这些图像操作来替代ImageMagick。 GD是一个简单的图像库,最近几年没有更新过。否则请尝试Imagemagick,Cairo或Gmagick,其中也有pecl插件http://php.net/manual/en/book.image.php

不得已作出某人它使用GD http://www.exorithm.com/algorithm/view/rotate_image_alpha,对于你所照顾,但结果不是因为它的一个简单的线性插值漂亮的函数:

linear interpolation

How to rotate an image in GD Image Library while keeping transparency?拍摄。也许如果你将线性插值函数转换成双三次或四次方,它会更好看。

+0

巨大的感谢! – Alex

0

请注意这些答案不适用于我,但这样做。

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