Sha256: 18203e45d7125b3479de66c581d422c6a2aa88f47e489f06d97257329aef4642

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

# The GraphicsMagick action, dependent on the `gm` command, is able to perform
# any number of GraphicsMagick conversions on an image passed in as an input.
# The options hash should specify the +name+ for the particular step (which is
# appended to the resulting image filename) the +command+ (eg. convert, mogrify), 
# the +options+ (to the command, eg. -shadow -blur), and the +extension+ which 
# will determine the resulting image type. Optionally, you may also specify
# +input+ as the name of a previous step; doing this will use the result of
# that step as the source image, otherwise each step uses the original image
# as its source.
class GraphicsMagick < CloudCrowd::Action
  
  # Download the initial image, and run each of the specified GraphicsMagick
  # commands against it, returning the aggregate output.
  def process
    result = {}
    options['steps'].each {|step| result[step['name']] = run_step(step) }
    result.to_json
  end
  
  # Run an individual step (single GraphicsMagick command) in a shell-injection
  # safe way, uploading the result to the AssetStore, and returning the public
  # URL as the result.
  # TODO: +system+ wasn't working, figure out some other way to escape.
  def run_step(step)
    cmd, opts = step['command'], step['options']
    in_path, out_path = input_path_for(step), output_path_for(step)
    `gm #{cmd} #{opts} #{in_path} #{out_path}`
    save(out_path)    
  end
  
  # Where should the starting image be located?
  # If you pass in an optional step, returns the path to that step's output
  # as input for further processing.
  def input_path_for(step)
    in_step = step && step['input'] && options['steps'].detect {|s| s['name'] == step['input']}
    return input_path unless in_step
    return output_path_for(in_step)
  end
  
  # Where should resulting images be saved locally?
  def output_path_for(step)
    "#{work_directory}/#{file_name}_#{step['name']}.#{step['extension']}"
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
documentcloud-cloud-crowd-0.0.2 actions/graphics_magick.rb
documentcloud-cloud-crowd-0.0.3 actions/graphics_magick.rb
documentcloud-cloud-crowd-0.0.4 actions/graphics_magick.rb