On-the-fly thumbnailer method for a Rails 'Photo' controller
1 2 require 'RMagick' 3 4 class PhotoController < ApplicationController 5 6 [...snip...] 7 8 def render_resized_image 9 @photo=Photo.find(@params["id"]) 10 11 maxw = @params["width"] != nil ? @params["width"].to_i : 90 12 maxh = @params["height"] != nil ? @params["height"].to_i : 90 13 aspectratio = maxw.to_f / maxh.to_f 14 15 16 pic = Magick::Image.from_blob(@photo.image)[0] 17 18 19 picw = pic.columns 20 pich = pic.rows 21 picratio = picw.to_f / pich.to_f 22 23 if picratio > aspectratio then 24 scaleratio = maxw.to_f / picw 25 else 26 scaleratio = maxh.to_f / pich 27 end 28 29 #breakpoint 30 31 thumb = pic.resize(scaleratio) 32 33 @response.headers["Content-type"]=@photo.mime 34 end 35 end
Requires RMagick
Based on Thumbnailer in Ruby and RMagick