Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect shot angle of photo, and auto rotate for website display like desktop apps do on viewing?

Tags:

If I take a photo with a camera it stores the orientation/angle of the apparatus so when I view the image on the PC with a good app, it shows auto-rotated to 0.

But when I upload to a website it shows the original angle, so the image doesn't look good.

How can I detect this with PHP and rotate the image, and clear this angle flag from it's meta information.

like image 433
Pentium10 Avatar asked Sep 07 '10 08:09

Pentium10


People also ask

How do I rotate a picture in a certain angle?

1. With your image open in Photoshop, go to Image > Image Rotation. 2. Select from the image rotation options — 90 degrees clockwise, 90 degrees counterclockwise, or 180 degrees.

How do you fix a digital picture that is sideways?

Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it.

How do I rotate my screen without auto rotate?

Short guide: Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.


1 Answers

In order to do that, you must read the EXIF information out of the JPEG file. You can either do that with exif PHP extension or with PEL.

Basically, you have to read the Orientation flag in the file. Here is an example using the exif PHP extension and WideImage for image manipulation.

<?php $exif = exif_read_data($filename); $ort = $exif['Orientation'];  $image = WideImage::load($filename);  // GD doesn't support EXIF, so all information is removed. $image->exifOrient($ort)->saveToFile($filename);  class WideImage_Operation_ExifOrient {   /**    * Rotates and mirrors and image properly based on current orientation value    *    * @param WideImage_Image $img    * @param int $orientation    * @return WideImage_Image    */   function execute($img, $orientation)   {     switch ($orientation) {       case 2:         return $img->mirror();         break;        case 3:         return $img->rotate(180);         break;        case 4:         return $img->rotate(180)->mirror();         break;        case 5:         return $img->rotate(90)->mirror();         break;        case 6:         return $img->rotate(90);         break;        case 7:         return $img->rotate(-90)->mirror();         break;        case 8:         return $img->rotate(-90);         break;        default: return $img->copy();     }   } } 
like image 52
Andrew Moore Avatar answered Sep 29 '22 19:09

Andrew Moore