Module: Alchemy::Picture::Transformations

Extended by:
ActiveSupport::Concern
Included in:
Alchemy::PictureVariant
Defined in:
app/models/alchemy/picture/transformations.rb

Overview

This concern can extend classes that expose image_file, image_file_width and image_file_height. It provides methods for cropping and resizing.

Instance Method Summary collapse

Instance Method Details

#crop(size, crop_from = nil, crop_size = nil, upsample = false) ⇒ Object

Returns the rendered cropped image. Tries to use the crop_from and crop_size parameters. When they can't be parsed, it just crops from the center.


17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/alchemy/picture/transformations.rb', line 17

def crop(size, crop_from = nil, crop_size = nil, upsample = false)
  raise "No size given!" if size.empty?

  render_to = inferred_sizes_from_string(size)
  if crop_from && crop_size
    top_left = point_from_string(crop_from)
    crop_dimensions = inferred_sizes_from_string(crop_size)
    xy_crop_resize(render_to, top_left, crop_dimensions, upsample)
  else
    center_crop(render_to, upsample)
  end
end

#crop_size?Boolean

Returns true if the class we're included in has a meaningful crop_size attribute

Returns:

  • (Boolean)

74
75
76
# File 'app/models/alchemy/picture/transformations.rb', line 74

def crop_size?
  respond_to?(:crop_size) && !crop_size.nil? && !crop_size.empty?
end

#landscape_format?Boolean Also known as: landscape?

Returns true if picture's width is greater than it's height

Returns:

  • (Boolean)

38
39
40
# File 'app/models/alchemy/picture/transformations.rb', line 38

def landscape_format?
  image_file.landscape?
end

#portrait_format?Boolean Also known as: portrait?

Returns true if picture's width is smaller than it's height

Returns:

  • (Boolean)

48
49
50
# File 'app/models/alchemy/picture/transformations.rb', line 48

def portrait_format?
  image_file.portrait?
end

#render_size?Boolean

Returns true if the class we're included in has a meaningful render_size attribute

Returns:

  • (Boolean)

68
69
70
# File 'app/models/alchemy/picture/transformations.rb', line 68

def render_size?
  respond_to?(:render_size) && render_size.present?
end

#resize(size, upsample = false) ⇒ Object

Returns the rendered resized image using imagemagick directly.


32
33
34
# File 'app/models/alchemy/picture/transformations.rb', line 32

def resize(size, upsample = false)
  image_file.thumbnail(upsample ? size : "#{size}>")
end

#square_format?Boolean Also known as: square?

Returns true if picture's width and height is equal

Returns:

  • (Boolean)

58
59
60
# File 'app/models/alchemy/picture/transformations.rb', line 58

def square_format?
  image_file.aspect_ratio == 1.0
end