Search code examples
rubyresizesinatrarmagickradiant

Resize image example on radiant CMS on ubuntu with back_door


I assume this is the same command as doing it in Sinatra.

I want to server-side resize user-uploaded images to a standard width using back_door. I assume RMijick is the way to go - but I'd just like a code sample to achieve this.

I want to know whether it is a better pattern to resize and then cache with rack-pagespeed, or just check to see if the image requested has already been resized and skip the operation.


Solution

  • Note: I'm assuming you've installed and configured radiant and the back_door extension.

    So start off with the ubuntu install:

    apt-get install libmagick9-dev
    

    Then the gem install

    gem install rmagick
    

    Then validate the gem version:

    irb -rubygems -r RMagick
    irb(main):001:0> puts Magick::Long_version
    

    Then reload apache

    /etc/init.d/apache2 reload
    

    Then in your page do:

    <r:ruby>
    require 'RMagick'
    if File.exists?("/var/www/grub/public/page_attachments/0000/0010/MexicanBeefSalad190.jpg")
      #"file exists"
    else
      img = Magick::Image.read "/var/www/grub/public/page_attachments/0000/0010/MexicanBeefSalad.jpg"
      img[0].change_geometry('190x190') { |cols, rows, img2|
        img2.scale!(cols, rows)
      }
      img[0].write ("/var/www/grub/public/page_attachments/0000/0010/MexicanBeefSalad190.jpg")
      #"not exists"
    end
    </r:ruby>