config.ru:
# encoding: UTF-8 require 'sinatra' require 'json' require 'RMagick' require 'magickwand'
# set root path ENV["APP_ROOT"] = File.dirname(FILE) # set image path folder IMAGE_PATH = ENV["APP_ROOT"] + "/public/images/" # require controller require ENV["APP_ROOT"] + "/app/controller/index.rb" run Sinatra::Application
app/controller/index.rb:
get '/image/:id/960x640/:type' do origin_image_path = "#{IMAGE_PATH}#{id}.#{type}" cache_image_path = "#{IMAGE_PATH}#{id}_#{size}.#{type}"
if !File.exist?(cache_image_path) image = Magick::Image.read(origin_image_path).first resized_image = image.resize(960, 640) resized_image.write(cache_image_path) end
image_path = cache_image_path image = Magick::Image.read(cache_image_path).first content_type "image/#{type.downcase}" image.to_blob
end
执行 rackup
命令行提示
[FATAL] failed to allocate memory
并且断开,无法访问。
初步测试是 resized_image = image.resize(960, 640) 这句的问题,但不知道怎么解决,求解,多谢~!