image icon indicating copy to clipboard operation
image copied to clipboard

How to remove white background in logos

Open andrecuellar opened this issue 5 years ago • 1 comments

I'm trying to remove the white background in logos to get a transparent icon, for example

on this image

enter image description here

Is .jpg, I want to remove white background (Only what's outside if is possible, otherwise removing all the white background)

I'm trying using this code

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;
}

but this is my result

enter image description here

the image lost color

what am I doing wrong? I feel that I am very close to achieving it, however, I need the icons to have color

andrecuellar avatar Jan 04 '21 16:01 andrecuellar

In your function you're applying a mask on a black image, the result looks correct. You should try to add $logo to $new_image before applying the mask.

vjandrea avatar Feb 23 '21 06:02 vjandrea