PHP Countdown with Image Rotation?

Soldato
Joined
14 Apr 2003
Posts
5,716
Location
Leicester
Hi all,

I am trying to get a countdown script working, which will then throw a different image up every time.

Here is the countdown script; haven't changed much in it, apart from the reference file for the image;

PHP:
<?

$month = 7; // Month of the countdown
$day = 1; // Day of the countdown
$year = 2004; // Year of the countdown

// mktime is the marked time,  and time() is the current time.
$target = mktime(0, 0, 0, $month, $day, $year);
$diff = $target - time();

$days = ($diff - ($diff % 86400)) / 86400;
$diff = $diff - ($days * 86400);
$hours = ($diff - ($diff % 3600)) / 3600;
$diff = $diff - ($hours * 3600);
$minutes = ($diff - ($diff % 60)) / 60;
$diff = $diff - ($minutes * 60);
$seconds = ($diff - ($diff % 1)) / 1;

$imgname = "countdown1.png";
$im = @imagecreatefrompng ($imgname);

//Here are some common color codes in case you want to change the colors.
//$white = imagecolorallocate ($im,  255,  255,  255);
//$blue = imagecolorallocate ($im,  0,  0,  255);
//$black = imagecolorallocate ($im, 0, 0, 0);
//$gray = imagecolorallocate ($im, 153, 153, 153);
//$red = imagecolorallocate ($im, 255, 0, 0);
//$orange = imagecolorallocate ($im,  255,  127,  36);

$background_color = imagecolorallocate ($im,  0,  0,  0);
$orange = imagecolorallocate ($im,  255,  127,  36);
$yellow = imagecolorallocate ($im,  247,  246,  201);
imagestring ($im,  2,  60,  5,   " Countdown to Half Life 2 Release:     Available from www.hl2empire.com",  $yellow);
imagestring ($im,  3,  65,  18,   "[ $days day(s) ] [ $hours hour(s) ] [ $minutes minute(s) ] [ $seconds second(s) ]",  $orange);
imagepng ($im);
imagedestroy ($im);
?>

Here is the script for countdown1.png (.htaccess rewrite); have also tried referencing as .php which doesn't make a difference

PHP:
<?php
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
readfile($files[array_rand($files)]);
?>

I was getting the following error messages in the log; now I am not getting anything!! :confused:

[21-Mar-2010 15:24:59] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/metalmay/public_html/sigs/countdown/test/countdown.php:22) in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 24
[21-Mar-2010 15:25:02] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/metalmay/public_html/sigs/countdown/test/countdown.php:22) in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 24
[21-Mar-2010 15:25:37] PHP Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 44
[21-Mar-2010 15:25:37] PHP Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 45
[21-Mar-2010 15:25:37] PHP Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 46
[21-Mar-2010 15:25:37] PHP Warning: imagestring(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 47
[21-Mar-2010 15:25:37] PHP Warning: imagestring(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 48
[21-Mar-2010 15:25:37] PHP Warning: imagepng(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 49
[21-Mar-2010 15:25:37] PHP Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/metalmay/public_html/sigs/countdown/test/countdown.php on line 50

File:
countdown.php


Any ideas? Really bugging me now!! :(

Edit: if I use the original script (which doesn't reference to the .php/.png image, it works...
 
Last edited:
countdown.php:
PHP:
<?
$month = 7; // Month of the countdown
$day = 1; // Day of the countdown
$year = 2004; // Year of the countdown

// mktime is the marked time,  and time() is the current time.
$target = mktime(0, 0, 0, $month, $day, $year);
$diff = $target - time();

$days = ($diff - ($diff % 86400)) / 86400;
$diff = $diff - ($days * 86400);
$hours = ($diff - ($diff % 3600)) / 3600;
$diff = $diff - ($hours * 3600);
$minutes = ($diff - ($diff % 60)) / 60;
$diff = $diff - ($minutes * 60);
$seconds = ($diff - ($diff % 1)) / 1;

//$imgname = "countdown1.png";
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
$imgname = $files[array_rand($files)];
$im = @imagecreatefrompng ($imgname);

//Here are some common color codes in case you want to change the colors.
//$white = imagecolorallocate ($im,  255,  255,  255);
//$blue = imagecolorallocate ($im,  0,  0,  255);
//$black = imagecolorallocate ($im, 0, 0, 0);
//$gray = imagecolorallocate ($im, 153, 153, 153);
//$red = imagecolorallocate ($im, 255, 0, 0);
//$orange = imagecolorallocate ($im,  255,  127,  36);

$background_color = imagecolorallocate ($im,  0,  0,  0);
$orange = imagecolorallocate ($im,  255,  127,  36);
$yellow = imagecolorallocate ($im,  247,  246,  201);
imagestring ($im,  2,  60,  5,   " Countdown to Half Life 2 Release:     Available from www.hl2empire.com",  $yellow);
imagestring ($im,  3,  65,  18,   "[ $days day(s) ] [ $hours hour(s) ] [ $minutes minute(s) ] [ $seconds second(s) ]",  $orange);
imagepng ($im);
imagedestroy ($im);
?>

Then:
PHP:
<img src="countdown.php" alt="countdown" />

Should work.
 
If you're sticking it on a website with a fair amount of traffic you might want to cache the created images for a few seconds, otherwise every request will cause the script to generate a new image :).
 
Going to sound like a noob here (well I guess I am when it comes to PHP!!) Any ideas how I can do that?
 
PHP:
<?
$cache_file = "cache/image.png";

// Cached file exists?
if (file_exists($cache_file)) {
	// Keep generated imges for 30 seconds
	if ((time() - filemtime($cache_file)) <= 30 ) {
		readfile($cache_file);
		exit();
	} else {
		unlink($cache_file);
	}
}

$month = 7; // Month of the countdown
$day = 1; // Day of the countdown
$year = 2004; // Year of the countdown

// mktime is the marked time,  and time() is the current time.
$target = mktime(0, 0, 0, $month, $day, $year);
$diff = $target - time();

$days = ($diff - ($diff % 86400)) / 86400;
$diff = $diff - ($days * 86400);
$hours = ($diff - ($diff % 3600)) / 3600;
$diff = $diff - ($hours * 3600);
$minutes = ($diff - ($diff % 60)) / 60;
$diff = $diff - ($minutes * 60);
$seconds = ($diff - ($diff % 1)) / 1;

//$imgname = "countdown1.png";
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
$imgname = $files[array_rand($files)];
$im = @imagecreatefrompng ($imgname);

//Here are some common color codes in case you want to change the colors.
//$white = imagecolorallocate ($im,  255,  255,  255);
//$blue = imagecolorallocate ($im,  0,  0,  255);
//$black = imagecolorallocate ($im, 0, 0, 0);
//$gray = imagecolorallocate ($im, 153, 153, 153);
//$red = imagecolorallocate ($im, 255, 0, 0);
//$orange = imagecolorallocate ($im,  255,  127,  36);

$background_color = imagecolorallocate ($im,  0,  0,  0);
$orange = imagecolorallocate ($im,  255,  127,  36);
$yellow = imagecolorallocate ($im,  247,  246,  201);
imagestring ($im,  2,  60,  5,   " Countdown to Half Life 2 Release:     Available from www.hl2empire.com",  $yellow);
imagestring ($im,  3,  65,  18,   "[ $days day(s) ] [ $hours hour(s) ] [ $minutes minute(s) ] [ $seconds second(s) ]",  $orange);
//imagepng ($im);
// Output to cache file
imagepng ($im, $cache_file);
imagedestroy ($im);

// Read cache file
readfile($cache_file);
?>

Probably full of holes :p, but it works :).

I added the 'cached file exists' check at the top of the code; if the create time of the last image was over 30 seconds ago a new image is generated, else the old one is used (tweak to whatever time you want).

I then changed imagepng ($im) to imagepng ($im, $cache_file) which stores the generated image as cache/image.png and reads it back in.

Should reduce server load quite a bit. You'll need to create a cache folder and give it the right permissions also.
 
Thanks :) - Really appreciated :D - Although it doesn't seem to be caching?

~Seems to be pulling the old file, which is random

index.php
 
Last edited:
Hmm worked for me. Try outputting:
echo time() - filemtime($cache_file);

and seeing what it says; it might not be picking up the file time properly or something.
 
Back
Top Bottom