What potential issues can arise when creating thumbnails using the GD-Lib in PHP?

One potential issue that can arise when creating thumbnails using the GD-Lib in PHP is that the aspect ratio of the original image may not be maintained, leading to distorted thumbnails. To solve this issue, you can calculate the appropriate dimensions for the thumbnail while maintaining the aspect ratio of the original image.

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

// Get the original image dimensions
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);

// Calculate the thumbnail dimensions while maintaining aspect ratio
$thumbnailWidth = 100; // desired width
$thumbnailHeight = floor($originalHeight * ($thumbnailWidth / $originalWidth));

// Create a blank thumbnail image
$thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);

// Resize and copy the original image to the thumbnail image
imagecopyresampled($thumbnailImage, $originalImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $originalWidth, $originalHeight);

// Save the thumbnail image
imagejpeg($thumbnailImage, 'thumbnail.jpg');

// Free up memory
imagedestroy($originalImage);
imagedestroy($thumbnailImage);