making a radar loop with PHP

So a few days ago I was out with friends and a storm was on it’s way in. Naturally, I’m the one they ask whether it’s ‘about to hit us’, so I pull up the web browser on my Treo 650 and realize… um… there’s no quick way to pull up a radar loop. I mean, I used the great mobile site from the NWS, but it still didn’t give me the effect I wanted. And hey, I’m already filling a database with weather observations and radar images! So I decided to see what it would take to make a custom radar loop that I could get to at any given moment. The Treo can display animated gif files, so that seemed like the most efficient solution. The question is, how do we create an animated gif using PHP?

Well, I don’t like to reinvent the wheel, especially if someone else out there has made a wheel superior to one that I could make myself. :) Hence, I found this. From here, we just need to read in the images and feed them to the GifMerge class.

[php]
Header(‘Content-type: image/gif’);
require(‘GifMerge.class.php’);

$count = 1;
$q = mysql_query(‘select * from (select recorded, radar from weather order by recorded desc limit 10) as t order by recorded’);
while($r = mysql_fetch_array($q)){
$image_p = imagecreate(300, 300);
$image = imagecreatefromgif(“radar/”.substr($r[‘radar’], 35));
imagecopyresampled($image_p, $image, 0, 0, 210, 120, 300, 300, 300, 300);
imagegif($image_p, “radar/loop/frame”.$count.”.gif”);
$count++;
}

$__i = array(“radar/loop/frame1.gif”, “radar/loop/frame2.gif”, “radar/loop/frame3.gif”, “radar/loop/frame4.gif”, “radar/loop/frame5.gif”, “radar/loop/frame6.gif”, “radar/loop/frame7.gif”, “radar/loop/frame8.gif”, “radar/loop/frame9.gif”, “radar/loop/frame10.gif”);
$__d = array(30, 30, 30, 30, 30, 30, 30, 30, 30, 300);
$gifmerge = new GifMerge($__i, 1, 1, 1, -10, $__d, 2, 0, 0, ‘C_FILE’);
echo $gifmerge -> getAnimation();
[/php]

One issue I had was that the images I scrape are rather large, and when you merge 10 of them into an animated GIF, you end up with an image that a little bigger than most handheld devices like to deal with. Not to mention, we’re talking about a GPRS connection here… it would be easier to download 50kb than 500kb. So to get around that, I used the imagecopyresampled function to cut out the portion of the image most important to me. I then save the cropped image into a ‘loop’ directory as 10 separate ‘frame’ files. After that, it’s pretty straightforward to follow the GifMerge example to create the image.

So without further ado, here’s the result:

10 Responses to “making a radar loop with PHP”

  1. LiquidBrain Says:

    Hello, i like your code, is there any way to see clouds above europe?

    Very interesting ;) Keep going.

  2. pr Says:

    LiquinBrain: Only if you want to write it. :) These instructions are a little open-ended so that you can form it to fit your needs. Give it a try!

  3. Natcher Says:

    Did something similar when Cyclone Larry was about to hit the coast near here – good work!

  4. pr Says:

    Natcher: that’s awesome! Good job to you as well!

    In case others don’t see they can click on your name… go here guys: http://www.natchling.net/bom_satellite.gif

  5. Dozey Says:

    I’m shocked to find someone else that has also done this.

    I have a version online (currently set up for wap viewing, I suggest you check it out in Opera if your normal browser doesn’t work or otherwise with a new(er) mobile phone)

    Small for phone: http://www.epimper.com/sbs2/compat.php
    Large: http://www.epimper.com/sbs2/large.php
    Another: http://www.epimper.com/sbs2/index.php

    ** Be patient / gentle, the script does not cache anything, it may take a little time to download the latest loop images

    The source of my radar loop images were GIF and I found resizing the entire image resulted in such a huge drop in quality that it wasn’t really worth it.

    As it turned out however, the WAP gateway or mobile phone must do some sort of resizing which manages to leave GD in the dust most of the time… so that was a nice work around for that problem.

    It was great fun.. until the realisation that I had wasted $40 credit admiring how great the invention was..

  6. mozey Says:

    Do canada!, do canada!!!!!!!!!

  7. me Says:

    excellent!good job

  8. The lsd Says:

    Hello

    I would like to traduct your post in french and paste it on a french website. May i have your authorization to do this ? (by the way, if you agree i’ll let a link to your site)

    Enjoy

    The lsd

  9. pr Says:

    lsd: Of course, I think that would rock! Feel free to translate it!

  10. The lsd Says:

    Ok thanks a lot

    Translation is done. If you want to see the french version, this is here : http://www.newbiecontest.org/forums/index.php?topic=1435.msg18549#msg18549

    Enjoy

    The lsd

Leave a Reply