Yesterday on Facebook I saw one of those "PLEASE SHARE WITH EVERYONE" type posts involving pictures and GPS data. Apparently there are still people who don't know about the metadata embedded with pictures and how they can be a risk. Fair enough - it's not like your camera typically warns you about this and if you don't know this stuff even exists, you can certainly understand how folks would be surprised when they found out. Given that you may want to help users out with this, how could you use ColdFusion to remove EXIF data from an image?

I thought this would be rather simple, but from what I can see, it is impossible. There is an imageGetEXIFMetadata function in ColdFusion, but no set or clear version. I did some Googling and discovered no solution at all. Brian Kresge blogged about this back in March, 2011 (EXIF Data, Coldfusion, and iPhones). His solution involved using imagePaste to copy the bits to a new image. I thought - surely - this can't be the only solution - but when I switched to Java I saw people doing something similar.

I hate to say it - but it looks like creating a new image is the only solution. This isn't terrible of course. If you are allowing folks to upload images you are probably doing work on them already - ensuring they aren't too big, possibly resizing them and creating thumbnails, etc. Here is a super simple example of this in action.

<cfset s = "/Users/ray/Desktop/ray.jpg">
<cfset img = imageRead(s)>
<cfset exif = imageGetExifMetadata(img)>
<cfdump var="#exif#" label="Exif Data">


<cfset sNew = "/Users/ray/Desktop/ray.clean.jpg">
<cfset imageWrite(img, sNew)>

<cfset img = imageRead(sNew)>
<cfset exif = imageGetExifMetadata(img)>
<cfdump var="#exif#" label="Exif Data">

I'd share a screen shot but all it shows is a big struct and then an empty struct. Keep in mind that if you want to preserve any of the EXIF data, you could. In my sample above I grab the data. You could store it in the database with the image file name. This could be useful data that you don't want to lose.