Image resize in ruby on rails [duplicate]

允我心安 提交于 2020-01-25 12:36:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!