Jump to content

Header Logo that changes randomly every hour


lqdmindz

Recommended Posts

Hi guys,

 

i have an issue with a code im using on a phpbb site.  This code rotates the headers perfectly fine, but, i dont want it to constantly change.  I would prefer the banner stay in play for an hour then change randomly and remain for another hour.  Below is the code i currently use.  The site links to this page to rotate the header.

 

<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:

http://photomatt.net/scripts/randomimage

*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

 

I have been thinking it over and cant really come up with a 1 page solution like the above code.  Im not sure if it would have to be time based or not.  Any help would be awesome.

 

Lqd

To see the logo change - http://www.h1vem1nd.org

Link to comment
Share on other sites

Hello,

(Sorry for my english...)

 

 

You could probably use a file or a db field with the last hour stocked !?

 

Example :

It's 10.50 right now, I visit your page I generate the first visit of this hour ... :

 

$rand = mt_rand(0, $i); // $i was incremented as we went along
//Assign the current HOUR to a variable let's say $curHour.
$curHour=date("H");
//Stock this and $rand into a file or db...

 

Next time somebody visit (to complete the code above) :

//Recover the last generated value from file or db ...
$curHour = valueFromFileOrDb(); ;
$rand = valueFromFileOrDb();
if($curHour==date("H")){
header('Location: '.$folder.$files[$rand]); // Voila!
}else{
//Code above, new random  Voila.
}

 

 

I hope this two step example inspired you...

Thepi

Link to comment
Share on other sites

That was great...i kinda guessed it would have to be something to do with a database.  If i might ask another question.  Is it possible for me to specify a time period to display an image.  So if it was between 10.00 and 10.59 it would display image 1.  If it were 11.00 to 11.59 it would display image 2 else display default ?

Link to comment
Share on other sites

Got bored so I wrote this for you...

 

functions.php

<?php
function updateBanner()
{
   $folder = './';
   $file   = 'rotation.txt';
   $exts   = array('jpg', 'jpeg', 'png', 'gif');
   $exts   = '{*.' . implode(',*.', $exts) . '}';

   //pull all images into an array using glob
   $images = glob($folder . $exts, GLOB_BRACE);
   shuffle($images);
   
   if (!$fp = fopen($folder . $file, "w"))
   {
      return false;
   }
   else
   {
      $nextRotate = date('d-m-Y H:i:s', strtotime('+1 hour'));
      fwrite($fp, $nextRotate . ',' . $images[0]);
      fclose($fp);
   }
}

function getImageInfo()
{
   $folder = './';
   $file = 'rotation.txt';
   
   if (!$fh = @fopen($folder . $file, "r"))
   {
      updateBanner();
   }
   else
   {
      $info = fread($fh, filesize($folder . $file));
      fclose($fh);
   }
   
   return explode(',', $info);
}
?>

 

banner.php

 

<?php

include "functions.php";

$date   = date('d-m-Y H:i:s');
$info   = getImageInfo();

   if ( strtotime($date) >= strtotime($info[0]) )
   {
      updateBanner();
   }

      $img = $info[1];
      $ext = explode('.', $img);
      $i   = count($ext) - 1;
      $ext = ($ext[$i] == 'jpg') ? 'jpeg' : $ext[$i];
      
      $imageCreate = 'imagecreatefrom' . $ext;
      $img = $imageCreate($img);
      
      header('Content-Type: image/' . $ext);
      $imageShow = 'image' . $ext;
      $imageShow($img);
      imagedestroy($img);

?>

 

:)

Link to comment
Share on other sites

functions.php, this contains two functions, one to update the banner with a new random banner if the current server time is greater than the stored time in the txt file, it glob's through the specified directory looking for JPG, GIF or PNG files, glob returns an array of all these image files, the array is then shuffle'd an the 1st array element is selected as the current banner, it stores the current server time + 1 hour in a file, followed by a comma and the filename of the banner.

 

The other function just reads the data from the text file, returning the data as an array, the first element will be the next rotation time, and the 2nd will be the filename of the banner.

<?php
function updateBanner()
{
   $folder = './';
   $file   = 'rotation.txt';
   $exts   = array('jpg', 'jpeg', 'png', 'gif');
   $exts   = '{*.' . implode(',*.', $exts) . '}';

   //pull all images into an array using glob
   $images = glob($folder . $exts, GLOB_BRACE);
   shuffle($images);
   
   if (!$fp = fopen($folder . $file, "w"))
   {
      return false;
   }
   else
   {
      $nextRotate = date('d-m-Y H:i:s', strtotime('+1 hour'));
      fwrite($fp, $nextRotate . ',' . $images[0]);
      fclose($fp);
   }
}

function getImageInfo()
{
   $folder = './';
   $file = 'rotation.txt';
   
   if (!$fh = @fopen($folder . $file, "r"))
   {
      updateBanner();
   }
   else
   {
      $info = fread($fh, filesize($folder . $file));
      fclose($fh);
   }
   
   return explode(',', $info);
}
?>

 

banner.php, this includes the functions file, and gets the current server date and time, pulls the data from the text file (using the getImageInfo() function), then uses an if statement, comparing the current server time, to the stored rotation time (after converting them to timestamps using the strtotime function), if the rotation time has passed, it calls the updateBanner() function to update the data in the text file.

 

Then the filename stored in the text file is inserted into the $img variable, and we get the extension (note: if the extension is jpg, it is converted to 'jpeg' to use some PHP functions) we then append the file extension to the string 'imagecreatefrom', to call as a variable function (see: imagecreatefromjpeg, imagecreatefrompng, etc..) then we set the content-type header to image/jpeg|png|gif depending on the extension, before displaying the image using imagejpeg, imagepng or imagegif, once again depending on the image extension. Then the image is destroyed using imagedestroy to free up the resources.

<?php

include "functions.php";

$date   = date('d-m-Y H:i:s');
$info   = getImageInfo();

   if ( strtotime($date) >= strtotime($info[0]) )
   {
      updateBanner();
   }

      $img = $info[1];
      $ext = explode('.', $img);
      $i   = count($ext) - 1;
      $ext = ($ext[$i] == 'jpg') ? 'jpeg' : $ext[$i];
      
      $imageCreate = 'imagecreatefrom' . $ext;
      $img = $imageCreate($img);
      
      header('Content-Type: image/' . $ext);
      $imageShow = 'image' . $ext;
      $imageShow($img);
      imagedestroy($img);

?>

 

Link to comment
Share on other sites

Thanks alot for your time.  Ive implemented the files on my site already and awaiting the the change.  A quick question.  I have looked on the server and there doesnt appear to be a rotations.txt.  Must i create this?

Link to comment
Share on other sites

You can do if you like, but if not, once you call banner.php twice, the first call to it will display a few errors and create the file, and the second time it will run fine. BTW, to display the image you need to call the banner.php file as the img src of your HTML img tag.

 

e.g.

 

<img src="images/banner.php" >

 

or using css

 

#banner
{
   background-image: url('path/to/banner.php');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.