Sha256: 9591e1a413843f6e2b936207f399f4f3800ee77ca4f9e4c433475edd6e406a8d

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 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 run
    return options['steps'].map {|step| run_step(step) }
  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)
    name, cmd, opts, ext = step['name'], step['command'], step['options'], step['extension']
    in_path, out_path = input_path_for(step), output_path_for(step)
    `gm #{cmd} #{opts} #{in_path} #{out_path}`
    public_url = save(out_path)
    {'name' => name, 'url' => public_url}.to_json
  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

1 entries across 1 versions & 1 rubygems

Version Path
documentcloud-cloud-crowd-0.0.1 actions/graphics_magick.rb