Sha256: dafbb9719686ff4e7807bdd6f2aaf5ff044381892022bfae0771b91b6648f97c

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'image_resizer/lib/disk_file_cache'
require 'image_resizer/lib/downloader'

module ImageResizer
  class ResizerController < Volt::HttpController
    include Downloader
    RESIZED_PATH  = "tmp/resized_images"

    # Create the resize_images folder if it does not exist
    FileUtils.mkdir_p(RESIZED_PATH)

    RESIZED_CACHE = DiskFileCache.new(RESIZED_PATH, 1000)

    def index
      image_url = params._image_url

      download_path = download_cached(image_url)

      height = params._height.to_i
      width = params._width.to_i
      crop = params._crop
      key = "#{height}/#{width}/#{crop}/#{image_url}"
      resize_path = RESIZED_CACHE.fetch(key) do |resize_path|
        resize_image(download_path, resize_path, height, width, crop)
      end

      resize_cache_time = Volt.config.resize_cache_time || 3600 # 1 hour default
      response_headers['Cache-Control'] = "public, max-age=#{resize_cache_time}"
      send_file(resize_path)
    end


    private

    # Resizes the image to the desired h/w/crop, returns the tmp path
    def resize_image(download_path, resize_path, height, width, crop)
      `convert #{download_path.inspect} -resize "#{height}x#{width}" #{resize_path.inspect}`
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
volt-image_resizer-0.1.1 app/image_resizer/controllers/server/resizer_controller.rb
volt-image_resizer-0.1.0 app/s3_image_resizer/controllers/server/resizer_controller.rb