Generators

Unlike processors and encoders, generators create content out of nothing, rather than modifying already existing content, for example text image generation.

You can register as many generators as you like.

Given a Dragonfly app

app = Dragonfly[:images]

we can get generated content using

image = app.generate(:some_method, :some => :args)

where :some_method is added by the configured generators.

ImageMagick Generator

See Imagemagick.

Custom Generators

To register a single custom generator:

app.generator.add :blank_image do |colour|
  SomeLibrary.create_blank_image(colour)     # return a String, Pathname, File or Tempfile
end

app.generate(:blank_image, 'red')      # => 'Job' object which we can get data, etc.

Or create a class like the ImageMagick one above, in which case all public methods will be counted as generator methods.

class RoundedCornerGenerator

  def top_left_corner(opts={})
    SomeLib.tlc(opts)
  end

  def bottom_right_corner(opts={})
    tempfile = Tempfile.new('brc')
    `some_command -c #{opts[:colour]} -o #{tempfile.path}`
    tempfile
  end

  # ...

  private

  def my_helper_method
    # do stuff
  end

end

app.generator.register(RoundedCornerGenerator)

app.generate(:top_left_corner, :colour => 'green')
app.generate(:bottom_right_corner, :colour => 'mauve')