Fixing Image Orientation using its Exif data
Sometimes photos are wrongly taken, eg: upside down, sideways etc, and most newbie users don't know how to rotate image, so they might just upload image "as it is". This might cause the problem later, unless you have some kind of image rotate program to set it right. Another way to fix this problem is using image EXIF data. Thankfully all new digital cameras have an orientation sensor, which sets the orientation flag in EXIF headers and can be read using PHP exif_read_data().PHP
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
//read EXIF header from uploaded file
$exif = exif_read_data($_FILES['ImageFile']['tmp_name']);
//fix the Orientation if EXIF data exist
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$createdImage = imagerotate($image,90,0);
break;
case 3:
$createdImage = imagerotate($image,180,0);
break;
case 6:
$createdImage = imagerotate($image,-90,0);
break;
}
}