【问题标题】:ImageMagick White to Transparent Background While Maintaining White ObjectImageMagick 白色到透明背景,同时保持白色对象
【发布时间】:2017-04-21 13:49:10
【问题描述】:

我正在使用 PHP 的 ImageMagick 将图像的白色背景变为透明。我将图像 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();

这些转换效果很好。但是,如果我有一个带有白色物体的图像,由于将模糊值传递给paintTransparentImage(),它就不能很好地工作;这就是我清理锯齿状边缘的方式。

这是一个结果示例,注意白色沙发:

如果我没有传递一个模糊值,那么我会得到一个正确的剪辑,但我会留下凌乱的边缘:

我曾尝试使用resizeImage() 来实现所谓的“空间抗锯齿”(将图像放大到非常大 -> 使用paintTransparentImage() -> 缩小图像),但我没有注意任何重大变化。

我能做些什么来更好地处理这些真正的白色图像吗?我玩过trimImage()edgeImage(),但我无法得到我想要的结果。

最坏的情况(虽然不理想),是否有一种方法可以识别图像是否包含一定百分比的特定颜色? IE。如果图像包含 >90% 的白色像素,那么我可以运行 paintTransparentImage(),模糊值为 0 而不是 3000,这至少会给我一个适当的剪切。

谢谢。

【问题讨论】:

  • 已解决。编辑帖子以显示解决方案。

标签: php imagemagick


【解决方案1】:

解决方案:

先用其他颜色替换白色背景,然后将该颜色更改为透明。

<?php

# get img url
$imgUrl = $_GET['img'];

# create new ImageMagick object from image url
$im = new Imagick($imgUrl);

# replace white background with fuchsia
$im->floodFillPaintImage("rgb(255, 0, 255)", 2500, "rgb(255,255,255)", 0 , 0, false);

#make fuchsia transparent
$im->paintTransparentImage("rgb(255,0,255)", 0, 10);

# 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();

【讨论】:

    【解决方案2】:

    2021 年更新 => imagick 模块版本 3.4.4(不客气:o)):

    $imgick = new Imagick($pathToFile);
    $aTaille=$imgick->getImageGeometry();
    $colorToTransparent=$imgick->getImagePixelColor(0,0); 
    $imgick->borderImage($colorToTransparent, 10, 10);
    
    $imgick->floodFillPaintImage("rgb(255, 0, 255)", 2500, $colorToTransparent, 0 , 0, false);
    
    $imgick->transparentPaintImage("rgb(255,0,255)", 0, 100, false);
    
    $imgick->cropImage($aTaille['width'], $aTaille['height'], 10, 10);
    
    $imgick->setImageFormat('png'); 
    
    # set header type as PNG image
    header('Content-Type: image/png');
    # output the new image
    echo $im->getImageBlob();
    

    【讨论】:

      【解决方案3】:

      你可以试试 exec 命令。它对我有用。 可以参考this链接,

      <?php
          $rand_new = rand();
          $target_dir = 'yourFolder/';
          $imageName = 'yourImage';
          $image = $target_dir.$imageName;
          $imageFileType = strtolower(pathinfo($image,PATHINFO_EXTENSION));
          $new_image = time().".png";
          exec("convert $image -fill none -fuzz 10% -draw 'matte 0,0 floodfill' -flop  -draw 'matte 0,0 floodfill' -flop $new_image");
      ?>
      

      【讨论】:

      • Exec() 是一个 PHP 函数,用于执行一些外部程序。在这种情况下,您只是使用 ImageMagic 的命令行版本。这与问题无关。
      • 他有没有提到不要将 CLI 用于他的解决方案?我想这会帮助他得到他想要的东西。
      • 由于我是问题的作者,我可以说我早在 2017 年 4 月就已经解决了这个问题,而且您对使用“exec()”的回答绝对没有解决我的问题,我的朋友。
      • 好的,虽然它可能会帮助其他人。
      猜你喜欢
      • 1970-01-01
      • 2018-03-25
      • 2014-08-21
      • 2010-12-27
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 2015-04-25
      相关资源
      最近更新 更多