问题
Like this I am currently uploading a file ( image ) in my ruby application .. I need to resize the image after uploading ... please help for resizing the image
uploaded_io = params[:category][:thumb]
if uploaded_io != ""
name = uploaded_io.original_filename
if(FileTest.exist?("#{RAILS_ROOT}/public/data/#{name}"))
id = Category.maximum('id').to_s
id = id.to_i+ 1
name =id.to_s+"_"+name
end
Thanks
回答1:
If you have imagemagick already installed - then use ImageScience or MiniMagick instead, they both use much less resources and work faster, and are installed just as a common gem (actually a little bit more installation for imagescience)
ImageScience:
ImageScience.with_image("#{RAILS_ROOT}/public/data/#{name}") do |image|
image.thumbnail(100) do |thumb|
thumb.save <path_to_small_image to be saved>
end
end
MiniMagick:
MiniMagick::Image.new("#{RAILS_ROOT}/public/data/#{name}").resize "100x100"
回答2:
Try RMagick!
require 'RMagick'
img = Image.new name
thumb = img.scale(125, 125)
thumb.write "thumb.gif"
http://www.imagemagick.org/RMagick/doc/comtasks.html#thumb
来源:https://stackoverflow.com/questions/8520062/image-resize-in-ruby-on-rails