Thumbnails of images in PHP with a specific height and width
Create tracks: Second part
In the second part, the thumbnail script, or thumbnail, will be improved so that the height and width of the thumbnail are set for all thumbnails, maintaining the proportions of the original images.
Indeed, the disadvantage of the thumbnail script of the first part was resizing the image to a given height or width, but the other dimension remained variable, which does not allow for galleries.
To achieve this result, preferred on a web page, the image will be resized to create an intermediate thumbnail that maintains proportions and then cropped to either height or width, depending on the shape of the original image.
Intermediate thumbnail sizing calculation
The intermediate thumbnail will have the width or height of the displayed thumbnail, depending on the height or width that is smaller than the other size.
Volume dimensions
In the demo, the dimensions are 150 in height and 140 in width. We could choose any values.
$thumbh = 150; $thumbw = 140;
Formula selection
Do I need to trim to height or width?
If the intermediate thumbnail has a height of 150, its width will be greater than or equal to this value.
If its width is 140, its height must be greater than or equal to.
To anticipate or calculate the aspect ratio of an image and thumbnail.
$ratio = $h / $w; $nratio = $thumbh / $thumbw;
If the ratio, the initial fraction, is greater than the nrazio, the fraction of the final miniature, then you need to cut off the height. Width in another case.
And if you grind in height, then the size of the image changes to width.
Width change
If the ratio exceeds the value, that is, the original image is too high compared to the thumbnail to be executed, an intermediate thumbnail is created based on the desired width:
$x = intval($w * $nh / $h);
if ($x < $nw)
{
$nh = intval($h * $nw / $w);
}
else
{
$nw = $x;
}
x, the intermediate width is determined by changing the image size to the formula w * nh/h.
If x is less than the desired width, the image size at height changes to the formula nh = h * nw/w.
Otherwise, we keep the height and set the new width nw to an intermediate value of x.
Change Height
If the new ratio is larger than the original ratio, then the image is wider than the higher, it must be reduced based on height, and then cropped to width.
$x = intval($h * $nw / $w);
if ($x < $nh)
{
$nw = intval($w * $nh / $h);
}
else
{
$nh = $x;
}
The calculations are reversed.
Create Intermediate Image
The PHP functions of the first part are again used to reduce the image:
$resimage = imagecreatefromjpeg($oldname); $newimage = imagecreatetruecolor($nw, $nh); imageCopyResampled($newimage, $resimage,0,0,0,0,$nw, $nh, $w, $h);
But in addition, you need to crop the image, so a blank image is created, into which part of the intermediate thumbnail is copied.
$viewimage = imagecreatetruecolor($thumbw, $thumbh); imagecopy($viewimage, $newimage, 0, 0, 0, 0, $nw, $nh);
All that remains is to save the thumbnail
In the demo, the image table is scanned by the foreach structure. See full source code.
Thumbnail Gallery created using script
Click to view the source image.
Source code
- See the full PHP thumbnail script.
- Download the archive with the script and images.