DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Svg Base64 Image Extractor

10.25.2010
| 3658 views |
  • submit to reddit
        // 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