Leave That Thing Alone Blog

Viewing By Entry / Main
16 May. 2006

Update: ColdFusion8 Exif and IPTC

Using Mark Mandel's JavaLoader and Drew Noakes' open source Java based metadata extractor it is possible to read EXIF info from a JPEG without having to have add a "ColdFusion Class Path" in ColdFusion server admin.

Extracting EXIF information out of a JPEG with the metadata extractor library isn't new for ColdFusion, Chris Wigginton demonstrated how to do this several months ago. But in a shared hosting environment setting the class path can be a problem, that's where the JavaLoader helps out by eliminating the need to add a class path.

The below code is an example of iterating through a JPG's EXIF tags using the metadata-extractor loaded with the JavaLoader:

<cfscript>
photo = "#expandPath("yourPhoto.jpg")#";
photoFile = createObject("java","java.io.File").init(photo);
//set the path
paths = ArrayNew(1);
paths[1] = expandPath("metadata-extractor-2.3.1.jar");
//create the loader
loader = createObject("component", "JavaLoader").init(paths);
//create the JpegMetadataReader instace
JpegMetadataReader = loader.create("com.drew.imaging.jpeg.JpegMetadataReader");
//Read jpg file
JpegMetadata = JpegMetadataReader.readMetadata(photoFile);
//get directory iterator
JpegDirectories = jpegMetadata.getDirectoryIterator();
</cfscript>

<!--- output all EXIF info tags --->
<cfoutput>
<cfloop condition="JpegDirectories.hasNext()">
   <cfset currentDirectory = JpegDirectories.next() />
   <cfset tags = currentDirectory.getTagIterator() />

   <table>
   <tr>
      <th colspan="4">Directory: #currentDirectory.getname()#</th>
   </tr>
   <cfloop condition="tags.hasNext()">
      <cfset currentTag = tags.next()>
         <tr>
         <td>#currentTag.getTagType()#</td>
         <td>#currentTag.getTagName()#</td>
         <td>#currentTag.getDescription()#</td>
         <td>#currentTag.toString()#</td>
         </tr>
   </cfloop>
   </table>
</cfloop>
</cfoutput>

It is also possible to refer to an individual EXIF tag item this way: #currentDirectory.getString(currentDirectory.TAG_Focal_Length)#

This example code can been seen in action here: Example EXIF output with different input JPGs

To get the above to to work you need to download the JavaLoader.cfc and the metadata-extractor-2.3.1.jar file and place them in the same directory as the example CF code.

Comments

Thanks, I've been looking for this myself... I came some way towards getting it working, but never got quite there.


This is great! I'm new to coldfusion, and although I managed to get your first example to work on my server, I'm having trouble with the second example. How do I speify an individual EXIF tag item? I know it may seem redundant to you, but I wondered if you could show a code example for that, too.


I ran an image from a Sony Alpha camera and got an error on the "Directory: Sony Makernote"

"a descriptor must be set using setDescriptor(...) before descriptions can be provided"


Thank you! It took me a while to get this set up (that classloader thing is complicated for us non-java-guru-guys), but it's working and will make my workflow much simpler! (Now I can let my users upload their images right out of Lightroom and they don't have to retype the metadata. Yay!)