Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.