What role does PHP EXIF orientation play in correctly displaying images after resizing?
When resizing images in PHP, the EXIF orientation metadata must be taken into account to correctly display the images. This metadata specifies the orientation of the image and needs to be applied during resizing to ensure the image is displayed correctly. Failure to consider the EXIF orientation can result in images being displayed sideways or upside down.
// Load the image
$image = imagecreatefromjpeg('image.jpg');
// Get the EXIF orientation
$exif = exif_read_data('image.jpg');
$orientation = isset($exif['Orientation']) ? $exif['Orientation'] : 0;
// 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;
}
// Resize the image
$newWidth = 200;
$newHeight = 150;
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image));
// Display the resized image
header('Content-Type: image/jpeg');
imagejpeg($resizedImage);
// Clean up
imagedestroy($image);
imagedestroy($resizedImage);
Keywords
Related Questions
- What are the potential pitfalls of using global variables within a PHP class?
- What potential pitfalls should be considered when using date functions in PHP to change images at specific times?
- What are the advantages and disadvantages of using floats versus text-align in styling PHP-generated content?