Class: Getimg::Image
- Inherits:
-
Object
- Object
- Getimg::Image
- Defined in:
- lib/getimg/image.rb
Instance Method Summary collapse
Instance Method Details
#base64_to_file(base64_data, filename = "image") ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/getimg/image.rb', line 5 def base64_to_file(base64_data, filename = "image") # return base64_data unless base64_data.present? && base64_data.is_a?(String) # decode64 img_from_base64 = Base64.decode64(base64_data) # find file type filetype = /(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF)/.match(img_from_base64[0, 16])[0] # write file file = "#{filename}.#{filetype}" File.binwrite(file, img_from_base64) end |
#file_to_base64(file_path) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/getimg/image.rb', line 19 def file_to_base64(file_path) File.open(file_path, "rb") do |file| # why strict_encode64 # https://ruby-doc.org/stdlib-2.3.1/libdoc/base64/rdoc/Base64.html#method-i-strict_encode64 Base64.strict_encode64(file.read) end end |