DEV Community

Cover image for Use Exiv2 to extract GPS data from Images using CFML
James Moberg
James Moberg

Posted on

Use Exiv2 to extract GPS data from Images using CFML

I mentioned Exiv2 on a blog post from 2 years ago regarding Supporting ColdFusion with Command Line Programs. Someone in the Adobe ColdFusion Forum recently inquired how to "use ImageGetEXIFMetaData to try to get gps coordinates of an image".

I've been down that road before and have found the built-in function (BIF) ImageGetEXIFMetaData to be both non-performant and lacking when it comes to retrieving image metadata. The BIF only returns Exif data which explains the extremely specific function name. Sometimes the metadata you want is accessible as IPTC, XMP metadata or ICC Profile.

Disclaimer: I stopped using many BIFs years ago in favor of dropping down to the command line and either using CFExecute or CFX_Exec[] to run apps like WKHTMLTOPDF (for PDF generation) and GraphicsMagick (for image manipulation). The result has been faster performance, higher quality results (data or display-wise) and the ability to perform tasks offline and/or concurrently. I also prefer it because Adobe has historically changed what some functions return (may be a bug, may be intentional, who really knows), whereas I haven't really encountered this issue when using portable command-line apps.

Performance: This Exiv2 UDF takes 40-135ms to return everything (exif, IPTC, XMP metadata & ICC profile) whereas ImageGetEXIFMetaData takes 280-340ms to return only exif data. The BIF's GPS data is also only returned in a sexagesimal display format rather than a useful decimal format. I've also ensured that the keys generated by the UDF are CF-variable-compatible (ie, isValid("variablename")) for easy reuse and/or direct output to an API. Dates are also outputted as valid yyyy-mm-dd hh:mm:ss dates rather than non-normalized yyyy:mm:dd HH:mm:ss format.

WRITING METADATA: If you need to write EXIF, IPTC, XMP or IPTC metadata to an image, I highly recommend using ExifTool. We use ExifTool to embed copyright information into images for the automatic display of the license information whenever thumbnails are displayed via Google Photos. (I find this approach to be superior than adding lots of hidden XML to webpages where the photos are displayed.)

Exiv2 (freeware)

Exiv is a Cross-platform C++ library and a command line utility to manage image metadata. It provides fast and easy read and write access to the Exif, IPTC and XMP metadata and the ICC Profile embedded within digital images in various formats.

Exiv2 UDF Usage

imageData = exiv2(filePath="c:\myImage", exePath="C:\Exiv2\Exiv2.exe");
Enter fullscreen mode Exit fullscreen mode

Source Code

Top comments (0)