base64 
Author Message
 base64

Hi,

how can I base64 encode an image so I can inline it in SVG like

<image width="400" height="300"
xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAs...

?

This is from RDoc's web/images/conv.rb; would it to what I want? Will it
work for PNGs and JPEGs as well?:
----------8<----------
# Convert a binary .gif file into a String so we can write it out
# from inside a Ruby program

name = ARGV.shift || raise("Missing name")

content = File.open(name, "rb") {|f| f.read}

content = [content].pack("m*").split("\n")

name.gsub!(/\./, '_').upcase!

puts "#{name} = ("
puts(content.collect {|line| "\t" + line.dump} .join(" +\n") +
   ").unpack('m*')[0]")
----------8<----------

Tobi

--
http://www.*-*-*.com/



Thu, 11 Nov 2004 05:14:05 GMT  
 base64

Quote:

> Hi,

> how can I base64 encode an image so I can inline it in SVG like

  [img_data].pack("m") should do it.

alternatively:

  require "base64"
  encode64(img_data)

maybe you also have to remove the newlines from the encoded string!

Regards,

  Michael

--



Thu, 11 Nov 2004 06:48:24 GMT  
 base64

Quote:

>   [img_data].pack("m") should do it.

Thanks. The below works, and uses code from RDoc's web/images/conv.rb :

require 'generated/svg/svg_writer'

name = ARGV.shift || raise("Missing name")
image_data = File.open(name, "rb") {|f| f.read}
base64_image = [image_data].pack("m*").split("\n")

doc = Svg_doc.new
root_el = doc.root
root_el.attributes["xmlns:xlink"]="http://www.w3.org/1999/xlink"
image = Image.new
image.width=100
image.height=100
image.xlink_href='data:image/jpeg;base64,'+base64_image.to_s
root_el.push image
open('generated/svg/base64.svg','w') {|f|f.write doc}

Tobi

--
http://www.pinkjuice.com/



Thu, 11 Nov 2004 16:52:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. What is base64.rb for? (from /usr/lib/ruby/1.6/base64.rb)

2. How to? Base64 to Image Backdrop

3. MaxProg Base64 library

4. Looking for a Base64 Plugin

5. How convert pictures as base64 coded JPEG?

6. Base64 (again)

7. Binary encoding of images (bin.base64)

8. Base64 Encoding for HTTP in CW5.0

9. Do anybody have base64 functions source code?

10. Base64 protocol

11. base64 encoding/decoding

12. base64 decoding/encoding library

 

 
Powered by phpBB® Forum Software