Resizing existing images uploaded using file_column

This post was migrated over from Muziboo DevBlog. File column is a great plugin by Sebastian Kanthak for uploading images (and other files) in a rails' site. The plugin has rmagick integration and can create thumbnails automatically for you. We use it for Muziboo and have been pretty happy with it (yes despite the rmagick dependency). We recently had to update the thumbnail dimensions for a few of our models. Its fairly simple to change the thumbnail sizes in the model and all new uploads are resized to new dimensions. However, the existing images are not automatically changed for you . Thankfully, on digging deeper into the code I found that there is pretty simple way to accomplish that. Lets say you have a user model (with a file_column field photo) and you want to update the thumbnails for all existing images. You just need to simply run the following commands
User.find(:all,:conditions => "photo IS NOT NULL").each do |u|
  u.photo = File.open(u.photo)
  u.save
end
Ofcourse if you have a lot of users, you may wanna fetch them in batches. Also image resizing is a pretty CPU intensive operation so you may wanna watch out for that. Once the operation is over, you will have updated thumbnails for all your previously uploaded images.

blog comments powered by Disqus
Hana Mohan
Hana Mohan