Basic image resizing in Ruby on Rails

前端 未结 5 1772
不思量自难忘°
不思量自难忘° 2020-12-06 01:45

I\'m creating a little photo sharing site for our home\'s intranet, and I have an upload feature, which uploads the photo at original size into the database. However, I also

5条回答
  •  悲哀的现实
    2020-12-06 02:02

    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

提交回复
热议问题