Sha256: 536f53589ae8b3995db8224800dc675fba05e0f83eeda58a1c522d202469e0a5

Contents?: true

Size: 1.47 KB

Versions: 33

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Alchemy
  class Picture < BaseRecord
    module Calculations
      # An Image smaller than dimensions
      # can not be cropped to given size - unless upsample is true.
      #
      def can_be_cropped_to?(string, upsample = false)
        return true if upsample

        is_bigger_than? sizes_from_string(string)
      end

      # Returns true if both dimensions of the base image are bigger than the dimensions hash.
      #
      def is_bigger_than?(dimensions)
        image_file_width > dimensions[:width] && image_file_height > dimensions[:height]
      end

      # Returns true is one dimension of the base image is smaller than the dimensions hash.
      #
      def is_smaller_than?(dimensions)
        !is_bigger_than?(dimensions)
      end

      # Given a string with an x, this function returns a Hash with point
      # :width and :height.
      #
      def sizes_from_string(string = "0x0")
        string = "0x0" if string.nil? || string.empty?

        raise ArgumentError unless string =~ /(\d*x\d*)/

        width, height = string.scan(/(\d*)x(\d*)/)[0].map(&:to_i)

        width = 0 if width.nil?
        height = 0 if height.nil?
        {
          width: width,
          height: height,
        }
      end

      # This function returns the :width and :height of the image file
      # as a Hash
      def image_size
        {
          width: image_file_width,
          height: image_file_height,
        }
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
alchemy_cms-5.3.8 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.7 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.6 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.5 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.4 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.3 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.2 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.1 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.3.0 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.2.7 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.2.6 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.1.10 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.2.5 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.1.9 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.2.4 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.1.8 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.1.7 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.2.3 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.1.6 app/models/alchemy/picture/calculations.rb
alchemy_cms-5.2.2 app/models/alchemy/picture/calculations.rb