This article describes how to use PHP to process Exif [Exchangeable image file format] data.
Using the Exif extension in PHP
A2 Hosting's shared servers include PHP support for processing Exif data. The Exif PHP extension enables you to work with metadata from images taken by digital devices. For example, one common use of PHP's Exif functions is to process photographs taken by a digital camera. Depending on the image file format, you can also retrieve embedded thumbnails of images.
To extract all of the Exif headers from an image file, use the exif_read_data[] function. The following PHP code sample demonstrates how to do this for an image file named photograph.jpg:
$my_exif_data = exif_read_data[ 'photograph.jpg' ]; print_r[ $my_exif_data ];
More Information
For more information about Exif-related functions available in PHP, please visit //php.net/manual/en/book.exif.php.
[PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8]
exif_read_data — Reads the EXIF headers from an image file
Description
exif_read_data[
resource|string $file
,
?string
$required_sections
= null
,
bool $as_arrays
= false
,
bool $read_thumbnail
= false
]: array|false
EXIF headers tend to be present in JPEG/TIFF images generated by digital cameras, but unfortunately each digital camera maker has a different idea of how to actually tag their images, so you can't always rely on a specific Exif header being present.
Height
and Width
are computed the same way getimagesize[] does so their values must not be part of any header returned. Also, html
is a height/width text string to be used inside normal
HTML.
When an Exif header contains a Copyright note, this itself can contain two values. As the solution is inconsistent in the Exif 2.10 standard, the COMPUTED
section will return both entries Copyright.Photographer
and Copyright.Editor
while the IFD0
sections contains the byte array with the NULL character that splits both entries. Or just the first entry if the datatype was wrong [normal behaviour of Exif]. The COMPUTED
will also contain the entry
Copyright
which is either the original copyright string, or a comma separated list of the photo and editor copyright.
The tag UserComment
has the same problem as the Copyright tag. It can store two values. First the encoding used, and second the value itself. If so the IFD
section only contains the encoding or a byte array. The COMPUTED
section will store both in the entries UserCommentEncoding
and UserComment
. The entry UserComment
is available in both cases so it should be used in
preference to the value in IFD0
section.
exif_read_data[] also validates EXIF data tags according to the EXIF specification [» //exif.org/Exif2-2.PDF, page 20].
Parameters
file
The location of the image file. This can either be a path to the file [stream wrappers are also supported as usual] or a stream resource.
required_sections
Is a comma separated list of sections that need to be present in file to produce a result array. If none of the requested sections could be found the return value is false
.
FILE | FileName, FileSize, FileDateTime, SectionsFound |
COMPUTED | html, Width, Height, IsColor, and more if available. Height and Width are computed the same way getimagesize[] does so their values must not be part of any header returned. Also, html is a height/width text string to be used inside normal HTML.
|
ANY_TAG | Any information that has a Tag e.g. IFD0 , EXIF , ...
|
IFD0 | All tagged data of IFD0. In normal imagefiles this contains image size and so forth. |
THUMBNAIL | A file is supposed to contain a thumbnail if it has a second IFD . All tagged information about the embedded thumbnail is stored in this section.
|
COMMENT | Comment headers of JPEG images. |
EXIF | The EXIF section is a sub section of IFD0 . It contains more detailed information about an image. Most of these entries are digital camera related.
|
as_arrays
Specifies whether or not each section becomes an array. The required_sections
COMPUTED
, THUMBNAIL
, and COMMENT
always become arrays as they may contain values whose names conflict with other sections.
read_thumbnail
When set to true
the thumbnail itself is read. Otherwise, only the tagged data is read.
Return Values
It
returns an associative array where the array indexes are the header names and the array values are the values associated with those headers. If no data can be returned, exif_read_data[] will return false
.
Errors/Exceptions
Errors of level E_WARNING
and/or E_NOTICE
may be raised for unsupported tags or other potential error
conditions, but the function still tries to read all comprehensible information.
Changelog
8.0.0 | required_sections is nullable now.
|
7.2.0 | The file parameter now supports both local files and stream resources.
|
7.2.0 | Support for the following EXIF formats were added:
|
Examples
Example #1 exif_read_data[] example
The first call fails because the image has no header information.
The above example will output something similar to:
test1.jpg: No header data found. test2.jpg: FILE.FileName: test2.jpg FILE.FileDateTime: 1017666176 FILE.FileSize: 1240 FILE.FileType: 2 FILE.SectionsFound: ANY_TAG, IFD0, THUMBNAIL, COMMENT COMPUTED.html: width="1" height="1" COMPUTED.Height: 1 COMPUTED.Width: 1 COMPUTED.IsColor: 1 COMPUTED.ByteOrderMotorola: 1 COMPUTED.UserComment: Exif test image. COMPUTED.UserCommentEncoding: ASCII COMPUTED.Copyright: Photo [c] M.Boerger, Edited by M.Boerger. COMPUTED.Copyright.Photographer: Photo [c] M.Boerger COMPUTED.Copyright.Editor: Edited by M.Boerger. IFD0.Copyright: Photo [c] M.Boerger IFD0.UserComment: ASCII THUMBNAIL.JPEGInterchangeFormat: 134 THUMBNAIL.JPEGInterchangeFormatLength: 523 COMMENT.0: Comment #1. COMMENT.1: Comment #2. COMMENT.2: Comment #3end THUMBNAIL.JPEGInterchangeFormat: 134 THUMBNAIL.Thumbnail.Height: 1 THUMBNAIL.Thumbnail.Height: 1
Example #2 exif_read_data[] with streams available as of PHP 7.2.0
The above example will output something similar to:
EXIF Headers: Height => 576 Width => 1024 IsColor => 1 ByteOrderMotorola => 0 ApertureFNumber => f/5.6 UserComment => UserCommentEncoding => UNDEFINED Copyright => Denis Thumbnail.FileType => 2 Thumbnail.MimeType => image/jpeg
Notes
Note:
If mbstring is enabled, exif will attempt to process the unicode and pick a charset as specified by exif.decode_unicode_motorola and exif.decode_unicode_intel. The exif extension will not attempt to figure out the encoding on its own, and it is up to the user to properly specify the encoding for which to use for decoding by setting one of these two ini directives prior to calling exif_read_data[].
Note:
If the
file
is used to pass a stream to this function, then the stream must be seekable. Note that the file pointer position is not changed after this function returns.
See Also
- exif_thumbnail[] - Retrieve the embedded thumbnail of an image
- getimagesize[] - Get the size of an image
- Supported Protocols and Wrappers
chadsmith729 at gmail dot com ¶
9 years ago
When the new update came out from Apple for iOS6 it provided the ability for iPad, iPod, and iPhones to be able to upload files from the device through Safari. Obviously this will open up an array of implementations where at one point it was just not possible.
The issue comes when a photo is uploaded it will be dependent on the location of the "button" when the photo was taken. Imagine if you will that you have your iPhone turned with the button at the top and you take a photo. The photo when uploaded to your server might be "upside down".
The following code will ensure that all uploaded photos will be oriented correctly upon upload:
What you do with the image resource from there is entirely up to you.
I hope that this helps you identify and orient any image that's uploaded from an iPad, iPhone, or iPod. Orientation for the photo is the key to knowing how to rotate it correctly.
darkain at darkain dot com ¶
14 years ago
I wanted some quick and easy functions for computing the shutter speed and f-stop. I couldn't find any anywhere, so I made some. It took some research :
drpain at webster dot org dot za ¶
10 years ago
Please note that when resizing images with GD and most image processing scripts or applications you will loose the EXIF information. What I did as a workaround is book this information into MySQL before I re-size images.