我正在使用 PHP图像魔术师 http://php.net/manual/en/book.imagick.php将图像的白色背景变为透明。我将图像 URL 传递给这个 PHP 脚本,它返回图像。
<?php
# grab the remote image url
$imgUrl = $_GET['img'];
# create new ImageMagick object
$im = new Imagick($imgUrl);
# remove extra white space
$im->clipImage(0);
# convert white background to transparent
$im->paintTransparentImage($im->getImageBackgroundColor(), 0, 3000);
# resize image --- passing 0 as width invokes proportional scaling
$im->resizeImage(0, 200, Imagick::FILTER_LANCZOS, 1);
# set resulting image format as png
$im->setImageFormat('png');
# set header type as PNG image
header('Content-Type: image/png');
# output the new image
echo $im->getImageBlob();
这些转变效果非常好。但是,如果我有一个带有白色物体的图像,由于将模糊值传递给绘制透明图像() http://php.net/manual/en/imagick.painttransparentimage.php;这就是我清理锯齿状边缘的方法。
这是结果的示例,注意白色沙发:
如果我没有传递模糊值,那么我会得到正确的剪切,但会留下混乱的边缘:
我尝试过使用调整图像大小() http://php.net/manual/en/imagick.resizeimage.php为了实现所谓的“空间抗锯齿”(将图像放大得很大 - >使用绘制透明图像() http://php.net/manual/en/imagick.painttransparentimage.php-> 缩小图像),但我没有注意到任何重大变化。
我可以做些什么来更好地处理这些纯白色图像吗?我玩过修剪图像() http://php.net/manual/en/imagick.trimimage.php and 边缘图像() http://php.net/manual/en/imagick.edgeimage.php,但我无法得到我想要的结果。
最坏的情况(尽管并不理想),是否有一种方法可以识别图像是否包含一定百分比的特定颜色? IE。如果图像包含 >90% 白色像素那么我可以运行绘制透明图像() http://php.net/manual/en/imagick.painttransparentimage.php模糊值为 0 而不是 3000,这至少会给我一个适当的剪切。
Thanks.