Class: Elrio::ImageOptimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/elrio/image_optimizer.rb

Instance Method Summary collapse

Instance Method Details

#optimize(image, pixel_insets) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/elrio/image_optimizer.rb', line 3

def optimize(image, pixel_insets)
  target = target_size(pixel_insets)

  return if target.height > image.height || target.width > image.width

  source_x = image.width - pixel_insets.right
  source_y = image.height - pixel_insets.bottom
  target_x = target.width - pixel_insets.right
  target_y = target.height - pixel_insets.bottom
  source_width = pixel_insets.left + pixel_insets.pattern_width
  source_height = pixel_insets.top + pixel_insets.pattern_height

  optimized = ChunkyPNG::Image.new(target.width, target.height)

  copy_rect(
    image,
    optimized,
    Rect.new(0, 0, source_width, source_height),
    Point.new(0, 0)
  )

  copy_rect(
    image,
    optimized,
    Rect.new(source_x, 0, pixel_insets.right, source_height),
    Point.new(target_x, 0)
  )

  copy_rect(
    image,
    optimized,
    Rect.new(0, source_y, source_width, pixel_insets.bottom),
    Point.new(0, target_y)
  )

  copy_rect(
    image,
    optimized,
    Rect.new(source_x, source_y, pixel_insets.right, pixel_insets.bottom),
    Point.new(target_x, target_y)
  )

  optimized
end