Sha256: f6c03932ae5bb309a611ec5a6eb51b2966eaed26c35908941d872c90dd5aa997

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8
module LightResizer
  class Middleware
    class Resizer

      def resize(dimensions, original_path, resize_path, crop)
        check_resized_dir(resize_path)
        store_image(dimensions, original_path, resize_path, crop)
      end

      private
      def check_resized_dir(resize_path)
        dir_name = File.dirname resize_path
        Dir.mkdir dir_name unless Dir.exist? dir_name
        #todo permissions?
      end

      def store_image(dimensions, original_path, resize_path, crop)
        @image = MiniMagick::Image.open original_path

        crop ? set_crop_options(dimensions) : set_options(dimensions)

        @image.write resize_path
      end

      def set_options(dimensions)
        @image.combine_options do |c|
          c.resize dimensions
          c.unsharp '0x1'
          c.add_command 'extent', dimensions
          c.gravity 'center'
        end
      end

      def set_crop_options(dimensions)
        @image.combine_options do |c|
          c.resize dimensions+'^'
          c.unsharp '0x1'
          c.add_command 'extent', dimensions
          c.gravity 'center'
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
light_resizer-0.1.2 lib/light_resizer/middleware/resizer.rb
light_resizer-0.1.1 lib/light_resizer/middleware/resizer.rb
light_resizer-0.0.2 lib/light_resizer/middleware/resizer.rb