What common issues can arise when using the imagecopyresized function in PHP?
One common issue that can arise when using the imagecopyresized function in PHP is that the resized image may appear distorted or stretched if the aspect ratio is not maintained. To solve this issue, you can calculate the aspect ratio of the original image and use it to resize the image proportionally.
// Calculate aspect ratio of original image
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
$aspect_ratio = $original_width / $original_height;
// Calculate new dimensions based on aspect ratio
$new_width = 200; // Set desired width
$new_height = $new_width / $aspect_ratio;
// Resize image proportionally using imagecopyresized
$resized_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
// Output resized image
header('Content-Type: image/jpeg');
imagejpeg($resized_image);
imagedestroy($resized_image);