Cloning Images

clone images after a minor tweak to attachment_fu

Daniel O'Shea • Jan. 30, 2008 11:51 PM
Clones.....

Clones.....

 

Here's the scenario: you have these really cool images that have been uploaded via your rails app with the attachment_fu plugin.  You'd really love to clone those images and the generated thumbnails for use with another model but you don't want to re-upload the images and re-size them.  You can try to clone the model but that won't make a copy of the actual image files on disk (or whatever storage backend you are using).  Have no fear this code snippet may help you out (insert into attachment_fu.rb): 

      # Create a clone of an image and it's thumbnails.
def create_clone
c = self.clone
self.thumbnails.each do |thumb|
n = thumb.clone
n.temp_path = thumb.create_temp_file
n.save_to_storage
c.thumbnails<<n
end
c.temp_path = self.create_temp_file
c.save_to_storage
c.save
return c
end

Presto, now you have a clone of the image and all of it's thumbnails with updated files in your storage backend of choice. 

Tested under Rails 1.2.3


Categories: DevelopmentRuby