Rails and RMagick made easy - generating thumbnails

June 14th, 2007 by Yavor Ivanov

The one thing that is different about this article from others around the web is that I wanted to do simple and easy stuff. I did not want to have a model or to put pictures in a database and so on I just wanted to store a resized image to the file system. Why the filesystem and not database? Database is like a funnel. You put and put and use and it isn’t something that you can easily extend or expand to fit easily your needs. There are extends and so on… and you can always hack around problems but why running into them in the first place? So my way was simple and easy without putting much effort in things that I don’t actually need.

 

Recently I needed to work with images on a Rails project. I never did anything with images other than uploading. And I found that IMAGE processing on Rails isn’t quite a breeze. From the many options out there for image processing in Rails I found that RMagick is the most powerful one. There were also some plugins and so on that were quite accepted by the majority of Rails developers but I didn’t find them so straight forward as expected and they actually did some unnecessary stuff.

 

Before I proceed to the code I want to give an overview of the problems with the majority of the options for image processing out there:

 

  1. Documentation - missing or incomplete
  2. Ease of use
  3. Maintenance - support for the tool

 

With these in mind I found that the simplest and well supported option is RMagick!

 

RMagick is well supported and has a cool documentation. The documentation is a way beyond complete and full of examples but let’s say it just gives you an idea of how to use the thing. Putting the rumors about RMagick eating a lot of memory aside `cause we ain’t going to use image processing a lot on our web site, just generating a couple of thumbnails - we are going to use it and thats it!

 

So let’s start coding! If you have trouble installing RMagick take a look at the RMagick documentation or the README file which is supplied with the RMagick source code.

 

 

:action => ’save_picture’ >> the actual “upload”, resizing and saving of the image

 

 

:action => ‘profile’ >> the upload form (and showing the current avatar)

 

 

And here are some additional docs to read if you want to mode the source a bit:

 

RMagick documentation
documentation part useful for thumbnails

 




You like it? Digg it!



Tags: none

Related Posts:
None

17 Responses to “Rails and RMagick made easy - generating thumbnails”

  1. Sanjeev Says:

    Hi,

    I liked the solution you provided and actually implemented in the same exact fashion as you have given here as I had exactly the same need as you had. I basically copy pasted your code and then uploaded to my app server.

    But there seems to be a serious problem. Everything is working fine on my local development Windows XP machine but my site is hosted on Linux machine on ASO. And when I try to run the your piece of code out (i.e hit the upload button to call the save_picture method of pictures controller)I get an error saying “Rails application failed to start properly”. This is happening only for this page and all other part of my app is working fine.

    Have you any idea why is that happening. Point to be noted that the code although could make the pictures directory and the first directory named 0 but it could not write the thumbnail and the profile pic out there.

    Please hep me out with this if you can. Any hint would be appretiated.

    Thank you very much. Waiting for yuor reply ASAP :-)

  2. Yavor Ivanov Says:

    Hi Sanjeev! Well my dev machine is Windows XP my mate machine is Linux and the Production is also Linux. You shouldn’t have any problems running the code. I can only say it should be an issue with PERMISSIONS but can’t say more about the problem myself cause I haven’t gone into it. Maybe it could be helpful to check the logs and the permission of the folders you are writing in.

    Provide more info if you can’t get it working and we can find a solution. What linux are you running, what reals and so on… Cheers!

  3. Sanjeev Says:

    Hey thank for your reply….

    Even I suspect the permissions to be a problem. I was reading through this article “http://6brand.com/application-error-rails-app-failed-to-start-properly” and saw that ppl have also got into trouble and the main reason was permissions. So was going to try something on those lines. But then I am very scared as I am not a linux guy and the changing the permissions seems a lil scary to me.

    The problem is that I am not getting any log in the production.log file besides this :

    “Processing PicturesController#save_picture (for [IP ADDRESS] at 2007-07-19 05:28:33) [POST]
    Session ID: 467c9114a210cb1d41b84f9aa5139a1d
    Parameters: {”picture_file”=>#, “action”=>”save_picture”, “upload”=>”Upload Avatar”, “controller”=>”pictures”}

    the permissions under my app folder are as follows :

    drwxr-xr-x ./
    drwxr-xr-x ../
    -rw-r–r– README
    -rw-r–r– Rakefile
    drwxr-xr-x app/
    drwxr-xr-x components/
    drwxr-xr-x config/
    drwxr-xr-x db/
    drwxr-xr-x doc/
    drwxr-xr-x lib/
    drwxr-xr-x log/
    drwxr-xr-x public/
    drwxr-xr-x script/
    drwxr-xr-x test/
    drwxr-xr-x tmp/
    drwxr-xr-x vendor/

    and those for the public folder are :

    drwxr-xr-x ./
    drwxr-xr-x ../
    -rw-r–r– .htaccess
    -rw-r–r– 404.html
    -rw-r–r– 500.html
    -rw-r–r– _index.html
    drwxr-xr-x cgi-bin/
    -rwxr-xr-x dispatch.cgi*
    -rwxr-xr-x dispatch.fcgi*
    -rwxr-xr-x dispatch.rb*
    -rw-r–r– favicon.ico
    drwxr-xr-x images/
    drwxr-xr-x javascripts/
    drwxr-xr-x pictures/
    -rw-r–r– robots.txt
    drwxr-xr-x stylesheets/

    If that info helps you out anyway…
    hoping top get a reply from you this time also:)

  4. Yavor Ivanov Says:

    Well hi again :)

    The list of directories you gave is not actually of much help. The interesting things was about the permissions of the following folders:

    - /images/
    - /images/0 (this is the created folder)

    As I suspect the numeric folder which is created as you’ve said has a different permission than the image folder. Maybe you should not bother much with the permissions and so on… just try deleting the created numeric folder and change the owner of that folder to the one rails is using to write and so on.

    Also! Pay attention that you should run mongrel or whatever your configuration is as ROOT in order to avoid those problems easily.

    Well try and tell ;)


    tldp.org has good docs about linux there you can find more about permissions and whatever on linux ;)

    and don’t post you IP address ;) may got you into trouble rather than help

  5. Sanjeev Says:

    One more thing I would like to mention is that I tried changing the permissions on the pictures directory that your code makes in side the public directory to 777 for testing and that also with -R option (recursive) but still that did not work out. Now I am just waiting top try the permission structure on the link that I gave you and see if that helps. But I am waiting for your reply first.

    thx

  6. Yavor Ivanov Says:

    hey why not using our forum…. it will be much more practical and easy to maintain this conversation.

    Please provide as much info as possible in a new post in our forum.

    I will consult my mate about the problem so he might got an other idea about the resolution of it.

  7. Sanjeev Says:

    you said:

    “The interesting things was about the permissions of the following folders:

    - /images/
    - /images/0 (this is the created folder)”

    Do you mean the pictures folder, as the folder 0 is created under pictures folder.

    “just try deleting the created numeric folder and change the owner of that folder to the one rails is using to write and so on.”

    Can u also tell me how to find the user that rails might be using to write and and how can I change the owner of the pictures folder (owner of 0 folder)

    thx

  8. Sanjeev Says:

    ok… I would like to do that… can u provide me with the url for that

  9. Yavor Ivanov Says:

    Please start a new topic in the forum so we can maintain this better. But yes I meant pictures sorry about the mistake!

    Well about the user?!?! Find our what user is starting them, if using mongrels anyway.

  10. Yavor Ivanov Says:

    our forum link is top-right ;)
    but here it is the url: http://forum.rubycorner.net/

  11. Sanjeev Says:

    do I need to register there???

  12. Yavor Ivanov Says:

    unfortunately yes… in order to maintain clear view of who is writing and prevent most of the spam bots :( registration is a need

  13. Sanjeev Says:

    I have created new topic ““Rails application failed to start properly” ERROR”. Please have a look. I am watching this tiopic now.

  14. hiutopor Says:

    Hello

    Very interesting information! Thanks!

    G’night

  15. Kai Says:

    Yavor, while you’re doing a good thing by blogging about this topic, you’re encouraging some very bad software engineering practices here, and it discourages me when I see that other readers comment saying they have implemented things the same way.

    By not creating a model, there’s just too much logic going into the Controller. The strength of the MVC pattern is that you can encapsulate and delegate responsibility. It just isn’t the responsibility of the Controller to enforce model constraints. What happens when you need to extend the uploading capability? You essentially need to make the Controller bigger and bigger. Fat Model, Thin Controller - that’s the de facto accepted way to write Rails, and it applies here. You lose out on:

    - validation - what happens if someone uploads a text file? Well, that’s a begin/rescue block you need to place in the controller. If you need to hash the filenames, you now need to add hashing to the Controller.
    - you lose the power of ActiveRecord associations, including finder methods, dependencies
    - this is difficult to write a good test for

    I’m not a fan of saving files to the database and I wrote my own Image class for this (based heavily on the Image class found in Rails Recipes). Nowadays, I hear good things about the acts_as_attachment plugin - anyone interested in implementing this functionality should check it out.

  16. MarcelloDL Says:

    IIRC RMa ofgick has a “thumbnail” method which is more efficient than resize when you need to shrink the image by a factor of 10 or so.

  17. Petyo Says:

    http://www.kanthak.net/opensource/file_column/

Leave a Reply