Reading EXIF meta data in Cold Fusion with csImageFile

The csImageFile component can be used to read and write EXIF data that is stored in JPEG and TIFF images. This data is usually written at the time that the image is created, usually by the camera or scanner. Here we will show how all the supported EXIF tags can be read and also demonstrate how some tag values can be converted into meaningful values. The code fragments shown below use Cold Fusion, although other COM enabled languages can be used.

Using a loop to display all the EXIF tags in an image

The code shown below will display the name of each EXIF tag stored in the file, together with its value. It assumes that the csImageFile object has been created with a name Image and a JPG or TIFF image has been loaded.

<cfif image.ExifCount EQ 0>
  <p>No EXIF data in the image.</p>
<cfelse>
  <p>
  <cfset loopto=image.ExifCount - 1>
  <cfloop index="I" from="0" to="#loopto#" step="1">
    <cfoutput>#image.ExifName(I)#</cfoutput> :
    <cfoutput>#image.ExifValueByIndex(I)#</cfoutput><br>
  </cfloop>
  </p>
</cfif>

csImageFile does not reserve a separate property for every EXIF tag. Instead it stores all the values in an array and they can be read by index or by the tag name. In this example they are read by index by finding the total number of tags present and then looping through them.

Reading a tag by name

If a specific tag must be read the ExifValueByName method can be used. For example, the camera model can be found by reading the Model tag:

<cfoutput>#Image.ExifValueByName("Model")#</cfoutput>

If the Model tag is not present, this will return an empty string. The tag name is not case sensitive so "model" and "MODEL" would also work.

Converting the EXIF values to the correct data type

csImageFile stores all the EXIF tags as strings, although these tags can contain numerical values. Numbers can be integers or rational (fractional) numbers, and dates are stored as strings in a specific format. Refer to the csImageFile instructions for more on these data types. We provide some helper functions for converting between the data types.

Example of converting a rational value to a real number:

<cfoutput>#Image.RationalToReal(Image.ExifValueByName("ShutterSpeedValue"))#</cfoutput>

The RationalToReal method converts a string in the form a/b into a real number, where a and b are integers.

Example of converting an EXIF date/time into a Cold Fusion date/time:

<cfoutput>#Image.ExifStringToDate(Image.ExifValueByName("DateTimeOriginal"))#</cfoutput>

The ExifStringToDate method takes the string value stored in the EXIF tag and converts it to a date/time that Cold Fusion recognises. This can be passed into the DateFormat function to format it for display.

We have an online example where you can upload a JPG image and the EXIF data will be displayed - here.

Click here for more on writing EXIF attributes.