Attempts to generate proportionally cropped fixed width / height thumbnails using PHP GD

I am 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 that he takes from the original image (based on my fixed width / height) will come from the center of the image to get a proportionally correct thumbnail.

I will try to demonstrate this confusing sentence with some nice ASCII:}

LANDSCAPE EXAMPLE: XXXXXXXXXXXXXXXX XXXXOOOOOOOOXXXX XXXXOOOOOOOOXXXX XXXXOOOOOOOOXXXX XXXXOOOOOOOOXXXX XXXXXXXXXXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX PORTRAIT EXAMPLE: XXXXXXXX XXXXXXXX OOOOOOOO OOOOOOOO OOOOOOOO OOOOOOOO XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX 

As you can see, it pulls a square from the center of the image to use as a thumbnail. In theory, it seems simple to get the height / width of the image, and then calculate the offset based on my fixed width / height to get a sketch. But I can not imagine a way to encode it: /

Also, how can I resize the image before pulling out the center of the square? So the thumbnail contains a detailed image of the original, and not an image with an enlarged graphic image?

+1
source share
4 answers

I apologize if I offer an indirect answer. But you can try using this library: PHPThumb . It is very easy to use. It supports GD and has this feature called adaptive resizing , which resizes the image from the center.

Here is an example of code from documents for adaptive resizing:

 <?php require_once 'path/to/ThumbLib.inc.php'; try { $thumb = PhpThumbFactory::create('/path/to/image.jpg'); } catch (Exception $e) { // handle error here however you'd like } $thumb->adaptiveResize(175, 175); $thumb->show(); ?> 
+3
source

This is what you are looking for:

Crop image using ASP / PHP

The script can be used to create square sketches from portrait / landscape images. The solution requires two steps:

1: proportionally resize the image so that one of the sizes corresponds to the desired size, while the other is equal to or larger.

  • Example 1: to crop an image of 1024x768px (ar = 1.33) to 200x200px, you must proportionally resize the image to 266x200px (ar = 1.33)
  • Example 2: to crop an image with a size of 600x900 pixels (ar = 0.66) to 200x200 pixels, you must proportionally resize the image to 200x300px (ar = 0.66)

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

  • Example 1: To extract a 200x200px portion from a 266x200px image, crop from 33.0
  • Example 2: To extract a 200x200px portion of a 200x300px image, crop to 0.50

Landscape cropping

Portrait cropping

+3
source

How about this: It takes an image of any size and resizes it proportionally (or up) to fill the canvas of any size.

eg. Original image: 400w x 600h and the thumbnail size should be 100w x 225h - the output image will be 100w x 225h and the images will be vertically centered with a width of 100 (maximum) and a height of 150 (with an upper and lower border of 37.5 pixels)

Here is the function

 function resize_to_canvas($filename,$canvas_w=100,$canvas_h=225){ list($width, $height, $type) = getimagesize($filename); $original_overcanvas_w = $width/$canvas_w; $original_overcanvas_h = $height/$canvas_h; $dst_w = round($width/max($original_overcanvas_w,$original_overcanvas_h),0); $dst_h = round($height/max($original_overcanvas_w,$original_overcanvas_h),0); $dst_image = imagecreatetruecolor($canvas_w, $canvas_h); $background = imagecolorallocate($dst_image, 255, 255, 255); imagefill($dst_image, 0, 0, $background); $src_image = imagecreatefromjpeg($filename); imagecopyresampled($dst_image, $src_image, ($canvas_w-$dst_w)/2, ($canvas_h-$dst_h)/2, 0, 0, $dst_w, $dst_h, $width, $height); imagegif($dst_image, $filename); imagedestroy($dst_image);} 

This function will replace the original file, but will be easily modified to create a new thumbnail. Just change the file name to the string imagegif ($ dst_image, $ filename);

+1
source

Without a clear and well-defined goal, it is difficult to try to propose a well-suited solution. In addition, you will usually find other stackers more useful if you at least try to find a solution for the table.

As I said, I found a couple of online tutorials that can at least give you an idea of ​​how to get started - let him know, and if you still have problems, feel free to post additional questions (with examples code and details of what does not behave).

0
source

All Articles