# Images from database

**URL:** <https://www.ruby-forum.com/t/images-from-database/116544>\
**Category:** Rails\
**Created:** [October 7, 2007, 4:37am UTC](https://www.ruby-forum.com/t/images-from-database/116544 "2007-10-07T04:37:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 7, 2007, 4:37am UTC](https://www.ruby-forum.com/t/images-from-database/116544/1 "2007-10-07T04:37:14Z")

</div>

I have an application that stores thumbnail images in a database as  
simple RGB uncompressed data.  
I am currently writing a rails application to display the contents of  
this database.  
I am wondering what’s the fastest and most portable way to have that  
RGB data (blobs) converted as a valid image that a browser can  
display.  
Ideally, due to the large number of images I’ll be dealing with and  
displaying at a time, I am trying to avoid having to use something  
like rmagick to create the images temporarily on disk as they are  
fetched from the database.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 7, 2007, 6:06am UTC](https://www.ruby-forum.com/t/images-from-database/116544/2 "2007-10-07T06:06:41Z")

</div>

gga wrote:

> fetched from the database.  
> Well caching them to disc may save a lot of load on your DB and Rails  
> app (it’s kind of wasteful to tie up a Rails process just to serve image  
> data) but if you want to serve them directly from the database and  
> assuming the data is in some format the browser will recognize use  
> send\_data. E.g.:

@thumb = Thumb.find(params[:id])

send\_data(@thumb.data,  
:filename =\> “some\_file\_name\_here”,  
:type =\> “image/png”,  
:disposition =\> “inline”)

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 7, 2007, 8:04am UTC](https://www.ruby-forum.com/t/images-from-database/116544/3 "2007-10-07T08:04:17Z")

</div>

> Well caching them to disc may save a lot of load on your DB and Rails  
> app (it’s kind of wasteful to tie up a Rails process just to serve image  
> data) but if you want to serve them directly from the database and  
> assuming the data is in some format the browser will recognize use  
> send\_data. E.g.:

Thanks, but no. They are not in a browser compatible format.  
Currently they are just RAW RGB 8-bit unsigned chars.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 8, 2007, 6:34am UTC](https://www.ruby-forum.com/t/images-from-database/116544/4 "2007-10-08T06:34:38Z")

</div>

> So you are trying to send it as a file (attachment) and not display it  
> inline? You could try setting :disposition to “attachment” with  
> send\_data and see if that works. Not sure what :type to use, though.

No. I am trying to display them inline, but the data is not in a  
browser compatible format as it is created elsewhere by another  
utility. This is done to save space. In the future the image stored  
in the database should also end up being compressed, instead of RAW  
RGB.

I currently have it working using RMagick and generating the files to  
disk, but I find this unsatisfactory, as I am creating a lot of  
useless .png images on disk. I mean, if I have to create the images  
on disk, not much sense in keeping the rgb data in the database in the  
first place.

Retrieving the rgb data from the database is very fast, but it can be  
somewhat slow with a 32-bits imagemagick as it forces me to turn all  
0…255 to floats.

I am no (x)HTML expert, but most of the (x)HTML inline methods for  
images seem to be the equivalent as saving the image to disk, as you  
need to store inline data in the html that is a valid gif, png or  
jpg… which kind of beats the purpose of inlining.  
I’m kind of amazed there’s not a simple way to just pass RGB uchar  
triplets or RGBA and have the browser show that as an image.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 8, 2007, 8:13am UTC](https://www.ruby-forum.com/t/images-from-database/116544/5 "2007-10-08T08:13:01Z")

</div>

gga wrote:

> RGB.
> 
> I currently have it working using RMagick and generating the files to  
> disk, but I find this unsatisfactory, as I am creating a lot of  
> useless .png images on disk. I mean, if I have to create the images  
> on disk, not much sense in keeping the rgb data in the database in the  
> first place.

If you are using RMagick to do the conversion in memory (rather than  
calling something on the command line) you can just pass the data into  
send\_data bypassing the save to file step.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 8, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/images-from-database/116544/6 "2007-10-08T13:52:23Z")

</div>

> If you are using RMagick to do the conversion in memory (rather than  
> calling something on the command line) you can just pass the data into  
> send\_data bypassing the save to file step.

I am doing the conversion in memory, but not sure I follow you. How  
can send\_data() be used multiple times (like in the list view) to show  
the thumbnails.  
This is what I tried with no success.

def list  
@image\_pages, @images = paginate :images, :per\_page =\> 20  
@images.each do |image|

```
  file = "images/dbimage-#{image.id}.png"

  #next if File.exists?(file)

  thumbnail = image.thumbnail

  width, height = image.thumbnail_width, image.thumbnail_height

  next if width <= 0 or height <= 0

  thumbnail = thumbnail.unpack('C*').map { |x| x / 255.0 }

  img = Magick::Image.constitute(width, height, 'BGRA', thumbnail)
  #img.write(file)

  img.format = 'PNG'

  send_data(img.to_blob,
            :filename => file,
            :type => "image/png",
            :disposition => "inline")
end

```

end

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 9, 2007, 11:55am UTC](https://www.ruby-forum.com/t/images-from-database/116544/7 "2007-10-09T11:55:50Z")

</div>

gga wrote:

> > If you are using RMagick to do the conversion in memory (rather than  
> > calling something on the command line) you can just pass the data into  
> > send\_data bypassing the save to file step.
> 
> I am doing the conversion in memory, but not sure I follow you. How  
> can send\_data() be used multiple times (like in the list view) to show  
> the thumbnails.  
> This is what I tried with no success.

How many images are you trying to send per page? I was assuming you had  
it setup where each image was it’s own URL like:

[http://localhost/image/image\_key](http://localhost/image/image_key)

and you would just use image\_tag multiple times on the page. send\_data  
is not going to work if you are trying to push out multiple images in a  
single call.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 7, 2007, 9:24am UTC](https://www.ruby-forum.com/t/images-from-database/116544/8 "2007-10-07T09:24:29Z")

</div>

gga wrote:

> > Well caching them to disc may save a lot of load on your DB and Rails  
> > app (it’s kind of wasteful to tie up a Rails process just to serve image  
> > data) but if you want to serve them directly from the database and  
> > assuming the data is in some format the browser will recognize use  
> > send\_data. E.g.:
> 
> Thanks, but no. They are not in a browser compatible format.  
> Currently they are just RAW RGB 8-bit unsigned chars.

So you are trying to send it as a file (attachment) and not display it  
inline? You could try setting :disposition to “attachment” with  
send\_data and see if that works. Not sure what :type to use, though.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [October 9, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/images-from-database/116544/9 "2007-10-09T12:08:09Z")

</div>

If only IE supported the data: URI. Then you could return all your  
images inline.

Others are right though. You’ll need to have some method of referring  
to an image using a URI, and have that method render the image. Using  
send\_data() will let you send the raw image data directly, rather than  
using a temporary file.

I’ve started storing images (base64 encoded) in a Postgresql database,  
and send\_data is very handy there.

–Michael
