Model to jpg


I need to create a jpg representation of data in the model and embed
that into an svg doc in base64 8-P

From what I’ve seen around I could:

1- Use RMagick to create the jpg image
2- Endode it into Base64
3- Generate the svg reply

The svg generation I’ve done already (pure svg). But I’ll start now to
see how the image can be generated and encoded into Base64.

Any good pointers to good info?
I’ll posting how I manage to do it.



Ok so if anyone is interested here’s how I managed to do it.

1- Install RMagick
2- In the controller where you want to generate the pic do something

GET /pics/1

GET /pics/1.xml

def show
@pic = Pic.find(params[:id])

Magick::RVG.dpi = 90

rvg =,,0,1000,300) do |canvas|
canvas.background_fill = 'black'
canvas.text(250, 150, ).styles(:font_family=>'Verdana', 

:font_size=>55, :fill=>‘white’), 250, 170).styles(:fill=>‘red’)
canvas.rect(997, 297).styles(:fill=>‘none’, :stroke=>‘blue’)


before ="text01.jpg").first
blob = before.to_blob
#puts blob

@base64image = Base64.encode64(blob)
puts "base64"
puts @base64image

respond_to do |format|
  format.html # show.html.erb
  format.svg  { render :action => "svg.rhtml", :layout => false }
  format.xml  { render :xml => @pic }


GET /pics/new

GET /pics/new.xml

def new
@pic =

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @pic }


3- Now you can generate an SVG image of the @base64image variable. That
image contains the name of a picture stored in the DB:

I think that was all I needed to do. So now when I go to I can see a generated pic with the
name of that pic.

Extra- The trick to be able to serve SVG is explained in the next

So basically I can now create a graphical representation of the model.
It was easy, if anyone has any optimization ideas to the process above
feel free to suggest.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs