Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Saturday, July 11, 2009

Doppler image animation with PHP and ImageMagick

Gnome Weather panel provides a radar map using an image URL to show a current weather forecast. Radar map has a support for animated gifs which drove me to develop a little PHP script to make a doppler animation.

I found out that Foreca provides a set of dopper images showing the next 7 hours forecast in Finland. Images have well formed filenames, so there's an easy way to make a script to download them periodically.

Dependencies: ImageMagick and a truetype font

// Nice little script I made courtesy to Gnome Weather Report panel's radar image
chdir(dirname(__FILE__));
exec('rm *.gif');
// download doppler images (foreca provides next 7 hours ahead)
for( $i = 0; $i < 7; $i++ ) {
$hour = date('H')+$i;
$hour = $hour%24;
$hour = str_pad($hour, 2, "0", STR_PAD_LEFT);

$date = date('Ymd').$hour;
$file = 'fin-'.$date.'.gif';

// thanks to taloussanomat :)
$url = 'http://http.foreca.com/xfer/taloussanomat/precipanim/'.$file;
exec( 'wget '.$url );

$date_str = $hour.':00';
// make a date image
exec( 'convert -size 50x20 xc:transparent -font '.dirname(__FILE__).'/tahoma.ttf -fill red '.
'-pointsize 14 -draw "text 5,15 \''.$date_str.'\'" date.png ' );
// add date image on doppler image
exec( 'composite -gravity southeast date.png '.$file.' '.$file );
}

// generate animation (1s delay, infinite loop)
exec( 'convert -delay 100 -loop 0 fin-*.gif doppleranimation.gif' );


The script can be added to crontab, to make the doppler animation refresh once an hour. Working animation can be seen here http://panic.fi/~tero/weather/doppleranimation.gif