Defining EXIF ​​Orientation and Image Rotation with ImageMagick

Canon DSLRs appear to save photos in landscape orientation and use exif::orientation for rotation.

Question: How can I use imagemagick to re-save the image in the intended orientation using the exif orientation data so that it is no longer necessary to display the exif data in the correct orientation?

+54
php imagemagick exif
Oct 18 '13 at 17:50
source share
2 answers

To do this, use the ImageMagick convert auto orientation option.

 convert your-image.jpg -auto-orient output.jpg 

Or use mogrify to do it in place

 mogrify -auto-orient your-image.jpg 
+88
Oct 20 '13 at 8:17
source share

The Imagick PHP method will check the image orientation and rotate / flip the image accordingly:

 function autorotate(Imagick $image) { 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); return $image; } 

The function can be used as follows:

 $img = new Imagick('/path/to/file'); autorotate($img); $img->stripImage(); // if you want to get rid of all EXIF data $img->writeImage(); 
+36
Aug 11 '15 at 14:00
source share



All Articles