What potential issues can arise when resizing images using PHP, especially when the new width is smaller than the new height?

When resizing images using PHP, especially when the new width is smaller than the new height, potential issues can arise with aspect ratio distortion or image stretching. To solve this issue, you can calculate the aspect ratio of the original image and then resize the image while maintaining the aspect ratio to avoid distortion.

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

// 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 width and height based on the aspect ratio
$new_width = 200; // new width
$new_height = $new_width / $aspect_ratio; // calculate new height

// Create a new image with the calculated dimensions
$resized_image = imagecreatetruecolor($new_width, $new_height);

// Resize the original image to the new dimensions
imagecopyresampled($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

// Save or output the resized image
imagejpeg($resized_image, 'resized.jpg');

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