Class: PhotoCook::Resize::Resizer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/photo-cook/resize/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(source_path, store_path, w, h, mode) ⇒ Object



18
19
20
# File 'lib/photo-cook/resize/resizer.rb', line 18

def resize(source_path, store_path, w, h, mode)
  send("resize_to_#{mode}", source_path, store_path, w, h)
end

#resize_to_fill(source_path, store_path, width, height) ⇒ Object

Resize the photo to fill within the specified dimensions:

  • the original aspect ratio will be kept

  • new dimensions may vary



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/photo-cook/resize/resizer.rb', line 41

def resize_to_fill(source_path, store_path, width, height)
  process photo: source_path, store: store_path do |photo|
    outw, outh = Calculations.size_to_fill(*photo[:dimensions], width, height)

    photo.combine_options do |cmd|
      cmd.resize     PhotoCook::Pixels.to_magick_dimensions(outw, outh) + '^'
      cmd.gravity    CENTER_GRAVITY
      cmd.background TRANSPARENT_BACKGROUND
      cmd.crop       PhotoCook::Pixels.to_magick_dimensions(outw, outh)  + '+0+0'
      cmd.repage.+
    end

    photo.resize_mode       = :fill
    photo.desired_width     = width
    photo.desired_height    = height
    photo.calculated_width  = outw
    photo.calculated_height = outh
  end
end

#resize_to_fit(source_path, store_path, width, height) ⇒ 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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/photo-cook/resize/resizer.rb', line 25

def resize_to_fit(source_path, store_path, width, height)
  process photo: source_path, store: store_path do |photo|
    outw, outh = Calculations.size_to_fit(*photo[:dimensions], width, height)
    photo.resize "#{PhotoCook::Pixels.to_magick_dimensions(outw, outh)}>"

    photo.resize_mode       = :fit
    photo.desired_width     = width
    photo.desired_height    = height
    photo.calculated_width  = outw
    photo.calculated_height = outh
  end
end