Rails: How to regenerate file_column images

Posted by Matt Kull Tue, 13 Mar 2007 03:49:00 GMT

The rails plugin file_column provides a simple method of handling uploading files. I am using it to store location “avatar” images. The image needs to be resized into several different versions, and file_column handles this nicely with one method call in my model.

file_column :image, :magick => { 
         :versions => { :square => "75x75!", 
                        :profile => "275x260" 
                      }
        }

However, recently requirements changed. I now needed an additional version of the image and change the sizes of the old versions.

Since file_column does not provide an out of the box method of regenerating the images, and I had several thousand records with images associated already I had a problem.

After quite a bit of searching the best(only) solution I found was from caboose however I was not able to get his method to work quite as he posted, several modifications to file_column.rb were needed. Its definitely hackery, but it works (at least for everything I have tested it with). I have posted the updated file_column.rb file (back up your old version first)

Once that is in place, I’d suggest creating a rake task which will run the update. Here is a sample scriplet which will do the trick.

#Note: "Location" is my model name, and "image" is the name of my file_column field
task :regenerate_images => :environment do
  Location.find(:all, :conditions => "image IS NOT null").each { |loc| 
    loc.image = File.open(loc.image)
    loc.save
  }
end

If you are unfamiliar with rake go read up on it . Create a file called “regenerate_images.rake”, include the code above and place the file in your lib/tasks folder. To execute the script from the command line, type “rake regenerate_images”

Leave a comment, View comments, View trackbacks

Your Comments.

Leave your own response

  1. Thilo replied:

    Hi, i found your post while searching for a more efficent way to regenerate file column images. We use the following skript do do the job, it requires no changes in file column.

    recreates all file_column images in all needed sizes

    class ImageUpdater

    def update_all update_model(:user, :photo) update_model(:auto, :photo) update_model(:photo, :file) update_model(:group, :photo) end

    private

    def updatemodel(classname, attribute_name) eval(classname.tos.capitalize).find(:all).each {|instance| begin dir = instance.send(attribute_name) unless dir.blank? instance.send(“#{attribute_name}=”, upload(dir)) instance.save! end rescue Exception => e puts e.message end } end

    # copied from filecolumn/testcase.rb def upload(path, content_type=:guess, type=:tempfile) if content_type == :guess case path when /.jpg$/ then content_type = “image/jpeg” when /.png$/ then content_type = “image/png” else content_type = nil end end uploadedfile(path, contenttype, File.basename(path), type) end

    def uploadedfile(path, contenttype, filename, type=:tempfile) # :nodoc: if type == :tempfile t = Tempfile.new(File.basename(filename)) FileUtils.copy_file(path, t.path) else if path t = StringIO.new(IO.read(path)) else t = StringIO.new end end (class << t; self; end).class_eval do alias local_path path if type == :tempfile definemethod(:localpath) { “” } if type == :stringio definemethod(:originalfilename) {filename} definemethod(:contenttype) {content_type} end return t end

    end

    Posted: 152 days later.
  2. Mike Karliner commented:

    Hi,

    I’ve installed your new file_column.rb and hit the following problems.

    a) on line 631: I get an uninitialised constant FileColumn::Magick. I’ve changed that to FileColumn::MagickExtension on a hunch and it nearly works. However, although it generates the correct different versions of the image, it’s creating an incorrect path for them: in my case it generates:

    product/largeimage/15/foo-thumb.jpg (for a version called thumb) and the actual image is in product/largeimage/15/thumb/foo.jpg. Any pointers on what I’ve done wrong or how to fix it?

    Posted: 212 days later.

Your Reply

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.
eXTReMe Tracker