How can the exif_read_data() function in PHP be utilized to address image rotation problems when uploading images from iPhones?

When uploading images from iPhones, the orientation of the image is often stored in the EXIF data. This can cause the image to appear rotated incorrectly when displayed on a website. To address this issue, the exif_read_data() function in PHP can be used to read the orientation information from the image and then rotate the image accordingly before displaying it.

$exif = exif_read_data($uploaded_image);
if(!empty($exif['Orientation'])) {
    $image = imagecreatefromjpeg($uploaded_image);
    switch($exif['Orientation']) {
        case 3:
            $image = imagerotate($image, 180, 0);
            break;
        case 6:
            $image = imagerotate($image, -90, 0);
            break;
        case 8:
            $image = imagerotate($image, 90, 0);
            break;
    }
    imagejpeg($image, $uploaded_image);
}