Sha256: d9d95a8a961f3afa88d052a1b1e39104c6ee34e62e48f4d566e3d2191c0f3c67
Contents?: true
Size: 1.24 KB
Versions: 3
Compression:
Stored size: 1.24 KB
Contents
require "base64" module Vectory class Datauri < Image REGEX = %r{^ data: (?<mimetype>[^;]+); (?:charset=[^;]+;)? base64, (?<data>.+) $}x.freeze def self.from_vector(vector) mimetype = vector.class.mimetype content = vector.content.gsub("\r\n", "\n") data = Base64.strict_encode64(content) new("data:#{mimetype};base64,#{data}") end def mime match = parse_datauri(@content) match[:mimetype] end def height to_vector.height end def width to_vector.width end def to_vector match = parse_datauri(@content) content = Base64.strict_decode64(match[:data]) image_class = detect_image_class(match[:mimetype]) image_class.from_content(content) end private def parse_datauri(uri) match = REGEX.match(uri) return match if match raise ConversionError, "Could not parse datauri: '#{uri.slice(0, 30)}'." end def detect_image_class(image_type) case image_type when Eps.mimetype then return Eps when Emf.mimetype then return Emf when Svg.mimetype then return Svg end raise ConversionError, "Could not detect image type '#{image_type}'." end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
vectory-0.4.2 | lib/vectory/datauri.rb |
vectory-0.4.1 | lib/vectory/datauri.rb |
vectory-0.4.0 | lib/vectory/datauri.rb |