# Embed Images in eruby

**URL:** <https://www.ruby-forum.com/t/embed-images-in-eruby/70721>\
**Category:** Ruby\
**Created:** [October 2, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721 "2006-10-02T12:10:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [October 2, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721/1 "2006-10-02T12:10:32Z")

</div>

I want to read an image from disk, modify it with rmagick (scale) and  
display  
it in an HTML IMG tag but without saving the modified image to disk.

Is there a way to send the Image object directly to the IMG tag??

Something like:

\<%  
require “RMagick”  
include Magick

```
    img = Image.read("image.jpg").first

## Do stuff to img.... like scale it down....

```

%\>

![]()

regards,  
Horacio

---

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [October 2, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721/2 "2006-10-02T13:20:40Z")

</div>

On 10/2/06, Horacio S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> include Magick
> 
> img = Image.read("image.jpg").first
> 
> ## Do stuff to img.... like scale it down....
> 
> ```
> 
> %\>
> 
> ![]()

See [http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html](http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html)

---

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [October 2, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721/3 "2006-10-02T13:34:42Z")

</div>

> See [http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html](http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html)

I don’t know what “Bumsparks” are, but they sound frightfully exciting.

Martin

---

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [October 2, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721/4 "2006-10-02T15:22:55Z")

</div>

Cool… I took me 3 mins to get what I wanted…

Here is the code I used:

\<%  
require ‘base64’  
require ‘RMagick’  
require ‘cgi’

```
include Magick

img = Image.read('test.jpg').first

def build_url(data)
    data = Base64.encode64(data).delete("\n")
    return "data:image/jpeg;base64,#{CGI.escape(data)}"
end

```

%\>

 ![]()

This displays the image in the page with no problems…  
~

æ??æ??æ?¥ 02 10æ?? 2006 20:33ã?Martin C. ã?ã??ã¯æ?¸ãã¾ã?ã?:

---

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [October 2, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721/5 "2006-10-02T23:46:13Z")

</div>

Horacio S. wrote:

> This displays the image in the page with no problems…

Beware that here the data is encoded within the URI, so you are limited  
on your browsers’ maximal URI length. I’ve read somewhere that Opera  
accepts URIs of maximal length 4kb, so that may be the upper limit for  
the image size.

See: [data URI scheme - Wikipedia](http://en.wikipedia.org/wiki/Data_Uri)

Greetings,

Esad

---

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [October 2, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/embed-images-in-eruby/70721/6 "2006-10-02T22:15:57Z")

</div>

Horacio S. wrote:

> ```
> img = Image.read("image.jpg").first
> 
> ```
> 
> ## Do stuff to img… like scale it down…
> 
> %\>
> 
> ![]()

[http://www.ietf.org/rfc/rfc2397.txt](http://www.ietf.org/rfc/rfc2397.txt)

Warning. Base64, madness, and death lie that way.

Well, not really, but I don’t think it works in IE.

David V.
