22 lines
442 B
Ruby
22 lines
442 B
Ruby
class Base64Image
|
|
attr_reader :data_uri
|
|
|
|
def initialize(data_uri)
|
|
@data_uri = data_uri
|
|
end
|
|
|
|
# Creates an instance from a given file path which is converted to a base64 data uri
|
|
def self.from_image(path, mime_type = "image/png")
|
|
data = Base64.encode64(File.open(path, "rb").read)
|
|
data_uri = "data:#{mime_type};base64,#{data}"
|
|
|
|
new(data_uri)
|
|
end
|
|
|
|
private
|
|
|
|
def encoded_image
|
|
@data_uri.split(",")[1]
|
|
end
|
|
end
|