Generate image from base64

Hi,

I have the following base64:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXMAAAB/CAIAAACSbbUKAAAKsGlDQ1BJQ0MgUHJvZmlsZQAASA2tlndUk8kaxuf70hstIRQpoXekV+k1FOlVVEISSCgxBIKIqKgsruCKIiICFtCVpuCq1LUgFkRdBBv2BVlU1OtiQVRU7he4ZPece/e/O+fMzC/v92TKOzPnPACQK1lCYTosB0CGIFsU7ufJiI2LZ+B+BxDAAAKgA0UWO0voERoaBP6xfLyLqJFyy0wy1j/K/vcHeQ43iw0AFIp8TuJksTMQPoXUArZQlA0AygaJ667OFko4FmGaCFkgwpJ5aCnzXCDhpHkun9NEhnshmgYA8GQWS5QCAOkUEmfksFOQcUi3EbYQcPgCAMhohF3ZPBYHYW+ETTMyVklYiLBh0t/GSfkbs1hJ0jFZrBQpz+8F+ScysTc/S5jOWjP34//ZZKSLkXzNFQ2kJWelRQQiPR7JWS6b5ROxwDwuU3Jmc3Fhtmf4AvOzmZELzBP7Ry2wOC3KY4HTVgVK9YKkpSELcXaWF5L7+THzeJExC8zhevsssGhVuFSflRMhjefxvJYuaFJZAZLznlsbS4TQf5ib7iedV5gdKl2nIH2pdC/JIl+phpv1136zeZH+C+NkiyKlmmS+L3MhzhP5S+PC9Lk7PbcGkThcmgeuIEqaQw7LW5pbwAfBgAXY2dxc5A4B4LVKuEbET+FlMzyQW881ZTAFbHNThpWFpS2QvCGJBoD39Lm3AdGv/hXL7AHAsRg5L8n1ZUhUALB0AOh6BgD1418xnXfI8e4A4MwgWyzKmddJrivyMolAFtCACtAAOsAQmAErYAecgTvwAQEgBESCOLACsAEPZAARWA3ywUZQBErADrAbVIED4BBoAMfACdABToPz4DK4BgbBHfAQjIBx8ApMgo9gBoIgHESBqJAKpAnpQSaQFeQAuUI+UBAUDsVBiVAKJIDEUD60GSqByqAqqBZqhH6BuqDzUD80BN2HRqEJ6B

Is there a way to generate a PNG or JPG from this code using rails ?

Thanks.

I would start here: http://ruby-doc.org/stdlib-2.2.2/libdoc/base64/rdoc/Base64.html#method-i-decode64

Save the result to a file on disk.

Walter

You might be able to look at https://github.com/madrobby/spark_pr

which can be extended with:

SparkCanvas.class_eval do
  def to_uri
    require 'base64'
    # doesn't attempt to respect some limits noted in RFC 2397 since the data:
    # url is supposed to be 'short' (1k), but the meaning of that word has changed
    # a bit since 1998.
    # Modern browsers allow 4k-32k or more According to http://dataurl.net/#about
    %{data:image/png;base64,#{Base64.encode64(to_png).gsub("\n",'')}}
  end
end

and work it backwards.

-Rob