-
-
Save Epictetus/1031117 to your computer and use it in GitHub Desktop.
Save Base64-encoded images with Paperclip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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