What are recommended resources or functions in PHP for creating thumbnail images from larger images in a directory?
When dealing with a directory of larger images, you may want to create thumbnail images for better performance and display on your website. One way to achieve this in PHP is by using the GD library, which provides functions for image manipulation. Specifically, you can use functions like `imagecreatefromjpeg()`, `imagecopyresampled()`, and `imagejpeg()` to create thumbnail images from larger images in a directory.
<?php
// Set the directory containing the larger images
$directory = 'path/to/larger/images/';
// Get all files in the directory
$files = glob($directory . '*');
// Loop through each file
foreach ($files as $file) {
// Load the larger image
$image = imagecreatefromjpeg($file);
// Get the dimensions of the larger image
$width = imagesx($image);
$height = imagesy($image);
// Calculate the thumbnail dimensions (e.g. 100px width)
$thumbWidth = 100;
$thumbHeight = intval($height * $thumbWidth / $width);
// Create a new image for the thumbnail
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
// Resize the larger image to create the thumbnail
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
// Save the thumbnail image
$thumbFilename = 'path/to/thumbnails/' . basename($file);
imagejpeg($thumb, $thumbFilename);
// Free up memory
imagedestroy($image);
imagedestroy($thumb);
}
?>