What are some potential issues that can arise when resizing images in PHP, especially when dealing with different aspect ratios?

When resizing images in PHP, a potential issue that can arise when dealing with different aspect ratios is distortion or stretching of the image. To solve this issue, you can maintain the aspect ratio of the original image by calculating the appropriate dimensions for resizing based on the aspect ratio.

function resize_image($image_path, $new_width, $new_height) {
    list($original_width, $original_height) = getimagesize($image_path);
    $original_aspect_ratio = $original_width / $original_height;
    
    if ($original_aspect_ratio >= 1) {
        $new_height = $new_width / $original_aspect_ratio;
    } else {
        $new_width = $new_height * $original_aspect_ratio;
    }

    $image = imagecreatefromjpeg($image_path);
    $resized_image = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
    
    return $resized_image;
}