Jump to content

Remove transparrency / Fit to canvas


Joshua4550

Recommended Posts

Hi,

 

I have recently been reading through some of the functions in the GD library, and custom functions people have built off of that.

 

I was wondering if there's a way, with or without the GD library, to get an image and crop it for "fit to canvas"? Such a feature would remove unnecessary transparrent canvas. For example if you had a 500x500 canvas, but the image inside of it was only 200x300, it would crop the image down to 200x300.

 

Is this possible?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/207677-remove-transparrency-fit-to-canvas/
Share on other sites

Yes, it's possible. As long as there's no transparency within the part of the image you want to keep it would be pretty easy.

 

You'll need to go over the image and find out the coordinates for where it starts, and where it ends. Some functions you should look into:

 

imagecolorat

imagecolorsforindex

 

You can use those to determine if a particular point is transparent or not. (For transparent pixels the alpha will be 127.)

 

Once you determine those 2 points you can easily crop the image to what you want.

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.