How can I create a thumbnail from an uploaded image in PHP?

To create a thumbnail from an uploaded image in PHP, you can use the GD library to resize the image and save it as a new thumbnail. First, you need to upload the image using PHP and then use GD functions to create a thumbnail by resizing the image to a smaller size while maintaining the aspect ratio.

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

// Create a thumbnail
$thumbnailWidth = 100;
$thumbnailHeight = 100;

$source = imagecreatefromjpeg($uploadedImage);
$thumbnail = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);

imagecopyresampled($thumbnail, $source, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, imagesx($source), imagesy($source));

// Save the thumbnail
$thumbnailPath = 'thumbnails/thumbnail.jpg';
imagejpeg($thumbnail, $thumbnailPath);

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

echo 'Thumbnail created successfully!';