class Media::GraphicsMagickTransmogrifier

Public Class Methods

available?() click to toggle source
# File lib/media/transmogrifiers/graphicsmagick.rb, line 33
def self.available?
  command_available?(GRAPHICSMAGICK_COMMAND)
end
average_color(filename) click to toggle source

returns the average color of an image, as represented by an array of red, green, blue values, integers in the range 0..255

note: it is important that the geometry is “1x1!” … without the ! this function might die a fiery death.

# File lib/media/transmogrifiers/graphicsmagick.rb, line 101
def self.average_color(filename)
  if available?
    args = [gm_command, 'convert', '-resize', '1x1!', filename, 'text:-']
    color = nil
    status, color = run_command(*args)
    if status == :success
      match = color.match(/^0,0: \(\s*(?<red>\d+),\s*(?<green>\d+),\s*(?<blue>\d+)\)/)
      if match
        return [match['red'].to_i, match['green'].to_i, match['blue'].to_i]
      end
    end
  end
  #if something goes wrong, assume white:
  return [256,256,256]
end
dimensions(filename) click to toggle source

try to detect the dimensions of the first page. fallback to detecting dimensions of all pages.

# File lib/media/transmogrifiers/graphicsmagick.rb, line 89
def self.dimensions(filename)
  return unless available?
  run_dimensions(filename.to_s + '[0]') ||
    run_dimensions(filename.to_s)
end
input_types() click to toggle source
# File lib/media/transmogrifiers/graphicsmagick.rb, line 21
def self.input_types
  %w( image/jpeg image/pjpeg image/gif
    image/png image/x-png image/jpg image/tiff )
  # application/pdf application/bzpdf application/gzpdf
  # application/postscript application/xpdf
end
output_types() click to toggle source
# File lib/media/transmogrifiers/graphicsmagick.rb, line 28
def self.output_types
  %w( application/pdf image/jpeg image/pjpeg
    image/gif image/png image/jpg image/tiff )
end

Protected Class Methods

gm_command() click to toggle source

this override is just used for test, at the moment.

# File lib/media/transmogrifiers/graphicsmagick.rb, line 129
def self.gm_command
  GRAPHICSMAGICK_COMMAND
end
run_dimensions(filename) click to toggle source
# File lib/media/transmogrifiers/graphicsmagick.rb, line 119
def self.run_dimensions(filename)
  args = [gm_command, 'identify', '-format', '%m %w %h', filename]
  status, dimensions = run_command(*args)
  if status == :success
    _type, width, height = dimensions.split(/\s/)
    return [width,height]
  end
end

Public Instance Methods

convert(input = input_file.to_s, &block) click to toggle source
# File lib/media/transmogrifiers/graphicsmagick.rb, line 61
def convert(input = input_file.to_s, &block)
  # +profile '*' will remove all the image profiles, which will save
  # space (sometimes) and are not useful for thumbnails
  arguments = [self.class.gm_command, 'convert', '+profile', "*"]
  if options[:size]
    # handle multiple size options, if it is an array.
    sizes = options[:size].is_a?(Array) ? options[:size] : [options[:size]]
    sizes.each do |size|
      if version_less_than?(1,3,6)
        size = size.sub('^','!')
      end
      arguments << '-geometry' << size
    end
  end
  if options[:background]
    # http://superuser.com/questions/213336/using-graphicsmagick-or-imagemagick-how-do-i-replace-transparency-with-a-fill-c
    arguments << '-background' << options[:background] << '-extent' << '0x0'
  end
  if options[:crop]
    # we add '+0+0' because we don't want tiles, just a single image
    arguments << '-crop' << options[:crop]+'+0+0'
  end
  arguments << input << output_file
  run_command(*arguments, &block)
end
run(&block) click to toggle source

gm has an option -monitor that will spit out the progress. this could be interesting. we would need to use getc instead of gets on the pipe, since the progress is updated on a single line.

# File lib/media/transmogrifiers/graphicsmagick.rb, line 42
def run(&block)
  if !File.exist?(input_file.to_s) ||
    ( File.size(input_file.to_s) == 0 )
    debug "Could not find input file: #{input_file}"
    return :not_fould
  end

  # try converting first page only
  status = convert(input_file.to_s + '[0]', &block)
  # retry with full file if result was empty
  if File.exist?(output_file.to_s) && File.size(output_file.to_s) == 0
    # reset filenames to the state before run
    set_temporary_outfile
    status = convert(&block)
  end
  FileUtils.chmod 0644, output_file.to_s if File.exist? output_file.to_s
  return status
end

Protected Instance Methods

version_less_than?(major,minor,tiny) click to toggle source
# File lib/media/transmogrifiers/graphicsmagick.rb, line 133
def version_less_than?(major,minor,tiny)
  installed_major, installed_minor, installed_tiny = GRAPHICSMAGICK_VERSION
  if installed_major < major
    true
  elsif (installed_major == major)
    if (installed_minor < minor)
      true
    elsif (installed_minor == minor) && (installed_tiny < tiny)
      true
    else
      false
    end
  else
    false
  end
end