Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

On-the-fly Thumbnailer Method For A Rails 'Photo' Controller

DZone's Guide to

On-the-fly Thumbnailer Method For A Rails 'Photo' Controller

·
Free Resource

require 'RMagick'

class PhotoController < ApplicationController

[...snip...]

    def render_resized_image
                @photo=Photo.find(@params["id"])

                maxw = @params["width"] != nil ? @params["width"].to_i : 90
                maxh = @params["height"] != nil ? @params["height"].to_i : 90
                aspectratio = maxw.to_f / maxh.to_f


                pic = Magick::Image.from_blob(@photo.image)[0]


                picw = pic.columns
                pich = pic.rows
                picratio = picw.to_f / pich.to_f

                if picratio > aspectratio then
                        scaleratio = maxw.to_f / picw
                else
                        scaleratio = maxh.to_f / pich
                end

                #breakpoint

                thumb = pic.resize(scaleratio)

                @response.headers["Content-type"]=@photo.mime
    end
end

Requires RMagick

Based on Thumbnailer in Ruby and RMagick

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}