Pulling webcam images with PHP

When Sandy was about to hit New York, @jorgenb came up with a great idea. Download the images from the NY-times webcam and make a timelapse of it. A few minutes later I had a very simple script ready, and could leave my server to the task.

The video I posted on thomas.bensmann.no quickly passed 160K views on Vimeo, after being featured on several news-sites, and several people have since asked how it was made. Therefore I am posting the script that downloaded the images, which really is very simple.


//folder to store images in
$folder = "/files";

//url to latest webcam image
$url = "http://graphics8.nytimes.com/.../latest.jpg";

//get folder contents so far
$array = scandir(dirname(__FILE__) . $folder);

//i stored the images as 1.jpg, 2.jpg, etc.
//the first two elements found in the folder are "." and ".."
$num = (count($array) - 1);

//create the new filename
$filename = dirname(__FILE__) . $folder . "/" . $num . ".jpg";

//get the image
$ny_times_image = file_get_contents($url);

//save the image
file_put_contents( $filename , $ny_times_image );

Here is the cron job i added to create an image every minute:

* * * * * /usr/bin/php /var/www/labs.bensmann.no/html/ny_times_fetch.php

When the pictures were gathered I used an old version of QuickTime to put them together in a time lapse video.

Quick tip: Don’t forget to remove the cron job! My last image was called 116327.jpg, do the math.