Last updated: August 28, 2026.
Phone cameras may store pixels in one direction and the intended display orientation in EXIF. Normalize the pixels once so every later derivative is correct.
Normalize common orientations
<?php
$image = imagecreatefromjpeg(__DIR__ . '/upload.jpg');
if ($image === false) throw new RuntimeException('Invalid JPEG.');
$exif = function_exists('exif_read_data') ? @exif_read_data(__DIR__ . '/upload.jpg') : false;
$orientation = (int) ($exif['Orientation'] ?? 1);
$degrees = [3 => 180, 6 => -90, 8 => 90][$orientation] ?? 0;
if ($degrees !== 0) { $rotated = imagerotate($image, $degrees, 0); imagedestroy($image); $image = $rotated; }
imagejpeg($image, __DIR__ . '/normalized.jpg', 88);
imagedestroy($image);Handle the full input set
- Add mirrored orientations 2, 4, 5, and 7 if required.
- Save a normalized derivative for later processing.
- Retain the original when metadata is important.
- Test portrait and landscape phone photos.
Reference: PHP exif_read_data reference.