Building a gallery of thumbnails of same size in PHP
Creating Thumbnails: Part Two
In this second part, the script of thumbnails will be upgraded so that the height and width are both defined for all thumbnails, while keeping the proportions of the original images.
Indeed the disadvantage of the thumbnail script in the first part was to resize the image to a height or width given the choice, but the other dimension remained variable which does not allow to build galleries.
To achieve this, we will resize the image to create an intermediary thumbnail which retains the proportions, and then crop it either vertically or horizontally, depending in the form of the initial image.
Calculating the size of the intermediate thumbnail
The intermediate thumbnail will be either the width or the height of the final thumbnail, depending if the width or the height is less than the other side.
Dimensions of the final thumbnail
In the demo, the dimensions are 150 in height and 140 in width. Any other values could be choosen.
$thumbh = 150; $thumbw = 140;
Choice of formula
Should we crop on the height or width?
If the intermediate thumbnail has a height of 150, its width must be greater than or equal that 140.
If its width is 140, its height must be greater than or equal to 150.
To anticipate it, we calculate the ratio of height to width, for the source image and the thumbnail.
$ratio = $h / $w; $nratio = $thumbh / $thumbw;
If ratio, the original proportion, is more than nratio, the proportion of the final thumbnail, it must be cropped on the height, and on the width in the other case.
And if we crop on the height, the image is scaled depending on the width.
Resizing on the width
In case ratio is higher than nratio, in other words, the original image is too high compared to the thumbnail to achieve, the intermediate thumbnail is created on the base of the desired width:
$x = intval($w * $nh / $h);
if ($x < $nw)
$nh = intval($h * $nw / $w);
$nw = $x;
A temporary width of x is defined using the formula: w * nh / h.
If x is less than the desired width, the image is scaled on the height by
the formula nh = h * nw / w.
Otherwise the height is retained and the temporary value x is given to the width nw of the thumbnail.
Resizing on the height
If the new ratio nratio is bigger than the original ratio, the image is wider than high, it must be reduced on the basis of height, and then cropped horizontally.
$x = intval($h * $nw / $w);
if ($x < $nh)
$nw = intval($w * $nh / $h);
$nh = $x;
The calculations are inverted.
Creating the intermediate image
The PHP functions of the first part are used again to resize the image:
$resimage = imagecreatefromjpeg($oldname); $newimage = imagecreatetruecolor($nw, $nh); imageCopyResampled($newimage, $resimage,0,0,0,0,$nw, $nh, $w, $h);
But furthermore, we must crop the image, thus creating an empty image on which a part of the intermediate thumbnail is copied.
$viewimage = imagecreatetruecolor($thumbw, $thumbh); imagecopy($viewimage, $newimage, 0, 0, 0, 0, $nw, $nh);
Now we have just to save the thumbnail.
In the demo, an array of images is scanned by the foreach structure. See the complete source code below.
Thumbnail gallery created with the script
Click to display the original image.
- See the complete PHP script of thumbnail gallery.
- Download the archive including the script and images.