Module: Alchemy::Picture::Transformations

Extended by:
ActiveSupport::Concern
Included in:
EssencePicture, 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.

Constant Summary collapse

THUMBNAIL_WIDTH =
160
THUMBNAIL_HEIGHT =
120

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.


54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/alchemy/picture/transformations.rb', line 54

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

  render_to = sizes_from_string(size)
  if crop_from && crop_size
    top_left = point_from_string(crop_from)
    crop_dimensions = 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)

102
103
104
# File 'app/models/alchemy/picture/transformations.rb', line 102

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

#default_mask(mask_arg) ⇒ Object

Returns the default centered image mask for a given size. If the mask is bigger than the image, the mask is scaled down so the largest possible part of the image is visible.


21
22
23
24
25
26
27
28
29
30
# File 'app/models/alchemy/picture/transformations.rb', line 21

def default_mask(mask_arg)
  mask = mask_arg.dup
  mask[:width] = image_file_width if mask[:width].zero?
  mask[:height] = image_file_height if mask[:height].zero?

  crop_size = size_when_fitting({width: image_file_width, height: image_file_height}, mask)
  top_left = get_top_left_crop_corner(crop_size)

  point_and_mask_to_points(top_left, crop_size)
end

#landscape_format?Boolean Also known as: landscape?

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

Returns:

  • (Boolean)

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

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)

82
83
84
# File 'app/models/alchemy/picture/transformations.rb', line 82

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)

96
97
98
# File 'app/models/alchemy/picture/transformations.rb', line 96

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

#resize(size, upsample = false) ⇒ Object

Returns the rendered resized image using imagemagick directly.


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

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

#square_format?Boolean Also known as: square?

Returns true if picture's width and height is equal

Returns:

  • (Boolean)

89
90
91
# File 'app/models/alchemy/picture/transformations.rb', line 89

def square_format?
  image_file.aspect_ratio == 1.0
end

#thumbnail_size(size_string = "0x0", crop = false) ⇒ Object

Returns a size value String for the thumbnail used in essence picture editors.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/alchemy/picture/transformations.rb', line 34

def thumbnail_size(size_string = "0x0", crop = false)
  size = sizes_from_string(size_string)

  # only if crop is set do we need to actually parse the size string, otherwise
  # we take the base image size.
  if crop
    size[:width] = get_base_dimensions[:width] if size[:width].zero?
    size[:height] = get_base_dimensions[:height] if size[:height].zero?
    size = reduce_to_image(size)
  else
    size = get_base_dimensions
  end

  size = size_when_fitting({width: THUMBNAIL_WIDTH, height: THUMBNAIL_HEIGHT}, size)
  "#{size[:width]}x#{size[:height]}"
end