Sha256: fb3ad05484b8341a4b2f026866731cf4072be9825199aae6e0bec7e68e5455e4

Contents?: true

Size: 1006 Bytes

Versions: 7

Compression:

Stored size: 1006 Bytes

Contents

module Frank
  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.expand_path(File.dirname(__FILE__)) + '/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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
frank-0.2.6 lib/frank/imager.rb
frank-0.2.5 lib/frank/imager.rb
frank-0.2.4 lib/frank/imager.rb
frank-0.2.3 lib/frank/imager.rb
frank-0.2.2 lib/frank/imager.rb
frank-0.2.1 lib/frank/imager.rb
frank-0.2.0 lib/frank/imager.rb