Model to jpg

Hi,

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.

Thanks.

Hi,

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
like:

GET /pics/1

GET /pics/1.xml

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

Magick::RVG.dpi = 90

rvg = Magick::RVG.new(10.cm, 3.cm).viewbox(0,0,1000,300) do |canvas|
canvas.background_fill = 'black'
canvas.text(250, 150, @pic.name ).styles(:font_family=>'Verdana', 

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

rvg.draw.write('text01.jpg')

before = Magick::Image.read("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 }
end

end

GET /pics/new

GET /pics/new.xml

def new
@pic = Pic.new

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

end

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
http://127.0.0.1.:3000/pics/3.svg 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
thread:
http://www.ruby-forum.com/topic/149851#new


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.

Cheers!

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