views:

262

answers:

1

I want to mimic the trim behavior of Photoshop (crop the area that is covered by the same color on all sides) with GD and PHP but I'm lacking ideas on how to accomplish this.

If someone has any idea on how to do this I would very much like to hear it.

Thanks in advance.

+2  A: 

The easiest solution I can think of is:

  1. Find out the color of the top left pixel.
  2. Go through the image line by line, horizontally and vertically, starting on the top, bottom , left hand, and right hand side respectively. If a line consists of ONLY the color encountered in the top left corner it is trimmable empty space. Omit that line from the resulting image by increasing the x or y coordinate of your imagecopy*() function.

Untested but might work.

BTW, ImageMagick can do this out of the box: http://www.imagemagick.org/Usage/crop/#trim

Pekka