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:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}