Jump to content

Cutting out images


en

Recommended Posts

Hi there!

 

I have some product images with an single coloured background. I want to cut out the products. My very simple solution is this one:

 

$img = imagecreatefromjpeg($remote_img);

$bg_color = ImageColorAt ($img, 1, 1);
ImageColorTransparent($img, $bg_color);

imagepng($img, $img_path);
ImageDestroy($img);

 

 

 

The results are okay, but of course not the best. The products are tattered. Is there a way to improve this method? Of course you can only set one colour to transparent, but is there a way to fill the background with another colour using a threshold to improve the results?

Link to comment
https://forums.phpfreaks.com/topic/153313-cutting-out-images/
Share on other sites

Although there is no treshhold parameter, there is an imagefilltoborder function.

Also an option for 'custom' things is to use the height/width of the image and then using a nested loop along with imagecolorat , you could compare those results to the color want to replace and if matches replacing it with imagesetpixel.

As you might imagine however, this wouldn't be a good idea to put on a high traffic page and you'll probably need to up the amount of memory allocated to php.

 

If you have access to imagick, you could try taking a look at the imagick classes and functions as it has a lot more options than GD, the documention however is severly lacking and I've been unable to draw a simple pie slice for weeks now.

Link to comment
https://forums.phpfreaks.com/topic/153313-cutting-out-images/#findComment-805467
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.