Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
93 lines (59 sloc) 3.07 KB


Photograph allows to take screenshots of webpages, the rendering being done by webkit thanks to PhantomJS and Poltergeist.

Typically, this can be used to generate previews for DOM based documents.

Please remind that having a webkit instance doing the rendering, even if it is launched only once then reused, is slow, around 600ms!


PhantomJS is required and must be available in your path. See installation instructions.

Then you can add this line to your application's Gemfile:

gem 'photograph'

And then execute:

$ bundle

Or install it yourself as:

$ gem install photograph


Photograph can be used either directly through the Photograph::Artist class or by its little Sinatra app.


Using Artist.shoot! without a block had been deprecated in 0.3 and will raise an exception.

Artist can be instanciated with an url to screenshot, calling #shoot! will take the screenshot and yields it. The screenshot is only accessible within the block as it is always deleted afterward.

@artist = => "")
@artist.shoot! do |image|
  image # => MiniMagick instance you can toy with

  # Sinatra? Serve screenshot to client with:
  send_file image.path, :type => :png

  # Jobs or Rake task ? Export screenshot with:
  FileUtils.cp(image.path, "/somewhere/image.png")


To crop the screenshot, accepts :x, y, w and h options:

@artist = => "", :x => 100, :y => 100, :w => 800, :h => 600)

This will take a 800x600 screenshot, skipping 100 pixels top and left.

Interacting with the page before shooting

If authentication is required (or any browser action), Artist#before allows to supply actions to be done before shooting. It yields a Capybara browser instance that respond to the usual methods, fill_in, click_link see for more details.

@artist = => "")
@artist.before do |browser|
  browser.click_button("Sign in")
  browser.fill_in("Username", :with => "MyUsername")
  # ...

@artist.shoot! do |image|
  send_file image, :type => :png


Full Documentation

As a web service

As having the web service builtin requires Sinatra and Thin, it had been extracted to Photograph-Service.


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request


  • Jean-Hadrien Chabran
  • Danial Pearce


Photograph is Copyright © 2013 JHCHABRAN. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.