我有一个透明背景的png图像,我想将其转换为具有白色背景的jpg图像。
代码基本上是这样的:
$image = new Imagick('transparent.png');
$image->writeImage('opaque.jpg');
但这会产生黑色背景jpg。我一直在努力寻找将透明转换为白色无效的最糟糕的文档。
编辑:嗯,我尝试了Marc B的想法并且有点工作。
$image = new Imagick('transparent.png');
$white = new Imagick();
$white->newImage($image->getImageWidth(), $image->getImageHeight(), "white");
$white->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0);
$white->writeImage('opaque.jpg');
$image->destroy();
$white->destroy();
现在的问题是,它总是导致脚本出现段错误。
flattenImages()
实际上是有效的。
但请记住,它不会修改给定的\Imagick()
对象,但会返回一个新对象:
$image = new \Imagick('transparent.png');
// Need to use the result of $image->flattenImages() here!
$image = $image->flattenImages();
$image->writeImage('opaque.jpg');
flattenImages()
默认为背景颜色white
。如果要使用其他背景颜色,则必须在加载图像之前进行设置:
$image = new \Imagick();
// Needs to be called before the image is loaded!
$image->setbackgroundcolor('green');
$image->readimage('transparent.png');
$image = $image->flattenImages();
$image->writeImage('opaque.jpg');
一般来说,当涉及到函数调用的顺序时,Imagick API非常合理(就像convert
及其在命令行中的参数一样),所以请务必检查您的订单是否正确。
祝好运!
2016年4月编辑:
$image->flattenImages()
was deprecated,应替换为:
$image->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN)
很难找到关于此的确切信息,但似乎这适用于PHP> = 5.6。
感谢ve提示!
我有一种情况,我试图用白色替换透明背景(但保持为png)。尝试了几种不同的方法(包括setImageAlphaChannel和setImageBackgroundColor)。结合OP对compositeImage的使用我想出了这个(希望对其他人有帮助):
$pic = new Imagick($filelocation); //specify file name
$pic->setResourceLimit(6, 1);
if ($pic->getImageAlphaChannel()) {
$white = new Imagick();
$white->newImage($pic->getImageWidth(), $pic->getImageHeight(), "white");
$white->compositeImage($pic, Imagick::COMPOSITE_OVER, 0, 0);
$pic = clone $white;
$white->destroy();
$pic->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
}
//do more things with $pic
尝试:
$image = new Imagick('transparent.png');
$image->setImageMatte(true);
$image->setImageMatteColor('white');
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE);
$image->writeImage('opaque.jpg');
在将PDF转换为PNG时遇到了同样的问题,我使用了flattenImages()。
//get the first page of the PDF
$im = new imagick( $file.'[0]' );
//set the background to white
$im->setImageBackgroundColor('white');
//flatten the image
$im = $im->flattenImages();
//do the rest of the image operations
$im->setResolution( 181, 181 );
$im->setCompressionQuality(100);
$im->resizeImage ( 181, 181, imagick::FILTER_LANCZOS, 1, TRUE);
$im->setImageFormat('png');
$imageName = $title.'_thumb.png';
$image = new Imagick('transparent.pdf');
$image->setImageType (imagick::IMGTYPE_TRUECOLOR);
$image->writeImage('opaque.tif');
为我做了!
(而不是以前的想象力:: IMGTYPE_TRUECOLORMATTE)
试试这个:
$white->newImage($image->getImageWidth(), $image->getImageHeight(), "transparent");
您可以通过更改Imagick常量来尝试它,如下所示
//$image will conatains image which needs background to be transparent
$white = new Imagick();
$white->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel( "white" ));
$white->compositeimage($image, Imagick::COMPOSITE_DEFAULT, $x1OfTransparentImage, $y1OfTransparentImage,);
$white->flattenImages();
$white->writeImage('opaque.jpg');
$white->destroy();
试试以下内容,它对我有用:
$im = new Imagick('trans.png');
$im->setimagebackgroundcolor('white');
$im = $im->flattenimages();
$im->writeimage('transToWhite.jpg');
希望有所帮助!
关于segfault的问题,我遇到了同样的问题。
显然$image->writeImage('somename')
摧毁了$image
或它的参考。
我遇到了同样的问题。我绕过它的方法是删除对我写完的对象的破坏调用。看似草率但这解决了segfault的问题
Segfault问题:我有一个类似的问题(脚本一直给我segfault,即使图像被正确处理和编写),我发现检查错误报告后的解决方案,请参阅:https://bugs.php.net/bug.php?id=61122
知道了,尝试添加
$white->setResourceLimit(6, 1); // 6 means "limit threads to"
在有问题的行之前(在我的情况下,我必须把它放在$ im-> resizeImage(...)之前;)