With PHP/GD, how do I trim an image?

后端 未结 2 957
时光取名叫无心
时光取名叫无心 2021-01-21 09:56

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

2条回答
  •  攒了一身酷
    2021-01-21 10:15

    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

提交回复
热议问题