Dragonflyはimagemagickを使用して画像を拡大縮小します。これは、 MiniMagickで行った以前のコードからまとめたコードなので、かなり似ています。
ファイルをTempfileに入れます。私はここでFaradayとTypheousを使ってこれを行いました。次に、magickを使用してスケーリングします。
require 'faraday'
require 'faraday_middleware'
#require 'faraday/adapter/typhoeus' # see https://github.com/typhoeus/typhoeus/issues/226#issuecomment-9919517 if you get a problem with the requiring
require 'typhoeus/adapters/faraday'
configure do
Faraday.default_connection = Faraday::Connection.new(
:headers => { :accept => 'image/*',
:user_agent => "Sinatra via Faraday"}
) do |conn|
conn.use Faraday::Adapter::Typhoeus
end
end
helpers do
def grab_image_and_scale
response = Faraday.get url # you'll need to supply this variable somehow, your choice
filename = "SOMETHING.jpg"
tempfile = Tempfile.open(filename, 'wb') { |fp| fp.write(response.body) }
thumb = MiniMagick::Image.open( tempfile.path )
thumb.thumbnail( "75x75" )
thumb.write( File.join settings.public, "images", "thumb_#{filename}")
scaled = MiniMagick::Image.open( secure_path )
scaled.resize( "600" )
scaled.write( File.join settings.public, "images", "scaled_#{filename}")
end
end
パブリックイメージフォルダーへのパスを一時ファイルに変更する方法を検討するのはあなたに任せます(そして、それがどのように行われるかを共有していただければ幸いです:)