Generating Website Screenshots with PHP

Posted 25th January 2016

In this post, I’m going to intro­duce a PHP pack­age I recently released for gen­er­at­ing screen­shots of web pages. It’s pretty straight­for­ward to get up-​and-​running, as I’ll demonstrate.

Why Gen­er­ate Screenshots?

There are a num­ber of rea­sons you may wish to gen­er­ate screen­shots. My moti­va­tion behind the pack­age orig­i­nally was to build a sim­ple “direc­tory” of web­sites, using screen­shots to illus­trate the sites.

Screen­shots are also use­ful in test­ing; you could even run a script to peri­od­i­cally gen­er­ate a screen­shot of one of the sites you main­tain in order to check that every­thing is ren­der­ing okay.

What are the Options?

Before I intro­duce the pack­age, it’s worth men­tion­ing that there are a num­ber of third-​party ser­vices which make it even eas­ier to gen­er­ats screen­shots such as Brow­shot, URL2PNG, Shrink the Web and Url­box — but it’s worth not­ing that these are all paid ser­vices. If you’d rather do it your­self, or do it on-​the-​cheap, you might like to try out my package.

Working with UK Postcodes in PHP

Posted 6th March 2014

I’ve cre­ated a sim­ple pack­age for work­ing with UK Post­codes. It con­tains two prin­ci­ple components:

  1. A sim­ple class to encap­su­late UK Postcodes
  2. A wrap­per to this web ser­vice

The post­code class can be used for:

  • val­i­dat­ing postcodes
  • for­mat­ting them, e.g. “sw1a2aa” -> “SW1A 2AA
  • get­ting the out­code (the first part of a post­code; e.g. M1, SW1A, GL9)

The meat and bones of the pack­age, though, is the web ser­vice. Most notably, it allows you to take a post­code, and find out its lat­i­tude and lon­gi­tude. Here’s an exam­ple of how it’s used:

// Create the client
$client = new Lukaswhite\UkPostcodes\UkPostcodesClient();       

// Call the web service
$postcode = $client->postcode('sw1a2aa');

print get_class($postcode);
// Lukaswhite\UkPostcodes\UkPostcode

print $postcode->formatted();
// SW1A 2AA

print get_class($postcode->getCoordinate());
// League\Geotools\Coordinate\Coordinate

print $postcode->getCoordinate()->getLatitude();
// 51.503539898876

print $postcode->getCoordinate()->getLongitude();
// -0.12768084037293

print get_class($postcode->getCoordinate()->getEllipsoid());
// League\Geotools\Coordinate\Ellipsoid 

print $postcode->council->title;
// City of Westminster

print $postcode->council->code;
// E09000033

print $postcode->council->uri;
// http://statistics.data.gov.uk/id/statistical-geography/E09000033

print $postcode->ward->title;
// St. James's

print $postcode->constituency->title;
// Cities of London and Westminster

Getting Track Artwork from Spotify using PHP

Posted 14th December 2012

At time of writ­ing, Spo­tify doesn’t pro­vide a track’s art­work via its API.

So, I’ve writ­ten a sim­ple func­tion to get the image; but please note, not only is it quick and dirty, but it’s hor­ri­bly inef­fi­cient and should be used spar­ingly — at the very least, make sure you cache the results somewhere!

You can access a public-​facing web-​page about a par­tic­u­lar track using the fol­low­ing URL:

http://open.spotify.com/track/[TRACK-ID]

…where [TRACK-​ID] is the last part of the track’s URI — so for exam­ple, for the track rep­re­sented by the URI spotify:track:5fpizYGbi5IQoEraj6FP0R, the track ID would be 5fpizYGbi5IQoEraj6FP0R, and thus the URL to the page would be:

The Laravel PHP Framework: A Walkthrough

Posted 5th August 2011

Lar­avel is the new kid on the block of PHP frame­works, and whilst in its early stages of evo­lu­tion (the author wrote it, he says, in the early part of 2011) it does promise — and deliv­ers, from what I can tell — an expres­sive syn­tax and an ele­gant (indeed, you might say elo­quent) ORM. Lar­avel is a fully Object Ori­ented MVC frame­work which makes exten­sive use of fea­tures new or recent to PHP — you can for­get about run­ning it on PHP4. With PHP5.3 com­pli­ant name­spacing mech­a­nisms as opposed to, for exam­ple, Zend Framework’s pseudo-​namespacing, it’s cer­tainly what you might call a mod­ern framework.

Routes

In Lar­avel you define routes — which, stay­ing true to the REST­ful par­a­digm, are defined not just as URI’s, but accord­ing to the HTTP oper­a­tion they are to react to. Thus, instead of defin­ing a route such as /about-us you would define a func­tion to respond to a GET request on that URI, which is done using a clo­sure which looks reas­sur­ingly like JavaScript:

A Concrete5 Development Tutorial; Creating a Quote Block

Posted 7th July 2011

I needed the means to add a quote to a web­site I was work­ing on — a sim­ple task, which could be acheieved by sim­ply adding a text or HTML block. How­ever this can be taken a step fur­ther by imple­ment­ing a sim­ple block type specif­i­cally for quotes, with the markup gen­er­ated for you.

To mark up a quote seman­ti­cally, we’ll want a blockquote for the con­tent and a cite for the source (or author), so the HTML needs to look some­thing like this:

<blockquote>
<p>It is the mark of an educated mind to be able to entertain a thought without accepting it.</p>
<p><cite>Aristotle</cite></p>
</blockquote>

…which will give us some­thing a lit­tle like this:

Creating a Concrete5 Package: Integrating the Craftyslide JQuery Plugin

Posted 5th July 2011

In this sim­ple tuto­r­ial, I’m going to show how Concrete5 can be extended to inte­grate a third-​party JQuery plu­gin to enhance an exist­ing mod­ule, and then how this can be encap­su­lated in a pack­age. I’m going to allow the slideshow mod­ule to dis­play images using the clean & light­weight Craftys­lide plu­gin, pic­tured below.

The Craftyslide JQuery Slider plugin in action
The Craftys­lide plu­gin in action

Concrete5 comes shipped with sim­ple slideshow func­tion­al­ity in the form of a block called, imag­i­na­tively enough, slideshow. As this is part of the core you can find it in /concrete/blocks/slideshow. It’s in the con­crete direc­tory to keep it sep­a­rate from cus­tom code. And some cus­tom code is what we’re going to pro­duce — to over­ride the out­put of that block, and then to encap­su­late this into a package.

Taxonomy Colour: Drupal Module Development Tutorial (Part Two)

Posted 4th July 2011

In the first part, I looked at cre­at­ing the data­base schema for a sim­ple Dru­pal mod­ule designed to allow you to asso­ciate colours with tax­on­omy terms. In this sec­ond part, I’ll look at the admin­is­tra­tion aspects of the mod­ule. In essence, what we need to do is as follows:

  • Since colour-​coding may only be appro­pri­ate to cer­tain vocab­u­lar­ies (e.g. cat­e­gories) and not oth­ers (tags, per­haps). We need to pro­vide the user with the option to spec­ify which vocab­u­lar­ies are applicable.
  • The user needs to be able to spec­ify a colour when adding or edit­ing a term, where appropriate.
  • We need to pro­vide a mech­a­nism which allows the colour asso­ci­ated with a term to be stored, retrieved, and to be displayed.