Create image thumbnails for web pages
You can create thumbnails for web pages using the PHP program installed on the server associated with the boot file, but to automate line creation and master image quality, it is better to install the script locally. It may be easier to provide the most efficient graphics functions.
Install PHP Graphics Library
PHP in version 4 or 5 uses the gd2 library for image processing, which is not enabled by default (it can be on the server, the phpinfo function is used to view it).
To install the gd2 graphics extension:
- Copy to/php/php.ini-recommended in php.ini.
- Set the path of extensions in the extension_dir variable, for example: extension_dir = "c :/php/ext."
- Include php_gd2.dll (or Linux php_gd2.so) in extension lists by removing the comma at the beginning.
Now, to make sure that gd is properly activated, run the following program provided by the PHP guide:
<?php
$array=gd_info ();
foreach ($array as $key=>$val)
{
if ($val===true) { $val="Enabled"; }
if ($val===false) { $val="Disabled"; }
echo "$key: $val \n";
}
?>
It displays supported formats or an error message until the extension is enabled.
Create Thumbnail
With PHP, you can create good quality thumbnails (tracks).
Set options
$oldname = "girl.jpg";
$newname = "thumb.girl.jpg";
$newh = 240;
We chose a height of 240 pixels for the thumbnail. You can, if better, set a new width: $ neww = xxx;
Dimension interpolation
$size = getImageSize($oldname);
$w = $size[0];
$h = $size[1];
$neww = intval($newh * $w / $h);
If you decide to specify width rather than height, the formula is $ newh = intval ($ neww * $ h/$ w);
Recreate a smaller image
$resimage = imagecreatefromjpeg($oldname);
$newimage = imagecreatetruecolor($neww, $newh);
imageCopyResampled($newimage, $resimage,0,0,0,0,$neww, $newh, $w, $h);
Functions were selected for the quality of the result. In some versions of PHP, they will not be available, and an equivalent function should be found in the guide, but its results may be of lower quality .
Save New Image
imageJpeg($newimage, $newname, 85);
Similarly, for the image type, select the function corresponding to the file format. In the case of a JPeg file, the third parameter is the recovery quality (opposite to the compression level), the maximum is 100 with zero compression.
Improve image quality
In our example with a comic book-style image, the thumbnail is as sharp as the original drawing. With other types of images, such as photos, if you are not satisfied with the result, you can use the G'MIC plugin.
To use this software:
1) Download and install free GIMP software.
2) Download and install G'MIC plugin.
3) In the filter menu, select G'MIC and select Enhancement, and then try the options.
Note that the G'MIC plugin is particularly effective for improving the quality of images that need to be restored, blurred, pixelated, or spot photos.
Comparison of results
The first thumbnail is made by G'MIC, the second by PHP with the imageCopyReampled overwrite command.
Source code
- resize.php file
Rename resize-php.txt to resize.php. - Interpolation code.
The second part: Creation of thumbnails with specified height and width.
Obtaining software
- G 'MIC. Plugin for GIMP.