Created
June 7, 2011 12:13
-
-
Save WizardOfOgz/1012107 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 |
This is so easy!
If you're on paperclip 5.1.0 you don't need to use the Paperclip::DataUriAdapter.register
, it's still included by default. I believe they only changed this with 5.2.0
Here it is saving only original format, any suggestion?
Paperclip 5.0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sarink I've include
Paperclip::DataUriAdapter.register
in paperclip.rb and still don't upload the image