Module: ChunkyPNG::Canvas::DataUrlImporting

Included in:
ChunkyPNG::Canvas
Defined in:
lib/chunky_png/canvas/data_url_importing.rb

Overview

Methods to import a canvas from a PNG data URL.

Instance Method Summary collapse

Instance Method Details

#from_data_url(string) ⇒ Canvas

Imports a canvas from a PNG data URL.

Parameters:

  • string (String)

    The data URL string to load from.

Returns:

  • (Canvas)

    The imported canvas.

Raises:

  • ChunkyPNG::SignatureMismatch if the provides string is not a properly formatted PNG data URL (i.e. it should start with “data:image/png;base64,”)



12
13
14
15
16
17
18
# File 'lib/chunky_png/canvas/data_url_importing.rb', line 12

def from_data_url(string)
  if string =~ %r[^data:image/png;base64,((?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?)$]
    from_blob($1.unpack("m").first)
  else
    raise SignatureMismatch, "The string was not a properly formatted data URL for a PNG image."
  end
end