How can one ensure that thumbnails are quick to load and have smaller dimensions when using PHP scripts?

To ensure that thumbnails are quick to load and have smaller dimensions when using PHP scripts, you can use the GD library to generate thumbnails on the fly. By resizing the images to smaller dimensions before displaying them, you can reduce the file size and loading time of the thumbnails.

<?php
// Set the maximum dimensions for the thumbnails
$maxWidth = 150;
$maxHeight = 150;

// Load the original image
$originalImage = imagecreatefromjpeg('original.jpg');

// Get the original image dimensions
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);

// Calculate the new dimensions for the thumbnail
if ($originalWidth > $originalHeight) {
    $newWidth = $maxWidth;
    $newHeight = $originalHeight * ($maxWidth / $originalWidth);
} else {
    $newHeight = $maxHeight;
    $newWidth = $originalWidth * ($maxHeight / $originalHeight);
}

// Create a new image with the new dimensions
$thumbnail = imagecreatetruecolor($newWidth, $newHeight);

// Resize the original image to fit the new dimensions
imagecopyresampled($thumbnail, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Output the thumbnail to the browser
header('Content-Type: image/jpeg');
imagejpeg($thumbnail);

// Free up memory
imagedestroy($originalImage);
imagedestroy($thumbnail);
?>