Class: PhotoCook::Resizer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/photo-cook/resizer.rb

Constant Summary collapse

CENTER_GRAVITY =
'Center'.freeze
TRANSPARENT_BACKGROUND =
'rgba(255,255,255,0.0)'.freeze

Instance Method Summary collapse

Instance Method Details

#resize(photo_path, width, height, pixel_ratio = 1.0, crop = false) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/photo-cook/resizer.rb', line 8

def resize(photo_path, width, height, pixel_ratio = 1.0, crop = false)
  if crop
    resize_to_fill(photo_path, width, height, pixel_ratio)
  else
    resize_to_fit(photo_path, width, height, pixel_ratio)
  end
end

#resize_to_fill(photo_path, width, height, pixel_ratio) ⇒ Object

Resize the photo to fit within the specified dimensions:

  • new dimensions will be the same as specified

  • the photo will be cropped if necessary

github.com/carrierwaveuploader/carrierwave/blob/71cb18bba4a2078524d1ea683f267d3a97aa9bc8/lib/carrierwave/processing/mini_magick.rb#L176



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/photo-cook/resizer.rb', line 39

def resize_to_fill(photo_path, width, height, pixel_ratio)

  # Do nothing if photo is not valid so exceptions will be not thrown
  return unless (photo = open(photo_path)) && photo.valid?

  store_path      = assemble_store_path(photo_path, width, height, pixel_ratio, true)
  cols, rows      = photo[:dimensions]
  mwidth, mheight = multiply_dimensions(width, height, pixel_ratio)

  # TODO
  # Original dimensions are 1000x800. You want 640x640@1x. You will get 640x640
  # Original dimensions are 1000x800. You want 640x640@2x. You will get 800x800
  # Original dimensions are 1000x800. You want 640x640@3x. You will get 800x800
  # Original dimensions are 1000x800. You want 1280x1280@1x. You will get ?
  # Original dimensions are 1000x800. You want 1000x1280@1x. You will get ?

  photo.combine_options do |cmd|
    if width != cols || height != rows
      scale_x = width / cols.to_f
      scale_y = height / rows.to_f
      if scale_x >= scale_y
        cols = (scale_x * (cols + 0.5)).round
        rows = (scale_x * (rows + 0.5)).round
        cmd.resize "#{cols}>"
      else
        cols = (scale_y * (cols + 0.5)).round
        rows = (scale_y * (rows + 0.5)).round
        cmd.resize "x#{rows}>"
      end
    end
    cmd.gravity CENTER_GRAVITY
    cmd.background TRANSPARENT_BACKGROUND
    if cols != width || rows != height
      cmd.extent "#{literal_dimensions(width, height)}>"
    end
  end

  store(photo, store_path)
end

#resize_to_fit(photo_path, width, height, pixel_ratio) ⇒ Object

Resize the photo to fit within the specified dimensions:

  • the original aspect ratio will be kept

  • new dimensions will be not larger then the specified

github.com/carrierwaveuploader/carrierwave/blob/71cb18bba4a2078524d1ea683f267d3a97aa9bc8/lib/carrierwave/processing/mini_magick.rb#L131



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/photo-cook/resizer.rb', line 21

def resize_to_fit(photo_path, width, height, pixel_ratio)

  # Do nothing if photo is not valid so exceptions will be not thrown
  return unless (photo = open(photo_path)) && photo.valid?

  store_path    = assemble_store_path(photo_path, width, height, pixel_ratio, false)
  width, height = multiply_dimensions(width, height, pixel_ratio)

  photo.combine_options { |cmd| cmd.resize "#{literal_dimensions(width, height)}>" }

  store(photo, store_path)
end