What are the best practices for handling image resizing and quality preservation in PHP when generating thumbnails?

When generating thumbnails in PHP, it's important to use image manipulation functions like `imagecopyresampled()` to resize images while preserving quality. Additionally, you can set the image quality using `imagejpeg()` function to control the compression level. This ensures that the thumbnails are resized efficiently without sacrificing image quality.

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

// Get the dimensions of the original image
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);

// Calculate the new dimensions for the thumbnail
$thumbnail_width = 100;
$thumbnail_height = ($original_height / $original_width) * $thumbnail_width;

// Create a blank thumbnail image
$thumbnail_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

// Resize the original image to fit the thumbnail dimensions
imagecopyresampled($thumbnail_image, $original_image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $original_width, $original_height);

// Output the thumbnail image with specified quality
imagejpeg($thumbnail_image, 'thumbnail.jpg', 80);

// Free up memory
imagedestroy($original_image);
imagedestroy($thumbnail_image);