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:
September 10th, 2006 at 9:33 am
Hello, i like your code, is there any way to see clouds above europe?
Very interesting ;) Keep going.
September 11th, 2006 at 12:51 pm
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!
September 21st, 2006 at 12:32 am
Did something similar when Cyclone Larry was about to hit the coast near here – good work!
September 21st, 2006 at 9:02 am
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
November 18th, 2006 at 10:20 am
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..
March 31st, 2008 at 4:27 pm
Do canada!, do canada!!!!!!!!!
August 26th, 2008 at 7:41 am
excellent!good job
March 13th, 2009 at 5:01 am
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
March 13th, 2009 at 8:43 pm
lsd: Of course, I think that would rock! Feel free to translate it!
March 14th, 2009 at 2:48 am
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
October 11th, 2016 at 5:30 pm
I really enjoy the blog article.Really looking forward to read more. Fantastic. fgeekbeggeeeecek
March 16th, 2017 at 5:14 am
Hey, I think your website might be having browser compatibility issues.
When I look at your blog site in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, excellent blog!
June 3rd, 2017 at 10:45 pm
Nice response in return of this query with solid arguments and explaining all regarding
that.