What are the performance implications of generating thumbnails for images in a PHP script for a photo gallery?

Generating thumbnails for images in a PHP script for a photo gallery can have performance implications, especially if the script is generating thumbnails on-the-fly for each image request. This can lead to increased server load and slower page load times. To improve performance, it's recommended to generate and store thumbnails for images in advance, rather than generating them dynamically. This way, the thumbnails can be served quickly without the need for on-the-fly processing.

// Example code to generate and store thumbnails for images in advance
function generateThumbnail($imagePath, $thumbnailPath, $width, $height) {
    $source = imagecreatefromjpeg($imagePath);
    $thumbnail = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumbnail, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
    imagejpeg($thumbnail, $thumbnailPath);
    imagedestroy($source);
    imagedestroy($thumbnail);
}

// Usage example
$imagePath = 'path/to/image.jpg';
$thumbnailPath = 'path/to/thumbnail.jpg';
generateThumbnail($imagePath, $thumbnailPath, 100, 100);