Sha256: 0cf10cfb184d47f3d50720e9aaeb44b6eba2a85b0be5c63417720c70307827d6

Contents?: true

Size: 1.06 KB

Versions: 17

Compression:

Stored size: 1.06 KB

Contents

module Frank
  module Middleware
    
    begin
      require 'mini_magick'
    rescue LoadError
    end

    class Imager
  
      def initialize(app, options={})
        @app = app
      end
  
      # choose a random image if random is in the query
      def image_filename(dims, query)      
        if query.include?('random')
          "frank#{rand(10)}.jpg"
        else
          "frank#{dims.hash.to_s[-1..-1]}.jpg"
        end  
      end
  
      # catch a request for _img/0x0, get an image, resize it to given dims
      def call(env)
        path = env['PATH_INFO']
        image_path = File.join(LIBDIR, 'frank/templates/imager/')

        if defined?(MiniMagick) && path.include?('_img')
          dims = '!' + path.split('/').last.match(/\d+x\d+/i).to_s
          filename = image_filename(dims, env['QUERY_STRING'])
      
          image = MiniMagick::Image.from_file(image_path + filename)
          image.resize dims
          return [ 200, { 'Content-Type' => 'image/jpg' }, image.to_blob ] 
        end
        @app.call(env)
      end
      
    end
    
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
frank-1.0.12 lib/frank/middleware/imager.rb
frank-1.0.11 lib/frank/middleware/imager.rb
frank-1.0.10 lib/frank/middleware/imager.rb
frank-1.0.9 lib/frank/middleware/imager.rb
frank-1.0.8 lib/frank/middleware/imager.rb
frank-1.0.7 lib/frank/middleware/imager.rb
frank-1.0.6 lib/frank/middleware/imager.rb
frank-1.0.5 lib/frank/middleware/imager.rb
frank-1.0.4 lib/frank/middleware/imager.rb
frank-1.0.3 lib/frank/middleware/imager.rb
frank-1.0.2 lib/frank/middleware/imager.rb
frank-1.0.1 lib/frank/middleware/imager.rb
frank-1.0.0 lib/frank/middleware/imager.rb
frank-0.4.1 lib/frank/middleware/imager.rb
frank-0.4.0 lib/frank/middleware/imager.rb
frank-0.3.2 lib/frank/middleware/imager.rb
frank-0.3.1 lib/frank/middleware/imager.rb