Basic image resizing in Ruby on Rails
I tried following way. Its working fine. I hope it will help to some one.
1.add following gem in Gemfile:
gem "ImageResize", "~> 0.0.5"
2.run bundle
3.use this in controller function:
require 'rubygems'
require 'ImageResize'
#input_image_filename, output_image_filename, max_width, max_height
Image.resize('big.jpg', 'small.jpg', 40, 40)
You can resize with RMagick gem.
This is just a example that you can adapt:
require 'RMagick'
f = File.new( File.join(save_path, self.filename), "wb" )
f.write form_file.read #remeber to set :html=>{:multipart => true} in form
f.close
image = Magick::Image.read(self.filename).first
image.change_geometry!("640x480") { |cols, rows, img|
newimg = img.resize(cols, rows)
newimg.write("newfilename.jpg")
}
More Info here: http://www.imagemagick.org/script/api.php#ruby
Simply use paperclip or attachment_fu