module PhotoCook::Resize

Attributes

cache_dir[RW]
multiplier[RW]

Public Class Methods

base64_uri(uri, width, height, mode, options = {}) click to toggle source

TODO Change uri to source_path

# File lib/photo-cook/resize/__api__.rb, line 51
def base64_uri(uri, width, height, mode, options = {})
  w, h, m     = parse_options(width, height, mode, options)
  command     = Command.assemble(w, h, m)
  source_path = Assemble.assemble_source_path_from_normal_uri(PhotoCook.root_path, uri)
  store_path  = Assemble.assemble_store_path(PhotoCook.root_path, source_path, command)
  photo       = if File.readable?(store_path)
    MagickPhoto.new(store_path)
  else
    Resize.perform(source_path, store_path, w, h, m)
  end

  "data:#{photo.mime_type};base64,#{Base64.strict_encode64(File.read(photo.path))}"
end
base64_uri_from_source_path(source_path, store_path, width, height, mode, options = {}) click to toggle source
# File lib/photo-cook/resize/__api__.rb, line 65
def base64_uri_from_source_path(source_path, store_path, width, height, mode, options = {})
  w, h, m = parse_options(width, height, mode, options)
  photo   = if File.readable?(store_path)
    MagickPhoto.new(store_path)
  else
    Resize.perform(source_path, store_path, w, h, m)
  end

  "data:#{photo.mime_type};base64,#{Base64.strict_encode64(File.read(photo.path))}"
end
parse_options(width, height, mode, options, check_pixels_in_bounds = true) click to toggle source
# File lib/photo-cook/resize/__api__.rb, line 84
def parse_options(width, height, mode, options, check_pixels_in_bounds = true)
  multiplier  = options.fetch(:multiplier, self.multiplier).to_f
  mode        = Mode.parse!(mode)
  width       = Pixels.round(Pixels.parse(width)  * multiplier)
  height      = Pixels.round(Pixels.parse(height) * multiplier)
  Pixels.check!(width, check_pixels_in_bounds)
  Pixels.check!(height, check_pixels_in_bounds)
  [width, height, mode]
end
perform(source_path, store_path, w, h, mode) click to toggle source

Performs photo resizing with ImageMagick:

perform_resize('/application/public/uploads/car.png', '/application/public/resized_car.png', 280, 280)

Source file: /application/public/uploads/car.png Result file: /application/public/resized_car.png

NOTE: This method will perform validation NOTE: This method knows anything about resize cache

# File lib/photo-cook/resize/__api__.rb, line 22
def perform(source_path, store_path, w, h, mode)
  w, h, mode  = parse_options(w, h, mode, multiplier: 1.0)
  photo, msec = PhotoCook::Utils.measure do
    resizer.resize(source_path, store_path, w, h, mode)
  end
  PhotoCook.notify('resize', photo, msec)
  photo
end
resizer() click to toggle source
# File lib/photo-cook/resize/resizer.rb, line 7
def resizer
  Resizer.instance
end
static_asset_uri(uri, *rest) click to toggle source
# File lib/photo-cook/resize/__api__.rb, line 78
def static_asset_uri(uri, *rest)
  # If production assets are precompiled and placed into public so we can resize as usually
  Rails.application.config.serve_static_files ? uri : uri(uri, *rest)
end
strip(uri, check = false) click to toggle source

Inverse of PhotoCook#resize (see ^):

strip('/uploads/resize-cache/fit-280x280/car.png')
  => /uploads/car.png

NOTE: This method will perform validation

# File lib/photo-cook/resize/__api__.rb, line 45
def strip(uri, check = false)
  # TODO Implement check
  Assemble.disassemble_resize_uri(uri)
end
uri(uri, width, height, mode, options = {}) click to toggle source

Builds URI which points to PhotoCook::Middleware:

uri('/uploads/car.png', 280, 280)
  => /uploads/resize-cache/fit-280x280/car.png

NOTE: This method will perform validation

# File lib/photo-cook/resize/__api__.rb, line 36
def uri(uri, width, height, mode, options = {})
  Assemble.assemble_resize_uri(uri, *parse_options(width, height, mode, options, false))
end