Twitter: raymondcamden


Address: Lafayette, LA, USA

Remove EXIF data with ColdFusion

01-06-2014 3,543 views ColdFusion 13 Comments

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">

<hr/>

<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.

13 Comments

  • Todd Sharp #
    Commented on 01-06-2014 at 8:00 AM
    What that scare post fails to tell you is that most responsible, legit social media sites do remove the metadata from the pictures you upload.
  • Commented on 01-06-2014 at 8:08 AM
    That's good to know. I never bothered to check if FB does it as I only use FB for friends anyway.

    Still surprised there isn't an easy way to do this.
  • Todd Sharp #
    Commented on 01-06-2014 at 11:10 AM
    I've read up on it a bit in the past and there seems to be 2 distinct sides to the issue - one being privacy information advocates and the other being copyright advocates. I can see both sides of the issue so it's hard to say one way or another which makes the most sense.

    I do agree though - you'd think there'd be an easy way.
  • Commented on 01-06-2014 at 11:11 AM
    I'd rather remove the EXIF data myself before submitting to another site than to rely and trust that someone else will do it for you. This mimics my beliefs in the workplace as I'm a proponent of the belief "If you want something done, you gotta do it yourself".

    The masses currently consume and utilize media and other technology without having a true understanding of the best practices and security risks involved, and even though we, as IT professionals, can work to help them in any way possible, I still feel that empowering someone to be aware of a responsible for those actions is the best course.

    Though many can (and will) claim the time-tested (yet illogical) adage of "I'm computer illiterate", the ultimate responsibility still lies with the person putting the photo up (rather than the site for scrubbing meta data).
  • Commented on 01-06-2014 at 11:46 AM
    FB strips out the geocoding right after uploading, which i find a PITA. it could simply figure out locations (down to some user-controlled level of detail) & then strip out that info.
  • Commented on 01-06-2014 at 11:57 AM
    in reference to the actual problem, have you looked at (used to be called sanselan)?

    http://commons.apache.org/proper/commons-imaging/

    we used older versions to write EXIF to JPEG images (geocoding) woks fine w/cf. the latest version has a method "removeExifMetadata" that strips out all EXIF data from an image.

    you might also ping steve erat, i think he knows this EXIF stuff more than most.
  • Commented on 01-06-2014 at 12:25 PM
    @Paul: No, I had not. As I said, my Googling seemed to show no real solution (outside of reading in the bits and saving them as a new file as a way to nuke the EXIF). I'll have to try this.
  • Nitai #
    Commented on 01-07-2014 at 2:11 PM
    The best tool for metadata handling is hands down ExifTool. It is a simple library (on Ubuntu you simply do a "apt-get install exiftool", has downloads for Windows and Mac too).

    Then all you need to do is use some cfecexute-fu and you can read, write, modify any metadata of images, videos, audios and PDF's to your heart content.

    We use it all over Razuna for many years with success.

    http://www.sno.phy.queensu.ca/~phil/exiftool/
  • Commented on 01-08-2014 at 11:59 AM
    @Nitai: I'm really kinda opposed to a cfexecute solution... but not for any good reason. ;)
  • Nitai #
    Commented on 01-08-2014 at 12:03 PM
    lol... I hear you.

    Actually, we are writing everything to a .bat or .sh file and then only use cfexecute to execute those files. Then cfexecute only has to execute the file and not handle anything. Using this technique has saved us from many "issues" with cfexecute :-)
  • Commented on 01-08-2014 at 12:36 PM
    batch files & exe are kind of icky. but that thing has wrappers:

    http://www.sno.phy.queensu.ca/~phil/exiftool/#link...

    under the "Programming" heading.
  • Commented on 02-18-2014 at 11:05 PM
    When do the Exif data actually get dropped? In the first write or in the second read?
    Try this.
    ImageRead() > Any Edit > imageWrite(). Go to image properties of the saved image (in windows) > details tab and you can still see the Exif data. ImageRead() or imageWrite() haven’t really drop Exif data yet.

    Next do this:
    Read > Edit > Write > Read > Write. Now if you go to details tab, Exif data is gone.
    This looks rather like a bug to me, a ladybug perhaps, but nevertheless a bug.
  • Commented on 02-19-2014 at 5:51 AM
    That's not what I saw with my test - in the script above note it is just read and then write.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty