PHP: How to fix Image rotation issue in img tag because of EXIF?
Photos captured by some smartphones especially iPhones are automatically rotated when we use it in <img> tag which is mostly not acceptable as we don’t need such unwanted rotation. This happens because of EXIF Data stored in that image. But we can easily fix it using PHP by following these steps:
function autorotate($src) { // check if extension exists or not if(extension_loaded('imagick')) { try { $image= new Imagick($src); switch ($image->getImageOrientation()) { case Imagick::ORIENTATION_TOPLEFT: break; case Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); break; case Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage("#000", 180); break; case Imagick::ORIENTATION_BOTTOMLEFT: $image->flopImage(); $image->rotateImage("#000", 180); break; case Imagick::ORIENTATION_LEFTTOP: $image->flopImage(); $image->rotateImage("#000", -90); break; case Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage("#000", 90); break; case Imagick::ORIENTATION_RIGHTBOTTOM: $image->flopImage(); $image->rotateImage("#000", 90); break; case Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage("#000", -90); break; default: // Invalid orientation break; } $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); $image->stripImage(); // if you want to get rid of all EXIF data $image->writeImage(); $image->clear(); $image->destroy(); return $image; } catch(Exception $e) { return 'Exception caught: ', $e->getMessage(), " "; } } else { return "Imagick extension is not installed."; } } // now run autorotate() function with source of file as parameter autorotate('my_img.jpg');
1function autorotate($src)
2{
3// check if extension exists or not
4if(extension_loaded('imagick'))
5{
6 try
7 {
8 $image= new Imagick($src);
9 switch ($image->getImageOrientation()) {
10 case Imagick::ORIENTATION_TOPLEFT:
11 break;
12 case Imagick::ORIENTATION_TOPRIGHT:
13 $image->flopImage();
14 break;
15 case Imagick::ORIENTATION_BOTTOMRIGHT:
16 $image->rotateImage("#000", 180);
17 break;
18 case Imagick::ORIENTATION_BOTTOMLEFT:
19 $image->flopImage();
20 $image->rotateImage("#000", 180);
21 break;
22 case Imagick::ORIENTATION_LEFTTOP:
23 $image->flopImage();
24 $image->rotateImage("#000", -90);
25 break;
26 case Imagick::ORIENTATION_RIGHTTOP:
27 $image->rotateImage("#000", 90);
28 break;
29 case Imagick::ORIENTATION_RIGHTBOTTOM:
30 $image->flopImage();
31 $image->rotateImage("#000", 90);
32 break;
33 case Imagick::ORIENTATION_LEFTBOTTOM:
34 $image->rotateImage("#000", -90);
35 break;
36 default: // Invalid orientation
37 break;
38 }
39 $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
40
41 $image->stripImage(); // if you want to get rid of all EXIF data
42 $image->writeImage();
43 $image->clear();
44 $image->destroy();
45 return $image;
46
47 }
48 catch(Exception $e) {
49 return 'Exception caught: ', $e->getMessage(), "
50";
51 }
52 }
53 else
54 {
55 return "Imagick extension is not installed.";
56 }
57}
58
59
60// now run autorotate() function with source of file as parameter
61autorotate('my_img.jpg');
Or you can also fix it with just one line if you have root access to your linux server. Just run following shell command:
convert image.jpg -auto-orient output.jpg
1convert image.jpg -auto-orient output.jpg