How can the PHP function imagerotate be utilized to correct image orientation based on EXIF data?
When images are uploaded from devices such as smartphones, the orientation may not be correct due to EXIF data. To correct this issue, the PHP function imagerotate can be used to adjust the image orientation based on the EXIF data. By reading the orientation information from the EXIF data of the image, we can determine the necessary rotation angle and apply it using imagerotate.
// Load the image file
$image = imagecreatefromjpeg('input.jpg');
// Get the EXIF data of the image
$exif = exif_read_data('input.jpg');
// Determine the orientation from the EXIF data
$orientation = $exif['Orientation'];
// 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 corrected image
imagejpeg($image, 'output.jpg');
// Free up memory
imagedestroy($image);