What are the potential pitfalls of using imagecopyresized in PHP for creating image thumbnails or cropped images?

One potential pitfall of using imagecopyresized in PHP for creating image thumbnails or cropped images is that it can result in distorted or blurry images if not used properly. To avoid this, it is important to carefully calculate the dimensions of the thumbnail or cropped image and maintain the aspect ratio of the original image.

// Calculate the dimensions for the thumbnail or cropped image
$thumbnail_width = 100; // desired width
$thumbnail_height = 100; // desired height

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

// Calculate the aspect ratio
$aspect_ratio = $original_width / $original_height;

// Calculate the new dimensions while maintaining the aspect ratio
if ($original_width > $original_height) {
    $thumbnail_height = $thumbnail_width / $aspect_ratio;
} else {
    $thumbnail_width = $thumbnail_height * $aspect_ratio;
}

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

// Copy and resize the original image to the thumbnail image
imagecopyresampled($thumbnail_image, $original_image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $original_width, $original_height);

// Output the thumbnail image
imagejpeg($thumbnail_image, 'thumbnail.jpg');