What are some potential pitfalls when creating thumbnails from cropped images in PHP?

One potential pitfall when creating thumbnails from cropped images in PHP is that the aspect ratio of the thumbnail may not match the original image, leading to distortion. To solve this issue, you can calculate the aspect ratio of the original image and adjust the dimensions of the thumbnail accordingly to maintain the correct aspect ratio.

// Calculate aspect ratio of original image
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);
$aspectRatio = $originalWidth / $originalHeight;

// Calculate dimensions for thumbnail while maintaining aspect ratio
$thumbnailWidth = 100; // desired width of thumbnail
$thumbnailHeight = $thumbnailWidth / $aspectRatio;

// Create thumbnail image with correct aspect ratio
$thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresampled($thumbnailImage, $originalImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $originalWidth, $originalHeight);

// Save or output thumbnail image