# Best way to upload an image and make a thumbnail

**URL:** <https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142>\
**Category:** Rails\
**Created:** [July 11, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142 "2007-07-11T21:11:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 11, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/1 "2007-07-11T21:11:19Z")

</div>

Hi, which is the best way to upload an image, keep that and also make a  
thumbnail?  
The images should not be stored in the database, but as normal image  
files in a specific directory.  
I think that for the thumbnail i should use rmagick, but for the upload  
? (something which work with and without javascript, in every browser)  
Thanks 🙂

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 11, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/2 "2007-07-11T21:19:17Z")

</div>

Try fleximage

[http://fleximage.rubyforge.org/](http://fleximage.rubyforge.org/)

Gokhan  
[www.sylow.net](http://www.sylow.net)

Mix M. wrote:

> Hi, which is the best way to upload an image, keep that and also make a  
> thumbnail?  
> The images should not be stored in the database, but as normal image  
> files in a specific directory.  
> I think that for the thumbnail i should use rmagick, but for the upload  
> ? (something which work with and without javascript, in every browser)  
> Thanks 🙂

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 11, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/3 "2007-07-11T21:20:21Z")

</div>

Just curious, is there any special reason as to why you would not want  
them stored in the database? How will you be referencing back to the  
images later?

I know this not helping you original question, but I was just curious  
to know why you would not want them stored.

–Cory

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 11, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/4 "2007-07-11T21:31:41Z")

</div>

CPerry wrote:

> Just curious, is there any special reason as to why you would not want  
> them stored in the database? How will you be referencing back to the  
> images later?
> 
> I know this not helping you original question, but I was just curious  
> to know why you would not want them stored.
> 
> –Cory

Actually would not be a problem, but with a lot of images all with a  
thumbnail i think that the db dimension would grow exponentially and  
maybe decrease the performance, and also because with just images i can  
move them to another server and reference to it, without move all the db  
To reference them i think it’s important to save just the name and then  
load it in the view as a normal image, could be a possible solution? 🙂

> Try fleximage
> 
> [http://fleximage.rubyforge.org/](http://fleximage.rubyforge.org/)

I think it could be quite heavy to resize on the fly images, especially  
when there are pages with 40 images, ok the cache, but isn’t better to  
resize them just one time and then use the thumb or the original when  
they are needed ?

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 11, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/5 "2007-07-11T21:56:26Z")

</div>

I’d do a system call to image magic’s convert program to make the  
thumbnail… about the best performance your going to get

def thumbnail(temp, target)  
system(  
“/usr/local/bin/convert #{escape(temp)} -resize 48x48!  
#{escape(target)}”  
)  
end

DHH touches on this in “Outsourcing the performance-intensive  
functions” on this blog page:  
[http://www.loudthinking.com/arc/2006\_09.html](http://www.loudthinking.com/arc/2006_09.html)

Not really following you on what your asking about with the how to  
upload part of the question? and not sure what javascript has to do  
with it? It should just be a file-upload field as far as the browser  
goes. For saving on the server I do something like this in one of my  
models:

def save\_file(file\_name, file)  
fh = File.open(file\_name, ‘w’)  
fh.write(file.read)  
fh.close  
end

def image=(img)  
return if img.blank?  
save\_file(“/whatever/name/for\_my\_file.jpg”, img)  
end

good luck!  
Tim

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 11, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/6 "2007-07-11T21:42:23Z")

</div>

I use attachment\_fu with RMagick for this. It also supports  
lightweight image processors such as ImageScience. You can find a  
howto here:

[http://clarkware.com/cgi/blosxom/2007/02/24](http://clarkware.com/cgi/blosxom/2007/02/24)

Mike

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 15, 2007, 9:26am UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/7 "2007-07-15T09:26:41Z")

</div>

Mike G. wrote:

> I use attachment\_fu with RMagick for this. It also supports  
> lightweight image processors such as ImageScience. You can find a  
> howto here:
> 
> [http://clarkware.com/cgi/blosxom/2007/02/24](http://clarkware.com/cgi/blosxom/2007/02/24)
> 
> Mike

does rmagick take a lot of memory to make thumbnails? i don’t know if  
choose it or imagescience (that i think could be ok, because i’ve just  
to resize)

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 15, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/best-way-to-upload-an-image-and-make-a-thumbnail/109142/8 "2007-07-15T12:59:41Z")

</div>

I do resizing when they are hit first time and cache them.

Gokhan  
[www.sylow.net](http://www.sylow.net)  
Mix M. wrote:

> > Try fleximage
> > 
> > [http://fleximage.rubyforge.org/](http://fleximage.rubyforge.org/)
> 
> I think it could be quite heavy to resize on the fly images, especially  
> when there are pages with 40 images, ok the cache, but isn’t better to  
> resize them just one time and then use the thumb or the original when  
> they are needed ?
