简体   繁体   中英

Cropping transparency in an image

I have a PNG image, which has a transparent background.

I'm using PHP GD to display these images, from a server. Is it possible in GD to also trim this transparent background, while maintaining the remainder of the image to be transparent and if so, how would I do it?

eg. original image: http://puu.sh/8pNdx/ab2ff1e64f.png

and the expected result after processing would be: http://puu.sh/8pNg7/7b7ec85459.png

If it is really important to remove transparent pixels than you will have to write a small custom algorithm.

  1. First try to find none transparent boundary pixels. ie Pixels that actually define boundary of your object.
  2. Than create a new transparent Bitmap according to position of those identified boundary pixels.
  3. Than draw all your none transparent pixels on that new Bitmap according to new adjusted position.

Try searching for some efficient algorithm for:

separating objects from an image

I hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM