module ImageResizer::Utils

Private Instance Methods

convert(temp_object=nil, args='', format=nil) click to toggle source
# File lib/image_resizer/utils.rb, line 14
def convert(temp_object=nil, args='', format=nil)
  tempfile = new_tempfile(format)
  interpolation_data = {}

  if temp_object.is_a?(Array)
    temp_object.length.times { |index| interpolation_data["input#{index}".to_sym] = temp_object[index].path }
    keys = interpolation_data.keys.map { |s| ":#{s}" }
    args = "#{keys.join(' ')} #{args} :output"
  elsif temp_object
    interpolation_data[:input] = temp_object.path
    args = ":input #{args} :output"
  else
    args = "#{args} :output"
  end

  interpolation_data[:output] = tempfile.path

  line = Cocaine::CommandLine.new(convert_command, args)
  line.run(interpolation_data)

  tempfile
end
identify(temp_object) click to toggle source
# File lib/image_resizer/utils.rb, line 37
def identify(temp_object)
  # example of details string:
  # myimage.png PNG 200x100 200x100+0+0 8-bit DirectClass 31.2kb
  format, width, height, depth = raw_identify(temp_object).scan(/([A-Z0-9]+) (\d+)x(\d+) .+ (\d+)-bit/)[0]
  {
    :format => format.downcase.to_sym,
    :width => width.to_i,
    :height => height.to_i,
    :depth => depth.to_i
  }
end
new_tempfile(ext=nil) click to toggle source
# File lib/image_resizer/utils.rb, line 58
def new_tempfile(ext=nil)
  tempfile = ext ? Tempfile.new(['ImageResizer', ".#{ext}"]) : Tempfile.new('ImageResizer')
  tempfile.binmode
  tempfile.close
  tempfile
end
raw_identify(temp_object, args='') click to toggle source
# File lib/image_resizer/utils.rb, line 53
def raw_identify(temp_object, args='')
  line = Cocaine::CommandLine.new(identify_command, "#{args} :input")
  line.run(:input => temp_object.path)
end
verbose_identify(temp_object) click to toggle source
# File lib/image_resizer/utils.rb, line 49
def verbose_identify(temp_object)
  raw_identify(temp_object, '-verbose')
end