# ImageMagick and EXIF Data

**URL:** <https://www.ruby-forum.com/t/imagemagick-and-exif-data/58656>\
**Category:** Rails\
**Created:** [April 24, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/imagemagick-and-exif-data/58656 "2006-04-24T16:07:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [April 24, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/imagemagick-and-exif-data/58656/1 "2006-04-24T16:07:37Z")

</div>

Hi there,

I’m writing a little application that parses an image file and extracts  
the  
camera settings from the file (the EXIF data) and saves all the fields  
to  
the database. The idea is that after awhile people will be able to  
search  
based on camera make, model, lens settings, etc… and all the pictures  
that  
meet those requirements will be displayed. The picture is stored on the  
file  
system, and not in the DB. I’m a little lost as to where to actually  
perform  
the parse on the EXIF data though. I know it should probably be done in  
the  
image model. In my DB I have columns named “camera\_model”,  
“camera\_make”,  
“focal\_length”, etc… Here’s what I ‘think’ I need to do to grab the  
the  
data from the image file:

def load\_exif\_data(image)  
self.shutter\_speed =  
image.get\_exif\_by\_entry(“ShutterSpeedValue”)[0][1]  
self.aperture = image.get\_exif\_by\_entry(“ApertureValue”)[0][1]  
self.iso\_speed = image.get\_exif\_by\_entry(“ISOSpeedRatings”)[0][1]  
self.camera\_model = image.get\_exif\_by\_entry(“Model”)[0][1]  
self.focal\_length = image.get\_exif\_by\_entry(“FocalLength”)[0][1]  
self.flash = image.get\_exif\_by\_entry(“Flash”)[0][1]  
self.save  
end

* * *

Here’s my whole image model ( it’s a rough model, but it works for now):

require ‘RMagick’

class Image \< ActiveRecord::Base

DIRECTORY = ‘public/uploaded\_images’  
THUMBDIRECTORY = ‘public/thumbs’  
DISPLAY\_DIRECTORY = ‘/public/uploaded\_images/’  
DISPLAYTHUMBS\_DIRECTORY = ‘/public/thumbs/’

after\_save :process  
after\_destroy :cleanup

def file\_data=(file\_data)  
@file\_data = file\_data  
write\_attribute ‘extension’,  
file\_data.original\_filename.split(’.’).last.downcase  
write\_attribute(“content\_type”, file\_data.content\_type)  
write\_attribute(“original\_name”, file\_data.original\_filename)  
write\_attribute(“file\_size”, file\_data.size)  
end

def url  
path.sub(/^public/,’’)  
end

def to\_url  
return DISPLAYTHUMBS\_DIRECTORY + “#{self.id}-thumb.jpg”  
end

def to\_large\_url  
return DISPLAY\_DIRECTORY + “#{self.id}-full.jpg”  
end

def thumbnail\_url  
thumbnail\_path.sub(/^public/,’’)  
end

def path  
File.join(DIRECTORY, “#{self.id}-full.jpg”)  
end

def thumbnail\_path  
File.join(THUMBDIRECTORY, “#{self.id}-thumb.jpg”)  
end

def self.display\_all\_images  
find(:all, :order =\> “created\_at desc”)  
end

private  
#######

def process  
if @file\_data  
create\_directory  
cleanup  
save\_fullsize  
create\_fullsize  
create\_thumbnail  
load\_exif\_data  
@file\_data = nil  
end  
end

def save\_fullsize  
File.open(path,‘w’) do |file|  
file.puts @file\_data.read  
end  
end

def create\_fullsize  
img = Magick::Image.read(path).first  
img.change\_geometry!(‘700 x 700’) { |cols, rows, large|  
large.resize!(cols, rows)  
large.write path  
}  
end

def create\_thumbnail  
img = Magick::Image.read(path).first  
img.change\_geometry!(‘200 x 160’) { |cols, rows, thumbnail|  
thumbnail.resize!(cols, rows)  
chopped = thumbnail.chop(0, thumbnail.rows/2, thumbnail.columns/2,  
thumbnail.rows)  
chopped.write thumbnail\_path  
}  
end

def create\_directory  
FileUtils.mkdir\_p DIRECTORY  
end

def cleanup  
Dir[File.join(DIRECTORY, “#{self.id}-\*”)].each do |filename|  
File.unlink(filename) rescue nil  
end  
end

## end

How would I actually link my load\_exif\_data method up to this model?

Thank you,  
Dave H.
