Module: Base64Image::ClassMethods
- Defined in:
- lib/base64_image.rb
Instance Method Summary collapse
Instance Method Details
#base64_to_binary(string_image_data) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/base64_image.rb', line 19 def base64_to_binary(string_image_data) if String(string_image_data).match(%r{^data:(.*?);(.*?),(.*)$}) # $1 - MIME, e.g. "image/png" # $2 - encoder, e.g. "base64" # $3 - encoded binary data binary_data = Base64.decode64($3) extension = $1.split('/')[1] io = StringIO.new(binary_data) # For carrierwave's attachments. io.class.class_eval do def original_filename "temp." << extension end end io end end |
#base64image_to(attr_name) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/base64_image.rb', line 10 def base64image_to(attr_name) dest_attr = String(attr_name) << '=' source_attr = 'base64_' << dest_attr define_method source_attr do |data| send dest_attr, self.class.base64_to_binary(data) end self end |