Detect EXIF Orientation and Rotate Image using ImageMagick
Use the auto-orient option of ImageMagick's convert
to do this.
convert your-image.jpg -auto-orient output.jpg
Or use mogrify
to do it in place
mogrify -auto-orient your-image.jpg
Setting image orientation exif data using imagemagick
Not sure if/how you can do that with ImageMagick, but you might like to use exiftool as follows:
# Set orientation with exiftool
exiftool -Orientation=3 -n input.jpg
1 image files updated
# Check with **ImageMagick**
identify -verbose input.jpg | grep rient
Orientation: BottomRight
exif:Orientation: 3
Here is a little loop to test all 8 values:
for x in {1..8}; do
exiftool -Orientation=$x -n input.jpg > /dev/null
identify -verbose input.jpg | grep Orientation
done
Orientation: TopLeft
exif:Orientation: 1
Orientation: TopRight
exif:Orientation: 2
Orientation: BottomRight
exif:Orientation: 3
Orientation: BottomLeft
exif:Orientation: 4
Orientation: LeftTop
exif:Orientation: 5
Orientation: RightTop
exif:Orientation: 6
Orientation: RightBottom
exif:Orientation: 7
Orientation: LeftBottom
exif:Orientation: 8
imagemagick convert: how to tell if images need to be rotated?
It's true that you will have to look for orientation value reading Exif metadata of Image. Using imagemagick you can get this value by
identify -format '%[exif:orientation]' <Path to Image>
//or by using
identify -format '%[orientation]' <Path to Image>
Exif Orientation values range from 1 to 8 and maps to orientation as below:
- 1 = Horizontal (normal)
- 2 = Mirror horizontal
- 3 = Rotate 180
- 4 = Mirror vertical
- 5 = Mirror horizontal and rotate 270 CW
- 6 = Rotate 90 CW
- 7 = Mirror horizontal and rotate 90 CW
- 8 = Rotate 270 CW
Thus, image would require orientation correction if this metadata tag returns with a non 1 value. If this tag returns with no value it can be assumed to be having normal orientation.
To correct orientation to normal you can use following command
convert -auto-orient <inputImagePath> <outputImagePath>
How can I display the orientation of a JPEG file?
You can use
identify -format '%[EXIF:Orientation]' <image.jpg>
as per identify -format
docs (It's the bit further down about exif metadata).
Try
identify -verbose <image.jpg>
To see what metadata is in the image (for example if the image was not taken with a camera, the orientation tag will not be set).
Alternatively you could do something like
identify -format '%wx%h' <image.jpg>
which gives you the width by height (e.g. '800x598', '1936x2592') and use these to determine whether the image is upright or not (not sure how reliable this is though - sometimes you take a portrait image with a camera and the EXIF data will correctly record the orientation, but the image may still appear landscape).
ImageMagick do not rotate image according to EXIF
This is a bug in ImageMagick and will be fixed in 6.8.8-2. The bug report can be found here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=24749
How to stop PHP iMagick auto-rotating images based on EXIF 'orientation' data
Try Imagick::setImageOrientation
. Experiment with the available constants.
Exif: I can't calculate the orientation of the images
As you didn't share your code, it is hard to say what is wrong. Here's a way to work it out though:
# Make a simple 1x1 black starting image with ImageMagick
magick xc:black start.jpg
# Make a copy with orientation=3 in "3.jpg"
exiftool -orientation#=3 start.jpg -o 3.jpg
# Analyse
exiftool -v -v -v 3.jpg
ExifToolVersion = 12.00
FileName = 3.jpg
Directory = .
FileSize = 260
FileModifyDate = 1632816155
FileAccessDate = 1632816155
FileInodeChangeDate = 1632816155
FilePermissions = 33188
FileType = JPEG
FileTypeExtension = JPG
MIMEType = image/jpeg
JPEG APP0 (14 bytes):
0006: 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 [JFIF..........]
+ [BinaryData directory, 9 bytes]
| JFIFVersion = 1 1
| - Tag 0x0000 (2 bytes, int8u[2]):
| 000b: 01 01 [..]
| ResolutionUnit = 0
| - Tag 0x0002 (1 bytes, int8u[1]):
| 000d: 00 [.]
| XResolution = 1
| - Tag 0x0003 (2 bytes, int16u[1]):
| 000e: 00 01 [..]
| YResolution = 1
| - Tag 0x0005 (2 bytes, int16u[1]):
| 0010: 00 01 [..]
| ThumbnailWidth = 0
| - Tag 0x0007 (1 bytes, int8u[1]):
| 0012: 00 [.]
| ThumbnailHeight = 0
| - Tag 0x0008 (1 bytes, int8u[1]):
| 0013: 00 [.]
JPEG APP1 (96 bytes):
0018: 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 05 [Exif..MM.*......]
0028: 01 12 00 03 00 00 00 01 00 03 00 00 01 1a 00 05 [................]
0038: 00 00 00 01 00 00 00 4a 01 1b 00 05 00 00 00 01 [.......J........]
0048: 00 00 00 52 01 28 00 03 00 00 00 01 00 01 00 00 [...R.(..........]
0058: 02 13 00 03 00 00 00 01 00 01 00 00 00 00 00 00 [................]
0068: 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 [................]
ExifByteOrder = MM
+ [IFD0 directory with 5 entries]
| 0) Orientation = 3
| - Tag 0x0112 (2 bytes, int16u[1]):
| 0030: 00 03 [..]
| 1) XResolution = 1 (1/1)
| - Tag 0x011a (8 bytes, rational64u[1]):
| 0068: 00 00 00 01 00 00 00 01 [........]
| 2) YResolution = 1 (1/1)
| - Tag 0x011b (8 bytes, rational64u[1]):
| 0070: 00 00 00 01 00 00 00 01 [........]
| 3) ResolutionUnit = 1
| - Tag 0x0128 (2 bytes, int16u[1]):
| 0054: 00 01 [..]
| 4) YCbCrPositioning = 1
| - Tag 0x0213 (2 bytes, int16u[1]):
| 0060: 00 01 [..]
JPEG DQT (65 bytes):
007c: 00 03 02 02 02 02 02 03 02 02 02 03 03 03 03 04 [................]
008c: 06 04 04 04 04 04 08 06 06 05 06 09 08 0a 0a 09 [................]
009c: 08 09 09 0a 0c 0f 0c 0a 0b 0e 0b 09 09 0d 11 0d [................]
00ac: 0e 0f 10 10 11 10 0a 0c 12 13 12 10 13 0f 10 10 [................]
00bc: 10 [.]
JPEG SOF0 (9 bytes):
00c1: 08 00 01 00 01 01 01 11 00 [.........]
ImageWidth = 1
ImageHeight = 1
EncodingProcess = 0
BitsPerSample = 8
ColorComponents = 1
JPEG DHT (18 bytes):
00ce: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [................]
00de: 00 09 [..]
JPEG DHT (18 bytes):
00e4: 10 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [................]
00f4: 00 00 [..]
JPEG SOS
JPEG EOI
# Dump
xxd 3.jpg
00000000: ffd8 ffe0 0010 4a46 4946 0001 0100 0001 ......JFIF......
00000010: 0001 0000 ffe1 0062 4578 6966 0000 4d4d .......bExif..MM
00000020: 002a 0000 0008 0005 0112 0003 0000 0001 .*..............
00000030: 0003 0000 011a 0005 0000 0001 0000 004a ...............J
00000040: 011b 0005 0000 0001 0000 0052 0128 0003 ...........R.(..
00000050: 0000 0001 0001 0000 0213 0003 0000 0001 ................
00000060: 0001 0000 0000 0000 0000 0001 0000 0001 ................
00000070: 0000 0001 0000 0001 ffdb 0043 0003 0202 ...........C....
00000080: 0202 0203 0202 0203 0303 0304 0604 0404 ................
00000090: 0404 0806 0605 0609 080a 0a09 0809 090a ................
000000a0: 0c0f 0c0a 0b0e 0b09 090d 110d 0e0f 1010 ................
000000b0: 1110 0a0c 1213 1210 130f 1010 10ff c000 ................
000000c0: 0b08 0001 0001 0101 1100 ffc4 0014 0001 ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0009 ................
000000e0: ffc4 0014 1001 0000 0000 0000 0000 0000 ................
000000f0: 0000 0000 0000 ffda 0008 0101 0000 3f00 ..............?.
00000100: 2a9f ffd9 *...
# Make a copy with orientation=5 in "5.jpg"
exiftool -orientation#=5 start.jpg -o 5.jpg
# Analyse
exiftool -v -v -v 5.jpg
ExifToolVersion = 12.00
FileName = 5.jpg
Directory = .
FileSize = 260
FileModifyDate = 1632816155
FileAccessDate = 1632816155
FileInodeChangeDate = 1632816155
FilePermissions = 33188
FileType = JPEG
FileTypeExtension = JPG
MIMEType = image/jpeg
JPEG APP0 (14 bytes):
0006: 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 [JFIF..........]
+ [BinaryData directory, 9 bytes]
| JFIFVersion = 1 1
| - Tag 0x0000 (2 bytes, int8u[2]):
| 000b: 01 01 [..]
| ResolutionUnit = 0
| - Tag 0x0002 (1 bytes, int8u[1]):
| 000d: 00 [.]
| XResolution = 1
| - Tag 0x0003 (2 bytes, int16u[1]):
| 000e: 00 01 [..]
| YResolution = 1
| - Tag 0x0005 (2 bytes, int16u[1]):
| 0010: 00 01 [..]
| ThumbnailWidth = 0
| - Tag 0x0007 (1 bytes, int8u[1]):
| 0012: 00 [.]
| ThumbnailHeight = 0
| - Tag 0x0008 (1 bytes, int8u[1]):
| 0013: 00 [.]
JPEG APP1 (96 bytes):
0018: 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 05 [Exif..MM.*......]
0028: 01 12 00 03 00 00 00 01 00 05 00 00 01 1a 00 05 [................]
0038: 00 00 00 01 00 00 00 4a 01 1b 00 05 00 00 00 01 [.......J........]
0048: 00 00 00 52 01 28 00 03 00 00 00 01 00 01 00 00 [...R.(..........]
0058: 02 13 00 03 00 00 00 01 00 01 00 00 00 00 00 00 [................]
0068: 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 [................]
ExifByteOrder = MM
+ [IFD0 directory with 5 entries]
| 0) Orientation = 5
| - Tag 0x0112 (2 bytes, int16u[1]):
| 0030: 00 05 [..]
| 1) XResolution = 1 (1/1)
| - Tag 0x011a (8 bytes, rational64u[1]):
| 0068: 00 00 00 01 00 00 00 01 [........]
| 2) YResolution = 1 (1/1)
| - Tag 0x011b (8 bytes, rational64u[1]):
| 0070: 00 00 00 01 00 00 00 01 [........]
| 3) ResolutionUnit = 1
| - Tag 0x0128 (2 bytes, int16u[1]):
| 0054: 00 01 [..]
| 4) YCbCrPositioning = 1
| - Tag 0x0213 (2 bytes, int16u[1]):
| 0060: 00 01 [..]
JPEG DQT (65 bytes):
007c: 00 03 02 02 02 02 02 03 02 02 02 03 03 03 03 04 [................]
008c: 06 04 04 04 04 04 08 06 06 05 06 09 08 0a 0a 09 [................]
009c: 08 09 09 0a 0c 0f 0c 0a 0b 0e 0b 09 09 0d 11 0d [................]
00ac: 0e 0f 10 10 11 10 0a 0c 12 13 12 10 13 0f 10 10 [................]
00bc: 10 [.]
JPEG SOF0 (9 bytes):
00c1: 08 00 01 00 01 01 01 11 00 [.........]
ImageWidth = 1
ImageHeight = 1
EncodingProcess = 0
BitsPerSample = 8
ColorComponents = 1
JPEG DHT (18 bytes):
00ce: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [................]
00de: 00 09 [..]
JPEG DHT (18 bytes):
00e4: 10 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [................]
00f4: 00 00 [..]
JPEG SOS
JPEG EOI
# Dump
xxd 5.jpg
00000000: ffd8 ffe0 0010 4a46 4946 0001 0100 0001 ......JFIF......
00000010: 0001 0000 ffe1 0062 4578 6966 0000 4d4d .......bExif..MM
00000020: 002a 0000 0008 0005 0112 0003 0000 0001 .*..............
00000030: 0005 0000 011a 0005 0000 0001 0000 004a ...............J
00000040: 011b 0005 0000 0001 0000 0052 0128 0003 ...........R.(..
00000050: 0000 0001 0001 0000 0213 0003 0000 0001 ................
00000060: 0001 0000 0000 0000 0000 0001 0000 0001 ................
00000070: 0000 0001 0000 0001 ffdb 0043 0003 0202 ...........C....
00000080: 0202 0203 0202 0203 0303 0304 0604 0404 ................
00000090: 0404 0806 0605 0609 080a 0a09 0809 090a ................
000000a0: 0c0f 0c0a 0b0e 0b09 090d 110d 0e0f 1010 ................
000000b0: 1110 0a0c 1213 1210 130f 1010 10ff c000 ................
000000c0: 0b08 0001 0001 0101 1100 ffc4 0014 0001 ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0009 ................
000000e0: ffc4 0014 1001 0000 0000 0000 0000 0000 ................
000000f0: 0000 0000 0000 ffda 0008 0101 0000 3f00 ..............?.
00000100: 2a9f ffd9 *...
Difference the two:
Related Topics
Data from Two Tables with Same Column Names
How to Change the Woocommerce_Form_Field HTML Structure
How to Dynamically Populate Options on Dropdown Lists Based on Selection in Another Dropdown
Dynamically Generating a Qr Code with PHP
How to Get a One-Dimensional Scalar Array as a Doctrine Dql Query Result
How to Run a Cronjob Every X Minutes
"Adaptive Server Is Unavailable or Does Not Exist" Error Connecting to SQL Server from PHP
PHP Remove All Characters Before Specific String
Double Results in My Array ( MySQL_Fetch_Array )
Warning: MySQL_Result() Expects Parameter 1 to Be Resource, Boolean Given
How to Do Multiple SQL Statements in One MySQL_Query
MySQL and PHP - Insert Null Rather Than Empty String
How to Execute Raw Queries with Laravel 5.1
Create an Order Programmatically with Line Items in Woocommerce 3+