Development,  Tech Tips

Automatic Screenshots of Drupal Content

In an earlier post I recommended webkit2png for automatically screenshotting a list of URLs. A lot of time has passed since that post, and I’ve discovered a more robust tool. Pageres is incredible, and it has a CLI and an api.

I’ll let you discover, on your own, what the Pageres tool can do. I needed to take screenshots of all of the content types on a site, at all of the important resolutions. Here’s a quick Drupal function I threw together to get N number of random nodes per content type:

The function spits out a list of URLs ready for usage with pageres. Simply save the results to a txt file (urls.txt in my example below).

Here’s the pageres command I used to generate the screenshots:

Why the 100-pixel height? Well, the height doesn’t really matter unless you enable cropping. I use 100 on all of them so that it’s obvious the value doesn’t mean anything. I tried 1200×1 but it breaks pageres. 1200×100 works perfectly.

How about another quick function? Here’s one to generate a list of URLs within a menu:

Now, how does this handle many URLs? Well, unfortunately not that well. Python comes to the rescue in just a few lines of simple code. This will process one URL at a time, generating all resolutions for each URL. I’m certain this could be better (filename should be an argument, for example), but it gets the job done.

UPDATE #1: Here’s a rough draft of a Python script that is a little more robust than the code above. It still lacks some niceties, but I’ll just wait until next time I need it to make improvements.

You would execute this like: python ~/repos/pageres_capture/ urls.txt

UPDATE #2: Here’s a version that appends the URL to the top of the screenshot using ImageMagick. You can turn it off using –no-overlay. As with the code above, this is alpha code. As I’m looking at it it’s clear I should make “sizes” an argument/switch. In fact, I should probably allow several of the pageres options.

This requires ImageMagick. Before running, you must be able to run mogrify  successfully from the command line.

Update #3: Same as above but corrects behavior if a URL is not accessible (and shows an error as it encounters those). This still only works for Python 2.7:


One Comment

Leave a Reply

Your email address will not be published.