Over a million developers have joined DZone.

Svg Base64 Image Extractor

·
// description of your code here


require "nokogiri"

doc = Nokogiri::XML(File.read(ARGV[0]))
(doc/"image").each do |image|
  type, base64 = image.attr("href").split(",", 2)
  extension = type[/^data:image\/([^;]+);/, 1]
  extension.sub!(/^jpeg$/, "jpg")
  image_content = base64.unpack("m*").first

  File.open(image.attr("id")+"."+extension, "w") { |f| f.write(image_content) }
end
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}