Leave That Thing Alone Blog

Viewing By Entry / Main
18 Jul. 2007

New in ColdFusion 8 is the cfimage tag and several functions specific to metadata contained in digital images. Here are some basic examples on how to the new ColdFusion 8 Exif and IPTC tags for digital photos, also several things to be careful about when using these tags.

ColdFusion 8 now has tags for getting Exif and IPTC metadata directly from your digital photos. The new tags are:

  • ImageGetEXIFTag() - Returns the specified Exif tag in an image.
  • ImageGetExifMetadata() - Returns a structure of all available Exif tags
  • ImageGetIPTCTag - Returns the specified IPTC tag for an image.
  • ImageGetIPTCMetadata() - Returns a structure of all available IPTC tags

So what are EXIF and IPTC? Exif metadata is the information in a digital image that describes the settings of the camera that took the photo. For example 'Shutter speed', 'aperature', and 'ISO'. Exif data is added to the digital image by a camera when the photo is taken.
IPTC metadata contains user added information like 'author' or 'copyright' of the digital image. This information is generally added after an image has been taken. Unfortunately ColdFusion 8 does not allow you to edit or add IPTC metadata.

How are these tags used?

Example of getting all Exif Metadata from a digital image:


<cfimage action="read" source="IMG_7654.JPG" name="myImage" />    

<cfset exif = ImageGetEXIFMetadata(myImage) />

<cfdump var="#exif#" label="Exif Metadata">

Output:
coldfusion 8 exif metadata

Example of how to display an individual Exif tag:

<cfimage action="read" source="IMG_7654.JPG" name="myImage" />    

<cfset model = ImageGetEXIFTag(myImage,"model") />

<cfoutput>#model#</cfoutput>

Output:
Canon 30D

Example of getting all IPTC Metadata from a digital image:

<cfimage action="read" name="myImage" source="img_7654.jpg">    

<cfset IPTC = imageGetIPTCMetadata(myImage) />

<cfdump var="#IPTC#">

In this case the IPTC data was added using Adobe Bridge, I labeled the fields as they are labeled in Bridge. This will show that that the IPTC tag names don't always match the tags in the tool the author used to add them. Adobe Bridge (CS2) has 30 IPTC fields, ColdFusion only retrieves 19 of them:

Output:
ColdFusion IPTC metadata

Some things to be careful/aware about:

  • Not all digital cameras write the same Exif metadata tags, so don't count on a particular tag existing. For example "Shutter Speed" may exist in a image taken with one digital camera but not in a different camera
  • ImageGetEXIFTag() and ImageGetIPTCTag() return a null if a requested tag does not exist. This means if you set a variable to this result value you may get an error if you try to evaluate it.
  • Date formats in Exif are generally stored in a format not formattable with the CF DateFormat() function.
  • Just like most other image manipulation programs if you save a new version of an image the Exif and IPTC metadata will not exist in the new image.
  • All four Exif and IPTC tags require the cfimage tag to read in an entire image before metadata is available, this can be time consuming and resource intensive.
  • Not all IPTC metadata tags may be available or be named as expected.

Retrieving individual Exif tags

Keep in mind that not all images have Exif/IPTC metadata, and if they do have metadata the tags may be different from camera to camera. A digital photo that has been manipulated/re-saved/re-sized/etc may very likely have lost its metadata. Also different digital cameras use different names for tags.

Using ImageGetEXIFTag() can cause problems if you are looking for a tag that doesn't exist in the image:

Erroring code:    

<cfimage action="read" source="IMG_7654.JPG" name="myImage" />

<cfset badTagName = ImageGetExifTag(myimage,"Bad Tag Name") />

<cfoutput>#badTagName#</cfoutput>

Will cause error: " Variable BADTAGNAME is undefined."

One solution to this:

<cfif isdefined('badTagName')>    

<cfoutput>#badTagName#</cfoutput>

</cfif>

Another solution to get around not knowing if certain Exif tags exist is to use ImageGetExifMetadata():

<cfimage action="read" source="IMG_7654.JPG" name="myImage" />    

<cfset exifMetadata = ImageGetExifMetadata(myimage) />

<cfif structKeyExists(exifMetadata,"Date/Time")>

<cfoutput>#ImageGetExifTag(myimage,"Date/Time")#</cfoutput>

</cfif>

Exif Date Issues

Most Exif Date/Time stamps are stored in a format that is not a valid ColdFusion date (that can be converted from a string). So to evaluate or dateFormat() an Exif date you may have to do a replace() or regex to format the date for CF.

Sample code to retrieve the date and time a digital photo was taken:

<cfimage action="read" source="IMG_7654.JPG" name="myImage" />    

<cfset datetime = ImageGetExifTag(myimage,"Date/Time") />

<cfdump var="#datetime#">

Output:
2005:12:28 12:20:32

Comments

Great info! Thanks for the time you spent preparing this!