【问题标题】:How do we remove image white background color using PHP Code [duplicate]我们如何使用 PHP 代码去除图像的白色背景颜色 [重复]
【发布时间】:2012-12-21 21:08:31
【问题描述】:

可能重复:
Translate Ruby into PHP code with the following code

我发现了一个非常有用的 Ruby 代码来去除图像的白色背景色。

请看下面的参考代码: Remove white background from an image and make it transparent

我试图将代码翻译成 php。但是我得到了一个不想要的结果。这是我第一次在这里发帖,有人可以给我一些指导并原谅我英语不好。

function setTransparency($new_image,$image_source) 
{         
    $transparencyIndex = imagecolortransparent($image_source); 
    $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255); 

    if ($transparencyIndex >= 0) { 
        $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);    
    } 

    $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); 
    imagefill($new_image, 0, 0, $transparencyIndex); 
    imagecolortransparent($new_image, $transparencyIndex); 

}

//create image from the source link
$image = imagecreatefrompng('http://i.stack.imgur.com/k7E1F.png');

//create image mask layer
$new_image = ImageCreateTruecolor(imagesx($image), imagesy($image));

//remove white background 
setTransparency($new_image,$image); 

//merge mask with original image source
ImageCopyMerge($new_image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image), 100);

imagejpeg($new_image, null, 95);

【问题讨论】:

  • 您的链接指的是 Mathematica 解决方案,而不是 Ruby。您需要考虑使用 ImageMagick:php.net/manual/en/book.imagick.php 这是另一个包含 ImageMagick 解决方案的问题:stackoverflow.com/questions/7738437/…
  • 这不是一个重复的问题,他想使用实际的 php 库来实现 Mark Ransom 的算法,您发布了一些与 Mark Ransom 的算法完全无关的命令行实用程序

标签: php image image-processing


【解决方案1】:

JPEG 格式不支持透明度。您应该考虑使用 png 作为输出格式。将最后一行更改为:

imagepng($new_image, null, 9);

【讨论】:

    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2020-01-13
    • 2018-06-24
    • 2019-03-31
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多