Module: Alchemy::Picture::Transformations

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

#can_be_cropped_to(string, upsample = false) ⇒ Object

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



119
120
121
122
# File 'app/models/alchemy/picture/transformations.rb', line 119

def can_be_cropped_to(string, upsample = false)
  return true if upsample
  is_bigger_than sizes_from_string(string)
end

#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.



50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/alchemy/picture/transformations.rb', line 50

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)


132
133
134
# File 'app/models/alchemy/picture/transformations.rb', line 132

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.



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

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

#image_sizeObject

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



109
110
111
112
113
114
# File 'app/models/alchemy/picture/transformations.rb', line 109

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

#landscape_format?Boolean Also known as: landscape?

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

Returns:

  • (Boolean)


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

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)


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

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)


126
127
128
# File 'app/models/alchemy/picture/transformations.rb', line 126

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

#resize(size, upsample = false) ⇒ Object

Returns the rendered resized image using imagemagick directly.



64
65
66
# File 'app/models/alchemy/picture/transformations.rb', line 64

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

#sizes_from_string(string = "0x0") ⇒ Object

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

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/alchemy/picture/transformations.rb', line 71

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

#square_format?Boolean Also known as: square?

Returns true if picture’s width and height is equal

Returns:

  • (Boolean)


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

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/alchemy/picture/transformations.rb', line 30

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