使用php magickwand和imagemagick在旋转时失去png的透明度


Losing transparency of png with php magickwand and imagemagick when rotation is applied

嗨,我正在从运行MagickWand 0.1.8和ImageMagick 6.2.9的windows环境移植脚本,其中透明度与MagickRotateImage一起工作得很好。

新环境是linux运行MagickWand 1.0.8与ImageMagick 6.5.4-7和透明度丢失,它显示黑色的背景,只要我的标志图像旋转。

从我在网上找到的,似乎PixelSetColor($bg,"none")不适用于较新的版本,因此黑色。最后,我需要知道用什么来代替PixelSetColor($bg,"none")。我只是没有图像制作的背景,所以在这方面有点挣扎。

首先,我的php运行这个函数,从url获取600x600 png图像的本地60x60版本。

function makeThumb($fileContents){
    GLOBAL $localImgPath1;
    GLOBAL $localImgPath2;
    $wand = NewMagickWand();
    $lg = MagickReadImageBlob($wand,$fileContents);
    $lg_w = MagickGetImageWidth($wand);
    $lg_h = MagickGetImageHeight($wand);
    $max = max($lg_h,$lg_w);
    $scale_factor = 60/$max;
    MagickResizeImage($wand,$lg_w*$scale_factor,$lg_h*$scale_factor, MW_GaussianFilter, .7);
    MagickWriteImage($wand, $localImgPath1);
    if($localImgPath2!="")
        MagickWriteImage($wand, $localImgPath2);
    $resized_w = MagickGetImageWidth($wand);
    $resized_h = MagickGetImageHeight($wand);
    DestroyMagickWand($wand);
}

然后,我用这个来读取本地写入的png图像并旋转它:

$logo = NewMagickWand();
$bg = NewPixelWand();
PixelSetColor($bg,"none");
MagickReadImage($logo, $localImgPath1);
MagickRotateImage($logo, $bg, $r);
header('Content-Type: image/PNG');
MagickEchoImageBlob($logo);
DestroyPixelWand($bg);
DestroyMagickWand($logo);

我试过添加:

$transparent = NewPixelWand("#FFFFFF");
PixelSetAlpha($transparent, 0);
//and then making the rotate call:
MagickRotateImage($logo, $transparent, $r);

也尝试添加MagickSetImageAlphaChannel($logo, MW_SetAlphaChannel);在旋转步骤之前。看到一些帖子提到这种方法,但可能这不是正确的使用方法。不确定。

我也有同样的问题在一个脚本,绘制一个文本字符串与设置字体到图像上。黑色立即显示,甚至在旋转之前应用,所以希望相同的修复标志脚本将被字体脚本使用。

任何帮助都将非常感激。谢谢。

您可以选择四个值作为透明背景。依次尝试:

"none", "transparent", "#00000000", or "rgba(0, 0, 0, 0.0)"

问题出在ImageMagick 6.5.4-7上。肯定是有bug的发布。升级到6.8.4-10及以上代码工作正常,PixelSetColor($bg,"none")的透明度工作