6

I'm trying to do an image conversion in a rails app from SVG to PNG. ImageMagick didn't work out for me, due to Heroku not able / wanting to upgrade IM at this time. I'm testing out some ideas of using RSVG2 / Cairo in dev but running into a roadblock.

I can easily convert and save the SVG to PNG like this:

#svg_test.rb
require 'debugger'
require 'rubygems'
require 'rsvg2'

SRC = 'test.svg'
DST = 'test.png'

svg = RSVG::Handle.new_from_file(SRC)
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 800, 800)
context = Cairo::Context.new(surface)
context.render_rsvg_handle(svg)
surface.write_to_png(DST)

But this only lets me write PNG files out. In the app, I need to be able to generate these on the fly, then send them down to the client browser as data. And I can't figure out how to do this, or even if its supported. I know I can call surface.data to get the raw data at least, but I don't know enough about image formats to know how to get this as a PNG.

Thanks

1
  • I'm curious if this worked out for you. I have a similar need and am running into difficulties with Heroku and ImageMagick. Commented Nov 22, 2015 at 11:16

1 Answer 1

8

Ah ha! I was so close and its pretty obvious in hindsight. Simply call the surface.write_to_png function with a StringIO object. This fills the string object, which you can then get the bytes for. Here's the finished svg_to_png function I wrote, along with a sample controller that calls it. Hope this helps someone else somewhere.

ImageConvert function:

  def self.svg_to_png(svg)
    svg = RSVG::Handle.new_from_data(svg)
    surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 800, 800)
    context = Cairo::Context.new(surface)
    context.render_rsvg_handle(svg)
    b = StringIO.new
    surface.write_to_png(b)
    return b.string
  end

Test controller:

  def svg_img
    path = File.expand_path('../../../public/images/test.svg', __FILE__)
    f = File.open(path, 'r')
    t = ImageConvert.svg_to_png(f.read)
    send_data(t , :filename => 'test.png', :type=>'image/png')
  end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.