Trying to generate proportionally cropped thumbnails at a fixed width/height with PHP GD

后端 未结 4 374
深忆病人
深忆病人 2021-01-25 01:59

I\'m trying to create a Thumbnail Generator in PHP with GD that will take an image and reduce it to a fixed width/height. The square it takes from the original image (based on m

4条回答
  •  执笔经年
    2021-01-25 02:35

    Is this what you're looking for:

    Crop-To-Fit an Image Using ASP/PHP

    The script can be used to create square thumbnails out of portrait/landscape images. The solution requires two steps:

    1: resize the image proportionally so that one of the dimension matches the desired dimension while other is equal or greater.

    • Example 1: to crop a 1024x768px image (ar = 1.33) to 200x200px you must proportionally resize the image to 266x200px (ar = 1.33)
    • Example 2: to crop a 600x900px image (ar = 0.66) to 200x200px you must proportionally resize the image to 200x300px (ar = 0.66)

    2: crop from the middle of the image; the math is simple.

    • Example 1: To extract 200x200px portion from a 266x200px image, crop from 33,0
    • Example 2: To extract 200x200px portion from a 200x300px image, crop from 0,50

    Landscape cropping

    Portrait cropping

提交回复
热议问题