class PhotoCook::Resize::Resizer

Constants

CENTER_GRAVITY
TRANSPARENT_BACKGROUND

Public Instance Methods

resize(source_path, store_path, w, h, mode) click to toggle source
# 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) click to toggle source

Resize the photo to fill within the specified dimensions:

  • the original aspect ratio will be kept

  • new dimensions may vary

# 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) click to toggle source

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

# 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

Protected Instance Methods

open(source_path) click to toggle source
# File lib/photo-cook/resize/resizer.rb, line 69
def open(source_path)
  # MiniMagick::Image.open creates a temporary file for us and protects original
  photo = MagickPhoto.open(source_path)
  photo.validate!
  photo.source_path = source_path
  photo
end
process(photo:, store:) { |photo| ... } click to toggle source
# File lib/photo-cook/resize/resizer.rb, line 63
def process(photo:, store:)
  photo = open(photo)
  yield(photo)
  store(photo, store)
end
store(resized_photo, store_path) click to toggle source
# File lib/photo-cook/resize/resizer.rb, line 77
def store(resized_photo, store_path)
  path_to_dir = File.dirname(store_path)

  # Solution to broken symlinks.
  # This added here because mkdir -p can't detect broken symlinks.
  FileUtils.rm(path_to_dir) if !Dir.exists?(path_to_dir) && File.symlink?(path_to_dir)
  FileUtils.mkdir_p(path_to_dir)
  resized_photo.write(store_path)
  resized_photo.store_path = store_path
  resized_photo
end