Sha256: 602f812f314be06eb952e1443290a28fb2fec975bcc07419bbe86e1f9dbad64d

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'gnawrnip/image'

module Gnawrnip
  class Developer
    def develop(file)
      image = Image.new(file)
      resize(image) if need_resize?
      image
    end

    private

    def resize(image)
      new_width, new_height = calculate_new_size(image.width, image.height)

      return if [new_width, new_height] === [image.width, image.height]

      image.resize(new_width, new_height)
    end

    def need_resize?
      !Gnawrnip.max_frame_size.nil?
    end

    #
    # Return new frame size (width and height).
    # This size is keeping original aspect ratio.
    #
    # @return  [Array]  New width and height size. [width, height]
    #
    def calculate_new_size(width, height)
      ratio  = width.to_f / height.to_f
      target = Gnawrnip.max_frame_size

      return [width, height] if target > [width, height].max

      if ratio < 1
        new_width  = target * ratio
        new_height = target
      else
        new_width  = target
        new_height = target / ratio
      end

      return [new_width, new_height]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gnawrnip-0.2.5 lib/gnawrnip/developer.rb
gnawrnip-0.2.4 lib/gnawrnip/developer.rb
gnawrnip-0.2.3 lib/gnawrnip/developer.rb