class ImageResizer::Processor

Constants

CROPPED_RESIZE_GEOMETRY
CROP_GEOMETRY
GRAVITIES
RESIZE_GEOMETRY

Geometry string patterns

THUMB_GEOMETRY

Public Instance Methods

_resize(temp_object, geometry, format=nil) click to toggle source
# File lib/image_resizer/processor.rb, line 48
def _resize(temp_object, geometry, format=nil)
  convert(temp_object, "-resize '#{geometry}'", format)
end
auto_orient(temp_object) click to toggle source
# File lib/image_resizer/processor.rb, line 52
def auto_orient(temp_object)
  convert(temp_object, "-auto-orient")
end
convert(temp_object, args='', format=nil) click to toggle source
Calls superclass method ImageResizer::Utils#convert
# File lib/image_resizer/processor.rb, line 220
def convert(temp_object, args='', format=nil)
  super
end
crop(temp_object, opts={}) click to toggle source
# File lib/image_resizer/processor.rb, line 56
def crop(temp_object, opts={})
  width   = opts[:width]
  height  = opts[:height]
  gravity = GRAVITIES[opts[:gravity]]
  x       = "#{opts[:x] || 0}"
  x = '+' + x unless x[/^[+-]/]
  y       = "#{opts[:y] || 0}"
  y = '+' + y unless y[/^[+-]/]
  repage  = opts[:repage] == false ? '' : '+repage'

  resize = opts[:resize] ? "-resize #{opts[:resize]} " : ''
  gravity = gravity ? "-gravity #{gravity} " : ''

  convert(temp_object, "#{resize}#{gravity}-crop #{width}x#{height}#{x}#{y} #{repage}", opts[:format])
end
crop_to_frame_and_resize(temp_object, options) click to toggle source
# File lib/image_resizer/processor.rb, line 128
def crop_to_frame_and_resize(temp_object, options)
  analyzer = ImageResizer::Analyzer.new

  desired_width = options[:width].to_i
  desired_height = options[:height].to_i

  upper_left_x_percent = options[:upper_left].first
  upper_left_y_percent = options[:upper_left].last

  lower_right_x_percent = options[:lower_right].first
  lower_right_y_percent = options[:lower_right].last

  original_width = analyzer.width(temp_object)
  original_height = analyzer.height(temp_object)

  upper_left_x = (original_width * upper_left_x_percent).round
  upper_left_y = (original_height * upper_left_y_percent).round
  frame_width = (original_width * (lower_right_x_percent - upper_left_x_percent)).round
  frame_height = (original_height * (lower_right_y_percent - upper_left_y_percent)).round

  if desired_width == 0 && frame_height > 0
    ratio = frame_width.to_f / frame_height
    desired_width = (desired_height * ratio).round
  end

  if desired_height == 0 && frame_width > 0
    ratio = frame_height.to_f / frame_width
    desired_height = (desired_width * ratio).round
  end


  convert(temp_object, "-crop #{frame_width}x#{frame_height}+#{upper_left_x}+#{upper_left_y} -resize #{desired_width}x#{desired_height} +repage", options[:format])
end
flip(temp_object) click to toggle source
# File lib/image_resizer/processor.rb, line 162
def flip(temp_object)
  convert(temp_object, "-flip")
end
flop(temp_object) click to toggle source
# File lib/image_resizer/processor.rb, line 166
def flop(temp_object)
  convert(temp_object, "-flop")
end
generate_icon(temp_object, options={}) click to toggle source
# File lib/image_resizer/processor.rb, line 224
def generate_icon(temp_object, options={})
  max_resolution = [options[:max_resolution] || 256, 256].min
  largest_png = convert(temp_object, "-resize #{max_resolution}x#{max_resolution}! -transparent white", :png)
  formats = []
  current = 16
  while current < max_resolution
    formats << convert(largest_png, "-resize #{current}x#{current}! -transparent white")
    current *= 2
  end
  formats << largest_png
  convert(formats, '', :ico)
end
grayscale(temp_object)
Alias for: greyscale
greyscale(temp_object) click to toggle source
# File lib/image_resizer/processor.rb, line 170
def greyscale(temp_object)
  convert(temp_object, "-colorspace Gray")
end
Also aliased as: grayscale
resize(temp_object, options={}) click to toggle source
# File lib/image_resizer/processor.rb, line 26
def resize(temp_object, options={})
  width = options[:width].to_i
  height = options[:height].to_i

  if height == 0 && width == 0
    temp_object.file
  elsif height == 0
    _resize(temp_object, "#{width}x", options[:format])
  elsif width == 0
    _resize(temp_object, "x#{height}", options[:format])
  else
    if options[:crop_from_top_if_portrait]
      analyzer = ImageResizer::Analyzer.new
      center_of_gravity = analyzer.aspect_ratio(temp_object) >= 1 ? 'c' : 'n'
    else
      center_of_gravity = 'c'
    end

    resize_and_crop(temp_object, :width => width.to_i, :height => height.to_i, :gravity => center_of_gravity, :format => options[:format])
  end
end
resize_and_crop(temp_object, opts={}) click to toggle source
# File lib/image_resizer/processor.rb, line 175
def resize_and_crop(temp_object, opts={})
  if !opts[:width] && !opts[:height]
    if opts[:format]
      return convert(temp_object, '', opts[:format])
    else
      return temp_object
    end
  elsif !opts[:width] || !opts[:height]
    attrs          = identify(temp_object)
    opts[:width]   ||= attrs[:width]
    opts[:height]  ||= attrs[:height]
  end

  opts[:gravity] ||= 'c'

  opts[:resize]  = "#{opts[:width]}x#{opts[:height]}^^"
  crop(temp_object, opts)
end
resize_and_crop_around_point(temp_object, options) click to toggle source
# File lib/image_resizer/processor.rb, line 72
def resize_and_crop_around_point(temp_object, options)
  analyzer = ImageResizer::Analyzer.new

  desired_width = options[:width].to_i
  desired_height = options[:height].to_i
  desired_ratio = desired_height > 0 ? desired_width.to_f / desired_height : 0

  original_width = analyzer.width(temp_object)
  original_height = analyzer.height(temp_object)
  original_ratio = original_width.to_f / original_height

  if desired_ratio > original_ratio
    width = original_width
    height = width / desired_ratio
  else
    height = original_height
    width = height * desired_ratio
  end

  focus_x = options[:point][0] * original_width
  focus_y = options[:point][1] * original_height

  half_width = width * 0.5
  half_height = height * 0.5

  upper_left_x = [focus_x - half_width, 0].max
  upper_left_y = [focus_y - half_height, 0].max

  lower_right_x = upper_left_x + width
  lower_right_y = upper_left_y + height

  x_offset = [lower_right_x - original_width, 0].max
  y_offset = [lower_right_y - original_height, 0].max

  upper_left_x -= x_offset
  upper_left_y -= y_offset

  lower_right_x -= x_offset
  lower_right_y -= y_offset

  upper_left_x_percent = upper_left_x / original_width
  upper_left_y_percent = upper_left_y / original_height

  lower_right_x_percent = lower_right_x / original_width
  lower_right_y_percent = lower_right_y / original_height

  crop_to_frame_and_resize(temp_object,
                          :upper_left => [upper_left_x_percent, upper_left_y_percent],
                          :lower_right => [lower_right_x_percent, lower_right_y_percent],
                          :width => desired_width,
                          :height => desired_height,
                          :format => options[:format]
                          )
end
rotate(temp_object, amount, opts={}) click to toggle source
# File lib/image_resizer/processor.rb, line 194
def rotate(temp_object, amount, opts={})
  convert(temp_object, "-rotate '#{amount}#{opts[:qualifier]}'")
end
strip(temp_object) click to toggle source
# File lib/image_resizer/processor.rb, line 198
def strip(temp_object)
  convert(temp_object, "-strip")
end
thumb(temp_object, geometry) click to toggle source
# File lib/image_resizer/processor.rb, line 202
def thumb(temp_object, geometry)
  case geometry
  when RESIZE_GEOMETRY
    resize(temp_object, geometry)
  when CROPPED_RESIZE_GEOMETRY
    resize_and_crop(temp_object, :width => $1, :height => $2, :gravity => $3)
  when CROP_GEOMETRY
    crop(temp_object,
      :width => $1,
      :height => $2,
      :x => $3,
      :y => $4,
      :gravity => $5
    )
  else raise ArgumentError, "Didn't recognise the geometry string #{geometry}"
  end
end