What are common issues when generating thumbnails in PHP and how can they be resolved?

Issue: Common issues when generating thumbnails in PHP include image distortion, incorrect aspect ratio, and poor image quality. These issues can be resolved by using PHP libraries like GD or Imagick to resize and crop images while maintaining the aspect ratio.

// Example code using GD library to generate thumbnails without distortion

// 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);

// Set the desired thumbnail width and height
$thumbnail_width = 100;
$thumbnail_height = 100;

// Create a new image with the desired dimensions
$thumbnail_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

// Resize and crop 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
header('Content-Type: image/jpeg');
imagejpeg($thumbnail_image);

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