如何使用Intervention Image删除图像中的背景

4

我想去掉标志中的白色背景,获得透明图标,例如:

这张图片:

enter image description here

它是 .jpg 格式,我想移除白色背景(如果可能,只移除外面的部分;否则移除所有白色背景)。

我正在尝试使用 Laravel 的 intervention image 通过以下代码实现:

public function resizeImage($logo, $placeName, $domain)
{
    $mask = Image::make($logo)
        ->contrast(100)
        ->contrast(50)
        ->trim('top-left', null, 40)
        ->invert(); // invert it to use as a mask

    $new_image = Image::canvas($mask->width(), $mask->height(), '#000000')
        ->mask($mask)
        ->resize(32,32, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        })
        ->encode('png', 100);

    $route = $domain . '/favicon/favicon.png';

    Storage::disk('gcs')
        ->put($route, $new_image);
    $route = Storage::disk('gcs')
        ->url($route);

    return $route;
}

但这是我的结果

enter image description here

图像失去了颜色(黑色)

我做错了什么?我感觉我离成功很近了,但是我需要让图标有颜色。


显然问题出在掩码上,但如果我尝试使用除掩码以外的方法,则结果是完全黑色的图像。 - Andre
你解决了吗?如果是,请分享解决方案。 - Amanullah Aman
1个回答

0

$new_image = Image::canvas($mask->width(), $mask->height())这是什么意思? - Andre
2
为什么这会有所不同?请在回答中添加一些解释。 - miken32
请查看官方文档:http://image.intervention.io/api/canvas - Shahrukh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接