lqdmindz Posted June 24, 2010 Share Posted June 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
thepi Posted June 24, 2010 Share Posted June 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 24, 2010 Author Share Posted June 24, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 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); ?> Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 24, 2010 Author Share Posted June 24, 2010 Wow thats awesome, could you explain how it works. Im guessing the functions.php assigns the names to the txt file the how does the time function work? Thanks sooo much for your time. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 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); ?> Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 24, 2010 Author Share Posted June 24, 2010 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? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 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'); } Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 24, 2010 Author Share Posted June 24, 2010 i understand about the calling banner.php instead of logo.jpg, it was the same with the previous text. I uploaded a rotation.txt and now i will have to wait haha. Thanks alot again. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 No worries, you can just update the text file manually to a time before the current server time if you want it to rotate. Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 24, 2010 Author Share Posted June 24, 2010 Its spot on www.h1vem1nd.org if you would like to see what its actually for Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 24-06-2010 00:04:54,./site_logo19.jpg I have already checked it out, nice design btw, you do that yourself? Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 24, 2010 Author Share Posted June 24, 2010 I didnt design the originally template...but i have made quite a few changes. I am handy with photoshop though...but this template had everything i wanted. Some of my old photoshop work lives at http://lqd.deviantart.com/gallery Quote Link to comment Share on other sites More sharing options...
thepi Posted June 24, 2010 Share Posted June 24, 2010 Thank Andy-H Nice banners lqdmindz! Many fit just great ! Quote Link to comment Share on other sites More sharing options...
lqdmindz Posted June 25, 2010 Author Share Posted June 25, 2010 Thanks for the compliment pi. Andy-H did an awesome job for me...and literally streamlined the code that much that the 2 combined files are less than the original, constant rotating, banner code i had. 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.