What is the role of EXIF data in determining image orientation and how can it be manipulated in PHP?

EXIF data contains information about the orientation of an image, which can be used to determine the correct orientation for display. In PHP, you can use the `exif_read_data()` function to read the EXIF data of an image and then use the `imagetruecolortopalette()` function to manipulate the image orientation accordingly.

// Read the EXIF data of the image
$exif = exif_read_data('image.jpg');

// Determine the orientation of the image
$orientation = $exif['Orientation'];

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

// Rotate the image based on the orientation
switch ($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;
}

// Save the manipulated image
imagejpeg($image, 'image_oriented.jpg');

// Free up memory
imagedestroy($image);