Ok. The class is ready for consumption. :) Here it is:
require 'RMagick'
include Magick
class ImageBlob
def ImageBlob.inject_attrib_assign_methods( attr_and_sizes_hash,
consumer_class )
attr_and_sizes_hash.each {|attr_name, max_size|
code = %{
def #{attr_name}=( file )
write_attribute( '#{attr_name}', ImageBlob.getBlob( file,
#{max_size} ))
end
}
consumer_class.class_eval code }
end
def ImageBlob.getBlob( file, max_image_size )
begin
img = Image.from_blob( file.read ).first
if not img
raise
end
img.format = "JPG"
if img.rows > max_image_size or img.columns > max_image_size
if img.rows > img.columns
factor = max_image_size / img.rows.to_f
else
factor = max_image_size / img.columns.to_f
end
img = img.scale( factor )
end
retVal = img.to_blob
GC.start
return retVal
rescue
return nil
end
end
end
--
Posted via http://www.ruby-forum.com/.