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.



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

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.



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

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)


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

def crop_size?
  self.respond_to?(:crop_size) && !self.crop_size.nil? && !self.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.



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

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



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

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)


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

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)


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

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)


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

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

#resize(size, upsample = false) ⇒ Object

Returns the rendered resized image using imagemagick directly.



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

def resize(size, upsample = false)
  self.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)


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

def sizes_from_string(string = "0x0")
  string = "0x0" if string.nil? || string.empty?

  raise ArgumentError unless string.match(/(\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)


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

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.



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

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