Module: Alchemy::Picture::Calculations

Included in:
Alchemy::Picture
Defined in:
app/models/alchemy/picture/calculations.rb

Instance Method Summary collapse

Instance Method Details

#can_be_cropped_to?(string, upsample = false) ⇒ Boolean

An Image smaller than dimensions can not be cropped to given size - unless upsample is true.

Returns:

  • (Boolean)


9
10
11
12
13
# File 'app/models/alchemy/picture/calculations.rb', line 9

def can_be_cropped_to?(string, upsample = false)
  return true if upsample

  is_bigger_than? sizes_from_string(string)
end

#image_sizeObject

This function returns the :width and :height of the image file as a Hash



41
42
43
44
45
46
# File 'app/models/alchemy/picture/calculations.rb', line 41

def image_size
  {
    width: image_file_width,
    height: image_file_height
  }
end

#is_bigger_than?(dimensions) ⇒ Boolean

Returns true if both dimensions of the base image are bigger than the dimensions hash.

Returns:

  • (Boolean)


17
18
19
# File 'app/models/alchemy/picture/calculations.rb', line 17

def is_bigger_than?(dimensions)
  image_file_width > dimensions[:width] && image_file_height > dimensions[:height]
end

#is_smaller_than?(dimensions) ⇒ Boolean

Returns true is one dimension of the base image is smaller than the dimensions hash.

Returns:

  • (Boolean)


23
24
25
# File 'app/models/alchemy/picture/calculations.rb', line 23

def is_smaller_than?(dimensions)
  !is_bigger_than?(dimensions)
end

#sizes_from_string(string) ⇒ Object

Given a string with an x, this function returns a Hash with point :width and :height.



30
31
32
33
34
35
36
37
# File 'app/models/alchemy/picture/calculations.rb', line 30

def sizes_from_string(string)
  width, height = string.to_s.split("x", 2).map(&:to_i)

  {
    width: width,
    height: height
  }
end