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.

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 string - unless upsample is true.



114
115
116
117
118
# File 'app/models/alchemy/picture/transformations.rb', line 114

def can_be_cropped_to(string, upsample = false)
  dimensions = sizes_from_string(string)
  return true if upsample
  is_bigger_than(dimensions)
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.



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/alchemy/picture/transformations.rb', line 45

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)


128
129
130
# File 'app/models/alchemy/picture/transformations.rb', line 128

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.



12
13
14
15
16
17
18
19
20
21
# File 'app/models/alchemy/picture/transformations.rb', line 12

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



104
105
106
107
108
109
# File 'app/models/alchemy/picture/transformations.rb', line 104

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)


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

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)


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

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)


122
123
124
# File 'app/models/alchemy/picture/transformations.rb', line 122

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

#resize(size, upsample = false) ⇒ Object

Returns the rendered resized image using imagemagick directly.



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

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)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/alchemy/picture/transformations.rb', line 66

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)


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

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/alchemy/picture/transformations.rb', line 25

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: 111, height: 93}, size)
  "#{size[:width]}x#{size[:height]}"
end