Skip to content

Instantly share code, notes, and snippets.

@Epictetus
Forked from WizardOfOgz/gist:1012107
Created June 17, 2011 09:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epictetus/1031117 to your computer and use it in GitHub Desktop.
Save Epictetus/1031117 to your computer and use it in GitHub Desktop.
Save Base64-encoded images with Paperclip
class Avatar < ActiveRecord::Base
attr_accessor :content_type, :original_filename, :image_data
before_save :decode_base64_image
has_attached_file :image,
PAPERCLIP_CONFIG.merge(
:styles => {
:thumb => '32x32#',
:medium => '64x64#',
:large => '256x256#'
})
protected
def decode_base64_image
if image_data && content_type && original_filename
decoded_data = Base64.decode64(image_data)
data = StringIO.new(decoded_data)
data.class_eval do
attr_accessor :content_type, :original_filename
end
data.content_type = content_type
data.original_filename = File.basename(original_filename)
self.image = data
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment