Embed Images in eruby

H

Horacio Sanson

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....
%>

<img src="<%=img.to_blob%>"></img>


regards,
Horacio
 
J

Jan Svitok

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....
%>

<img src="<%=img.to_blob%>"></img>

See http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html
 
H

Horacio Sanson

Cool.... I took me 3 mins to get what I wanted.... =20

Here is the code I used:


<%
require 'base64'
require 'RMagick'
require 'cgi'

include Magick

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

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

<div>
<img src=3D"<%=3Dbuild_url(img.to_blob)%>"></img>
</div>


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


=E6=9C=88=E6=9B=9C=E6=97=A5 02 10=E6=9C=88 2006 20:33=E3=80=81Martin Coxall=
=E3=81=95=E3=82=93=E3=81=AF=E6=9B=B8=E3=81=8D=E3=81=BE=E3=81=97=E3=81=9F:
 
D

David Vallner

--------------enig33CC85BB2D0C7FE3ACE33744
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Horacio said:
I want to read an image from disk, modify it with rmagick (scale) and d= isplay=20
it in an HTML IMG tag but without saving the modified image to disk.=20
=20
Is there a way to send the Image object directly to the IMG tag??
=20
Something like:
=20
<%
require "RMagick"
include Magick
=20
img =3D Image.read("image.jpg").first
=20
## Do stuff to img.... like scale it down....
%>
=20
<img src=3D"<%=3Dimg.to_blob%>"></img>
=20

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 Vallner


--------------enig33CC85BB2D0C7FE3ACE33744
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFIXMAy6MhrS8astoRAv3UAJwP56S1XU72RC36jgu1CPHO/zXxuQCeJv91
Udi2hURlqRYtG8A2g+n6kpw=
=CaSi
-----END PGP SIGNATURE-----

--------------enig33CC85BB2D0C7FE3ACE33744--
 
E

Esad Hajdarevic

Horacio said:
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: http://en.wikipedia.org/wiki/Data_Uri

Greetings,

Esad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top