Detect EXIF Orientation and Rotate Image using ImageMagick
Camera manufacturers, including Canon DSLRs, frequently write images in a way that defies immediate visual inspection. A photo taken in portrait mode often appears landscape-oriented when opened directly in an editor because the exif::orientation tag dictates that the viewer should rotate the image 90 degrees clockwise before rendering it. This is a standard practice to save storage space and bandwidth, but it creates a common headache for web developers: images appear sideways on the page until the browser or gallery software honors the metadata.
The goal here is to detect that orientation flag and bake the rotation directly into the image pixels, stripping the EXIF data so the file displays correctly regardless of viewer settings. While modern browsers handle this natively via CSS transforms based on EXIF data, relying solely on metadata is fragile. If you need to deliver images to systems that ignore EXIF tagsâor if you want to reduce file size by removing unnecessary metadataâyou must physically rotate the bitmap.
Detecting the Orientation Flag
Before touching the image data, you need to know what rotation is required. ImageMagick provides a robust way to inspect this without altering the file. You can use identify to query specific EXIF tags. The tag for orientation is Orientation.
Run the following command to see the current state:
identify -format "%[EXIF:Orientation]" input.jpg
If the output returns 1, the image is already oriented correctly. If it returns 2, 3, 4, 5, 6, or 8, you need to apply a rotation. These values correspond to specific degrees and directions:
- 1: Top-left (no rotation)
- 2: Rotate 90° clockwise
- 3: Flip vertically
- 4: Rotate 90° counter-clockwise
- 5: Flip horizontal then rotate 90° counter-clockwise
- 6: Rotate 270° clockwise (or 90° counter-clockwise)
- 8: Flip horizontally
Rotating the Image with mogrify
Once you have identified a non-standard orientation, use mogrify to process the file in place or save it to a new path. The core operation relies on the -rotate flag combined with logic that interprets the EXIF value.
ImageMagick does not have a single "fix EXIF orientation" switch that automatically reads the tag and rotates; you must map the EXIF value to the correct rotation degrees manually or via a script. Here is a robust bash snippet that handles the mapping:
#!/bin/bash
INPUT="input.jpg"
OUTPUT="output.jpg"
ORIENTATION=$(identify -format "%[EXIF:Orientation]" "$INPUT")
case $ORIENTATION in
2) ROTATION="-rotate 90" ;;
3) ROTATION="-flop -flip" ;; # Or -rotate 180 depending on interpretation, usually flip vertical
4) ROTATION="-rotate 270" ;;
5) ROTATION="-flop -rotate 90" ;;
6) ROTATION="-rotate 270" ;;
8) ROTATION="-flop -rotate 90" ;;
*) echo "No rotation needed or unknown tag"; exit 1 ;;
esac
# Apply rotation and strip EXIF data to ensure clean output
convert "$INPUT" $ROTATION \
-strip \
-quality 85 \
"$OUTPUT"
Note the -strip flag. This removes all metadata, including the Orientation tag. If you omit this, the new image will still be written as landscape with no rotation instructions, and the viewer won't know to rotate it back (because you already rotated the pixels). The combination of pixel manipulation and stripping metadata ensures the image is "baked" into the correct orientation.
Handling Edge Cases and Trade-offs
There is a subtle but critical distinction between rotating pixels and flipping them. While -rotate handles 90-degree shifts cleanly, values like 3 (flipped vertically) or 5/8 (flips combined with rotation) require careful handling. Using -flop followed by -flip or combining them with rotation ensures the geometry matches the EXIF intent exactly.
Additionally, consider the quality of your source image. Rotating an image can sometimes introduce interpolation artifacts if the original was compressed heavily. If you are processing high-volume uploads in a Laravel application, ensure your storage path is writable and that you aren't inadvertently storing the intermediate rotated files.
If you prefer to keep the EXIF data intact for compatibility with older software that relies on it, you can skip the -strip flag. However, this defeats the primary purpose of fixing the orientation for web delivery: ensuring universal compatibility without relying on browser-side CSS transforms or metadata interpretation.
For production environments where this logic is automated, integrating this into a queue worker ensures that every uploaded asset is normalized immediately upon receipt. This prevents downstream issues in galleries, SEO image sitemaps, and thumbnail generation pipelines.