Sha256: 1616013151a2f2a822c7a64b9916a54812431b94a2d4bb5f6d14ee041ecc94cf

Contents?: true

Size: 1.44 KB

Versions: 53

Compression:

Stored size: 1.44 KB

Contents

require_relative 'base'

module Udongo
  module ImageManipulation
    class ResizeToFill
      include Udongo::ImageManipulation::Base

      # Resize the image to fit within the specified dimensions while retaining
      # the aspect ratio of the original image. If necessary, crop the image in the
      # larger dimension.
      #
      # Possible values for options[:gravity] are:
      # NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
      #
      def resize(path)
        gravity = @options.key?(:gravity) ? @options[:gravity] : 'Center'

        img = MiniMagick::Image.open(@file)
        cols, rows = img[:dimensions]

        img.combine_options do |cmd|
          if @width != cols || @height != rows
            scale_x = @width/cols.to_f
            scale_y = @height/rows.to_f

            if scale_x >= scale_y
              cols = (scale_x * (cols + 0.5)).round
              rows = (scale_x * (rows + 0.5)).round
              cmd.resize "#{cols}"
            else
              cols = (scale_y * (cols + 0.5)).round
              rows = (scale_y * (rows + 0.5)).round
              cmd.resize "x#{rows}"
            end
          end

          cmd.quality @options[:quality] if @options.key?(:quality)
          cmd.gravity gravity
          cmd.background 'rgba(255,255,255,0.0)'
          cmd.extent "#{@width}x#{@height}" if cols != @width || rows != @height
        end

        img.write(path)
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
udongo-7.9.0 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.8.1 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.8.0 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.7.2 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.7.1 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.7.0 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.6.2 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.6.1 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.6.0 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.5.1 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.5.0 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.4.2 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.4.1 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.4.0 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.3.5 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.3.4 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.3.3 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.3.2 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.3.1 lib/udongo/image_manipulation/resize_to_fill.rb
udongo-7.2.1 lib/udongo/image_manipulation/resize_to_fill.rb