How can PHP be used to create thumbnails for images on a website?

To create thumbnails for images on a website using PHP, you can use the GD library which provides functions for image manipulation. You can resize the original image to create a smaller thumbnail version while maintaining the aspect ratio.

<?php
// Path to the original image
$original_image = 'path/to/original/image.jpg';

// Load the original image
$source = imagecreatefromjpeg($original_image);

// Get the dimensions of the original image
$width = imagesx($source);
$height = imagesy($source);

// Calculate the new dimensions for the thumbnail
$new_width = 100; // Set the desired width for the thumbnail
$new_height = ($height / $width) * $new_width;

// Create a blank thumbnail image
$thumbnail = imagecreatetruecolor($new_width, $new_height);

// Resize the original image to create the thumbnail
imagecopyresampled($thumbnail, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Save the thumbnail image
imagejpeg($thumbnail, 'path/to/thumbnail/image.jpg');

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