Use Exiv2 to extract GPS data from Images using CFML
Tags: cfml, coldfusion
Published: 2022-07-06 18:15:24

I mentioned [Exiv2](https://exiv2.org/) on a blog post from 2 years ago regarding [Supporting ColdFusion with Command Line Programs](https://dev.to/gamesover/supporting-coldfusion-with-command-line-programs-2plj).  Someone in the Adobe ColdFusion Forum recently inquired how to ["use ImageGetEXIFMetaData to try to get gps coordinates of an image"](https://community.adobe.com/t5/coldfusion-discussions/using-imagegetexifmetadata-to-try-to-get-gps-coordinates-of-an-image/m-p/13053016).

I've been down that road before and have found the built-in function (BIF) [ImageGetEXIFMetaData](https://cfdocs.org/imagegetexifmetadata) to be both non-performant and lacking when it comes to retrieving image metadata. The BIF only returns [Exif](https://en.wikipedia.org/wiki/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](https://cfdocs.org/cfexecute) or CFX_Exec[http://adiabata.com/cfx_exec.cfm] to run apps like [WKHTMLTOPDF](https://wkhtmltopdf.org/) (for PDF generation) and [GraphicsMagick](http://www.graphicsmagick.org/) (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](https://exiftool.org/). 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");
writeDump(imageData);
```

## Source Code

https://gist.github.com/JamoCA/308df6f2bc927203330a23c1c6678c39

{% gist https://gist.github.com/JamoCA/308df6f2bc927203330a23c1c6678c39 %}