How can one generate thumbnails of uploaded images in PHP?

To generate thumbnails of uploaded images in PHP, you can use the GD library to resize the image to a smaller size. This can be achieved by creating a new image resource with the desired dimensions, copying the uploaded image onto the new resource, and then saving the new image as a thumbnail.

// Set the maximum dimensions for the thumbnail
$maxWidth = 100;
$maxHeight = 100;

// Get the uploaded image
$uploadedImage = $_FILES['image']['tmp_name'];

// Create a new image resource from the uploaded image
$source = imagecreatefromjpeg($uploadedImage);

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

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

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

// Resize the uploaded image to fit the thumbnail dimensions
imagecopyresampled($thumbnail, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

// Save the thumbnail as a new image file
imagejpeg($thumbnail, 'thumbnails/thumbnail.jpg');

// Free up memory by destroying the image resources
imagedestroy($source);
imagedestroy($thumbnail);