Here we are going to observe how to use PHP to generate thumbnails for us. A thumbnail is a smaller size and usually smaller quality of an image shown as preview or link to the original image. Thumbnails can be very useful for faster loading times and quicker display of images on your web pages and a few other reasons. This script will allow us to use a directory on our server and convert all image files in it to a new thumbnail, with dimensions we want, and save it on a new thumbnails directory.
We do this using some PHP functions. First we have to open the directory that we specify as the directory to the images we want to convert to thumbnails. We use the function opendir().
The next part:
[code lang=”php”]
while (false !== ($fname = readdir($dir))) {
[/code]
This loops through the directory until it reaches the end of the files stored in the directory.
Then we make sure it is an image file type by checking its extension:
[code lang=”php”]
$valid_extensions = array(“jpg”,”gif”,”png”,”jpeg”);
if(in_array(strtolower($info[‘extension’]),$valid_extensions)){
[/code]
We use the function imagecreatefromjpeg() to make an image out of the file with the image extension. With this we grab the image’s width and height to calculate the new height of the thumbnail based off of the $thumbWidth specified in the beginning of the function (200).
We then use the function imagecreatetruecolor() to make a temporary image for the new thumbnail. With it we transfer it to a new image file in our thumbnails directory with the following code:
[code lang=”php”]
$genThumb = imagejpeg($tempImage,$pathToThumbs . rand(100,999) . $fname);
[/code]
The rand(100,900) make a random 3 digit number in front of the file name. Remove it if you wish.
Here is our function:
[code lang=”php”]
function generateThumbs(){
$pathToScreens = “../screenshots/”; // Directory to your images you want converted to thumbnails.
$pathToThumbs = “../screenshots/thumbs/”; // Directory to your thumbnails.
$thumbWidth = 200; // Width of the thumbnails generated.
$dir = opendir($pathToScreens) or die(“Could not open directory”);
$counter = 0;
while(($fname = readdir($dir)) !== false){
if($fname != “.” && $fname != “..”){
// Remove folders.
$valid_extensions = array(“jpg”,”jpeg”); // Only jpeg images allowed.
$info = pathinfo($pathToScreens . $fname);
if(in_array(strtolower($info[“extension”]),$valid_extensions)){
// Make sure the file is an image file by checking its extension to the array of image extensions.
$img = imagecreatefromjpeg($pathToScreens . $fname); // Select the file as an image from the directory.
$width = imagesx($img);
$height = imagesy($img);
// Collect its width and height.
$newHeight = floor($height * ($thumbWidth / $width)); // Calculate new height for thumbnail.
$tempImage = imagecreatetruecolor($thumbWidth,$newHeight); // Create a temporary image of the thumbnail.
// Copy and resize old image into new image.
imagecopyresized($tempImage,$img, 0, 0, 0, 0, $thumbWidth,$newHeight,$width,$height);
$genThumb = imagejpeg($tempImage,$pathToThumbs . rand(100,999) . $fname);
// Create the thumbnail with the new width and height in the thumbnails directory.
// I added a rand 3 digit number in front of the file name to avoid overwrite.
$counter++; // Increment.
}
}
}
if($counter > 0){
return $counter . ” thumbnails generated from the directory \””.$pathToScreens.”\”.”;
} else {
return “No image files could be processed.”;
}
closedir($dir); // Close the directory.
}
?>
[/code]
Then we can just call it like so:
[code lang=”php”]
echo generateThumbs();
[/code]
No comment yet, add your voice below!