旋转PNG图像,并保持透明背景


Rotate PNG image, and maintain transparent background

我想旋转图像,但我似乎无法维护图像本身。

这是我所拥有的:

// Content type
header('Content-type: image/png');  
$image = 'http://mapning.com/img/plane.png';
$file = imagecreatefrompng($image);
// Rotate
$rotate = imagerotate($file, $angle, 0);
// Output
imagepng($rotate);

我做错了什么?

您必须专门强制 GD 以保持透明度。尝试

imagesavealpha($file, TRUE);

imagepng()通话之前。

在GD和ImageMagick中,对我有帮助的是获取您最终将要覆盖的背景颜色的十六进制/RGB代码。然后,将其用作"背景"颜色。旋转后,将该颜色转换为透明。然后,一旦您进行叠加,小的抗锯齿边缘将看起来很平滑。