module Neofiles

Constants

VERSION

Public Class Methods

crop_requested?(params) click to toggle source

Is request params hash contains crop request?

# File lib/neofiles.rb, line 80
def crop_requested?(params)
  params[:crop].present? and params[:crop] != '0'
end
is_admin?(context) click to toggle source

Is current user considered “admin”? “Admin” means the user can fetch images w/o watermarks.

# File lib/neofiles.rb, line 95
def is_admin?(context)
  Rails.application.config.neofiles.try(:current_admin).try(:call, context)
end
quality_requested(params) click to toggle source

The quality value requested, from request params hash.

# File lib/neofiles.rb, line 90
def quality_requested(params)
  params[:quality].to_i if params[:quality].present? and params[:quality] != '0'
end
quality_requested?(params) click to toggle source

Is request params hash contains quality request?

# File lib/neofiles.rb, line 85
def quality_requested?(params)
  !!quality_requested(params)
end
resized_image_dimensions(image_file, width, height, resize_options) click to toggle source

Calculate image dimensions after resize. Returns [w, h] or nil if some info is lacking (e.g. image passed as ID so no width & height available).

image_file      - Neofiles::Image, ID or Hash
width, height   - max width and height after resize
resize_options  - {crop: '1'/'0'}, @see Neofiles::ImagesController#show
# File lib/neofiles.rb, line 38
def resized_image_dimensions(image_file, width, height, resize_options)
  # dimensions are equal to requested ones if cropping
  return width, height if crop_requested? resize_options

  # otherwise ask ImageMagick - prepare input vars...
  image_file = Neofiles::Image.find image_file if image_file.is_a?(String)
  return nil if image_file.nil?

  if image_file.is_a? Neofiles::Image
    image_file_width = image_file.width
    image_file_height = image_file.height
  elsif image_file.is_a? Hash
    image_file_width = image_file[:width]
    image_file_height = image_file[:height]
  end

  # no input, terminate
  return if image_file_width.blank? || image_file_height.blank?

  # image fits into requested dimensions, no resizing will occur
  return image_file_width, image_file_height if image_file_width <= width && image_file_height <= height

  in_aspect   = 1.0 * image_file_width / image_file_height
  out_aspect  = 1.0 * width / height

  if in_aspect > out_aspect
    # If image is more "flat", the output width will always be equal to the requested width,
    # and the output height will be less than the requested height
    height = nil
  else
    # If input image is more "stretched" vertically or its aspect ratio is equal to output aspect ratio,
    # the output height will be equal to the requested height, and the output width will be less than or equal to the requested width
    width = nil
  end

  AspectRatio.resize(image_file_width, image_file_height, width, height).map(&:to_i)

rescue
  nil
end

Private Instance Methods

crop_requested?(params) click to toggle source

Is request params hash contains crop request?

# File lib/neofiles.rb, line 80
def crop_requested?(params)
  params[:crop].present? and params[:crop] != '0'
end
is_admin?(context) click to toggle source

Is current user considered “admin”? “Admin” means the user can fetch images w/o watermarks.

# File lib/neofiles.rb, line 95
def is_admin?(context)
  Rails.application.config.neofiles.try(:current_admin).try(:call, context)
end
quality_requested(params) click to toggle source

The quality value requested, from request params hash.

# File lib/neofiles.rb, line 90
def quality_requested(params)
  params[:quality].to_i if params[:quality].present? and params[:quality] != '0'
end
quality_requested?(params) click to toggle source

Is request params hash contains quality request?

# File lib/neofiles.rb, line 85
def quality_requested?(params)
  !!quality_requested(params)
end
resized_image_dimensions(image_file, width, height, resize_options) click to toggle source

Calculate image dimensions after resize. Returns [w, h] or nil if some info is lacking (e.g. image passed as ID so no width & height available).

image_file      - Neofiles::Image, ID or Hash
width, height   - max width and height after resize
resize_options  - {crop: '1'/'0'}, @see Neofiles::ImagesController#show
# File lib/neofiles.rb, line 38
def resized_image_dimensions(image_file, width, height, resize_options)
  # dimensions are equal to requested ones if cropping
  return width, height if crop_requested? resize_options

  # otherwise ask ImageMagick - prepare input vars...
  image_file = Neofiles::Image.find image_file if image_file.is_a?(String)
  return nil if image_file.nil?

  if image_file.is_a? Neofiles::Image
    image_file_width = image_file.width
    image_file_height = image_file.height
  elsif image_file.is_a? Hash
    image_file_width = image_file[:width]
    image_file_height = image_file[:height]
  end

  # no input, terminate
  return if image_file_width.blank? || image_file_height.blank?

  # image fits into requested dimensions, no resizing will occur
  return image_file_width, image_file_height if image_file_width <= width && image_file_height <= height

  in_aspect   = 1.0 * image_file_width / image_file_height
  out_aspect  = 1.0 * width / height

  if in_aspect > out_aspect
    # If image is more "flat", the output width will always be equal to the requested width,
    # and the output height will be less than the requested height
    height = nil
  else
    # If input image is more "stretched" vertically or its aspect ratio is equal to output aspect ratio,
    # the output height will be equal to the requested height, and the output width will be less than or equal to the requested width
    width = nil
  end

  AspectRatio.resize(image_file_width, image_file_height, width, height).map(&:to_i)

rescue
  nil
end