filiptc Posted May 20, 2007 Share Posted May 20, 2007 Hi everyone, this is the first 'real thing' I'm trying to code with PHP for my site. I want to grab a weather radar image from http://www.example.com/radar_latest.gif which is a static gif image and gets refreshed every 30min (aprox.) and make a (5-frame, 1 second each, looping) animated gif with the 5 latest ones. The tricky thing (for my php level) is to make the new gif every time the latest 5 images (into a temp folder?) and delete the oldest (the sixth), as well as making the gif (using GD?). The finished animated gif should always be saved with the same name (overwritten each time) in the same folder, so that it can be shown on the site. Thanks in advance Phil Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted May 20, 2007 Share Posted May 20, 2007 ok... theres a php extension........ i think its called ming..... and its a php / flash based extension. if enyone has xampp, or wamp, or something like that, then its one of the examples...... i dont know how to use it, but it might be of some help............ you can google "xampp" download it a=for about 24 mb i think, its apache php, pearl mysql and firezilla etc, but its there, even if you only install temporarily, it might give you an idea on how its done........ the only thing is i dont think its php certified or whatever yet, which mens you might find getting a tutorial a little dificult good luck Quote Link to comment Share on other sites More sharing options...
zq29 Posted May 20, 2007 Share Posted May 20, 2007 As far as I'm aware, GD doesn't support the creation or modification of animated GIFs, although the ImageMagick tool 'mogrify' does - There is an excellent introduction to basic animation with ImageMagick - HERE. As for grabbing the image, if you have PHP configured correctly, I think you might be able to use the copy() function. If not, you might have to use a combination of fopen() fclose() and fwrite() Quote Link to comment Share on other sites More sharing options...
filiptc Posted May 20, 2007 Author Share Posted May 20, 2007 @PC Nerd: I'm using linux so no XAMPP (but separate packages of apache/mysql/php/perl instead). I should have the same oprions though. I'll check out the 'ming' thing. @SemiApocalyptic: I'll also check out the ImageMagic tool. As for the grabbing, I initially thought of fopen() fclose() and fwrite() anyways, I'll look into copy() too though. Otherwise the other option would be not to create an animated gif but to use javascript to rotate between the last fetched images. BTW, the actual image I'm trying to fetch is http://www.meteosat.com/imagenes/radar/sp/ultima_madrid.gif just in case anyone was wondering about the example.com broken link The twisted part (besides the technical issue of creating the animated gif) is the keeping the latest 5 images and deleting the old ones (rotating the names of the fetched images: image1.gif, image2.gif ... image5.gif, and deleting the outdated image5.gif or something like that). Any coding help is (very much) appreciated. Phil Quote Link to comment Share on other sites More sharing options...
zq29 Posted May 20, 2007 Share Posted May 20, 2007 Basic idea... <?php function grab_latest() { unlink("images/0.gif"); for($i=0; $i<5; $i++) rename("images/".$i+1.".gif","images/$i.gif"); copy("http://somewhere.com/image.gif","images/4.gif"); } ?> Quote Link to comment Share on other sites More sharing options...
filiptc Posted May 20, 2007 Author Share Posted May 20, 2007 Ok! Got it working... here's what I did... I used your code even though I had to change some stuff because of php errors. Thanks to both. <?php $radartime = filectime("radar/5.gif") + 1800 - time(); if($radartime < 0) { unlink("radar/1.gif"); rename('radar/2.gif' , 'radar/1.gif'); rename('radar/3.gif' , 'radar/2.gif'); rename('radar/4.gif' , 'radar/3.gif'); rename('radar/5.gif' , 'radar/4.gif'); copy("http://www.meteosat.com/imagenes/radar/sp/ultima_madrid.gif","radar/5.gif"); shell_exec("convert -delay 100 -size 479x529 -page +0+0 /var/www/html/radar/1.gif -page +0+0 /var/www/html/radar/2.gif -page +0+0 /var/www/html/radar/3.gif -page +0+0 /var/www/html/radar/4.gif -page +0+0 /var/www/html/radar/5.gif -loop 0 /var/www/html/radar/radar.gif"); } ?> Quote Link to comment Share on other sites More sharing options...
filiptc Posted May 28, 2007 Author Share Posted May 28, 2007 Here is a cleaner, reworked, more usable code. Thanks again everyone for making my first php thingy work! <?php /* ########################### #### Define Variables ##### ########################### */ // # of frames $seq = 13; // Size of frames ([width]x[height] in pixels) $size = "479x529"; // delay on the first frame (1/100 of sec) $delay_first_frame = 100; // speed of each other frame (1/100 of sec) $speed = 10; // delay on the last frame (1/100 of sec) $delay_last_frame = 200; // absolute system path of where the frames are saved $absolute_path_frames = "/var/www/html/radar/temp/"; // absolute system path of where the animation is saved $absolute_path_animation = "/var/www/html/radar/"; // relative path to this php document of where the frames are saved $relative_path_frames = "radar/temp/"; // URL where to fetch the latest frame $source = "http://www.meteosat.com/imagenes/radar/sp/ultima_madrid.gif"; /* ########################### ######## Begin Code ####### ########################### */ // If the last one does not exist (for example after raising the # of frames by 1) we create it if (file_exists($relative_path_frames.$seq.".gif") == FALSE) { for ($i = $seq; $i >= 2; $i--) { rename( $relative_path_frames . ($i-1) . ".gif" , $relative_path_frames . $i . ".gif"); } } copy($relative_path_frames . "2.gif", $relative_path_frames . "1.gif"); // Reorder the sequence for ($i = 1; $i <= ($seq-1); $i++) { rename( $relative_path_frames . ($i+1) . ".gif" , $relative_path_frames . $i . ".gif"); } // Fetch last frame copy($source,$relative_path_frames . $seq . ".gif"); // Create frame sequence sintax (frames in between the first and the last) for ($i = 2; $i <= ($seq-1); $i++) { $intermediate_frames = $intermediate_frames . $absolute_path_frames . $i . ".gif "; } // Execute ImageMagick (shell) to create the secuence exec("convert -delay " . $delay_first_frame . " -size " . $size . " " . $absolute_path_frames . "1.gif -delay " . $speed . " " . $intermediate_frames . "-delay " . $delay_last_frame . " " . $absolute_path_frames . $seq . ".gif -loop 0 " . $absolute_path_animation . "radar.gif"); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.