# Model to jpg

**URL:** <https://www.ruby-forum.com/t/model-to-jpg/135092>\
**Category:** Rails\
**Created:** [April 17, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/model-to-jpg/135092 "2008-04-17T19:59:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 17, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/model-to-jpg/135092/1 "2008-04-17T19:59:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 17, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/model-to-jpg/135092/2 "2008-04-17T22:52:48Z")

</div>

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](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](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!
