=20
Here's my image model. Perhaps that will give someone some insight.
=20
Say, I'm doing the Magick::Image.from_blob call in #after_save.
Perhaps something weird is happening with @file's lifetime or
something?
=20
class Image < ActiveRecord::Base
belongs_to :house
belongs_to :community
belongs_to :contractor
=20
FileLocation =3D "#{RAILS_ROOT}/public/media/"
DisplayLocation =3D "/media"
=20
def file=3D(file)
if file.size > 0
self.content_type =3D file.content_type.strip
@file =3D file
end
end
=20
# Runs after Image is saved, saves the image to the file system.
def after_save
if @file
@magick_image =3D Magick::Image.from_blob(@file.read).first
save_full_image
save_thumbnail_image
save_rounded_thumbnail_image
save_medium_image
save_large_image
end
end
=20
# If image is already a jpeg, then just write it to a file.
# If it's not, convert it to a jpeg and then write.
def save_full_image
@magick_image.write(full_file)
end
=20
# Write a thumbnail to the filesystem
def save_thumbnail_image
thumbnailed_magick_image =3D resize_image_to_exact_size(180,135)
thumbnailed_magick_image.write(thumbnail_file)
end
=20
def save_rounded_thumbnail_image
thumbnail_image =3D resize_image_to_exact_size(180, 135)
color =3D "#000033"
corner_width =3D 15
corners =3D Magick:
raw.new
corners.stroke(color)
corners.fill_opacity(0)
corners.stroke_width(15)
corners.roundrectangle(0, 0,
thumbnail_image.columns, thumbnail_image.rows,
corner_width, corner_width)
corners.draw(thumbnail_image)
thumbnail_image.write(rounded_thumbnail_file)
end
=20
# Resizes a Magick image to an exact size, keeping the same aspect rati= o
def resize_image_to_exact_size(width, height)
new_aspect_ratio =3D width.to_f / height.to_f
old_aspect_ratio =3D @magick_image.columns.to_f / @magick_image.rows.= to_f
=20
if old_aspect_ratio < new_aspect_ratio
# Image too tall, geometry string should restrict height
geometry_string =3D "#{width}"
else
# Image too wide or just right, geometry string should
# restrict width
geometry_string =3D "x#{height}"
end
=20
# Get the resized image
resized_image =3D resize_image(geometry_string)
=20
# Return the resized, crop image
resized_image.crop(Magick::CenterGravity, width, height)
end
=20
def resize_image(geometry_string)
logger.debug "resizing image to #{geometry_string}"
@magick_image.change_geometry(geometry_string) do |w, h, img|
img.resize(w, h)
end
end
=20
def save_medium_image
medium_image =3D resize_image("275")
medium_image.write(medium_file)
end
=20
def save_large_image
large_image =3D resize_image("500")
large_image.write(large_file)
end
=20
=20
=20
# Delete images from filesystem
def after_destroy
File.delete(full_file) if File.exist? "#{full_file}"
File.delete(thumbnail_file) if File.exist? "#{thumbnail_file}"
File.delete(rounded_thumbnail_file) if File.exist?
"#{rounded_thumbnail_file}"
File.delete(medium_file) if File.exist? "#{medium_file}"
File.delete(large_file) if File.exist? "#{large_file}"
end
=20
# Returns the filesystem location of the image file
def full_file
"#{FileLocation}/full/#{id}.jpg"
end
def thumbnail_file
"#{FileLocation}/thumbnail/#{id}.jpg"
end
def rounded_thumbnail_file
"#{FileLocation}/rounded_thumbnail/#{id}.jpg"
end
def medium_file
"#{FileLocation}/medium/#{id}.jpg"
end
def large_file
"#{FileLocation}/large/#{id}.jpg"
end
=20
=20
# Returns the location of the image to the browser
def full
"#{DisplayLocation}/full/#{id}.jpg"
end
def thumbnail
"#{DisplayLocation}/thumbnail/#{id}.jpg"
end
def medium
"#{DisplayLocation}/medium/#{id}.jpg"
end
def large
"#{DisplayLocation}/large/#{id}.jpg"
end
def rounded_thumbnail
"#{DisplayLocation}/rounded_thumbnail/#{id}.jpg"
end def resize
if File.exist? full_file
@file =3D File.open full_file
save
end
end
end
=20