How can thumbnails be efficiently generated for gallery previews in PHP without compromising performance?

Generating thumbnails for gallery previews in PHP can be efficiently done by using the GD library to resize images. By resizing images to a smaller size, thumbnails can be generated quickly without compromising performance. Additionally, caching thumbnails can further improve performance by reducing the need to regenerate them for each page load.

// Function to generate thumbnail
function generateThumbnail($source, $destination, $width, $height) {
    $sourceImage = imagecreatefromjpeg($source);
    $thumbImage = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumbImage, $sourceImage, 0, 0, 0, 0, $width, $height, imagesx($sourceImage), imagesy($sourceImage));
    imagejpeg($thumbImage, $destination);
    imagedestroy($sourceImage);
    imagedestroy($thumbImage);
}

// Example usage
generateThumbnail('source.jpg', 'thumbnail.jpg', 100, 100);