minimagick/rmagick
require 'RMagick'
def resize_img
uploaded_io = params[:avatar]
if uploaded_io.nil?
redirect_to "/", status: :found, notice: "请选择头像"
return
end
name = uploaded_io.original_filename
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
name_path = Rails.root.join('public', 'uploads', name)
img = Magick::Image.read(name_path).first
resize_width = params[:width] ? params[:width] : 200
resize_height = params[:height] ? params[:height] : 200
thumb = img.resize(resize_width , resize_height )
thumb.write(name_path)