Randomise my Forum's Logo?

Started by SergeantAsh, May 23, 2006, 03:01:59 PM

Previous topic - Next topic

SergeantAsh

This is possibly a very simple one but I want to randomise my forums logo! I've hardcoded the images path into the code, so the TD cell now look like this...

<td class="AshHeaderBanner" height="85" align="left"><a href="vauxhall_calibra_clubcalibra.html"><img src="http://www.clubcalibra.com/graphics/logos/clubcalibra1.gif" alt="', $context['forum_name'], '" border="0" /></a></td>

What would I add to this code to perhaps randomise between 3 images, clubcalibra1.gif, clubcalibra2.gif, clubcalibra3.gif?

I'm an ASP person, not PHP so I haven't really a clue!

Many thanks!

Cheers, Ash
Quote"Moderation has been called a virtue to limit the ambition of great men, and to console undistinguished people for their want of fortune and their lack of merit."

SergeantAsh

For anyone else looking to do this, I've found a useful little script...

Paste the following into a file called 'rotate.php' or similar...

<?php

   $folder = '.';

      $extList = array();
      $extList['gif'] = 'image/gif';
      $extList['jpg'] = 'image/jpeg';


// --------------------- END CONFIGURATION -----------------------

$img = null;

if (substr($folder,-1) != '/') {
   $folder = $folder.'/';
}

if (isset($_GET['img'])) {
   $imageInfo = pathinfo($_GET['img']);
   if (
       isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
      $img = $folder.$imageInfo['basename'];
   }
} else {
   $fileList = array();
   $handle = opendir($folder);
   while ( false !== ( $file = readdir($handle) ) ) {
      $file_info = pathinfo($file);
      if (
          isset( $extList[ strtolower( $file_info['extension'] ) ] )
      ) {
         $fileList[] = $file;
      }
   }
   closedir($handle);

   if (count($fileList) > 0) {
      $imageNumber = time() % count($fileList);
      $img = $folder.$fileList[$imageNumber];
   }
}

if ($img!=null) {
   $imageInfo = pathinfo($img);
   $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
   header ($contentType);
   readfile($img);
} else {
   if ( function_exists('imagecreate') ) {
      header ("Content-type: image/png");
      $im = @imagecreate (100, 100)
          or die ("Cannot initialize new GD image stream");
      $background_color = imagecolorallocate ($im, 255, 255, 255);
      $text_color = imagecolorallocate ($im, 0,0,0);
      imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
      imagepng ($im);
      imagedestroy($im);
   }
}

?>

Then, in your image tag, do this...
<img src="rotate.php">

Should work fine! Put the rotate.php into the SAME directory as your random images.
Quote"Moderation has been called a virtue to limit the ambition of great men, and to console undistinguished people for their want of fortune and their lack of merit."

kegobeer

If you just want to pick a random image, just do this:


<td class="AshHeaderBanner" height="85" align="left"><a href="vauxhall_calibra_clubcalibra.html"><img src="http://www.clubcalibra.com/graphics/logos/clubcalibra', rand(1,3), '.gif" alt="', $context['forum_name'], '" border="0" /></a></td>


Make three images named clubcalibra1.gif, clubcalibra2.gif, and clubcalibra3.gif.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

SergeantAsh

Thanks for that, much simpler bit of code but I'll stick with my script now as I can simply upload files to the directory and they're randomised without having to change the code.
Quote"Moderation has been called a virtue to limit the ambition of great men, and to console undistinguished people for their want of fortune and their lack of merit."

AllBucDUp


carlatf

Hi,
Thanks for the code, I really like the ability to not have to enter the image URL.

In case anyone is looking for a changeable set but in order here's the code.

/portada is the directory of the images


  <script type="text/javascript" language="javascript">
var headerimageURLs = new Array(

\'/portada/select10.jpg\' ,
\'/portada/select11.jpg\' ,
\'/portada/select12.jpg\' ,
\'/portada/select13.jpg\' ,
\'/portada/select14.jpg\' ,
\'/portada/select15.jpg\' ,
\'/portada/select16.jpg\' ,
\'/portada/select17.jpg\' ,

);

var whichimg;
if (window.name.indexOf(\'headerimage_\') == -1) whichimg = 0;
else {
whichimg = Number(window.name.split(\'_\')[1]);
whichimg = (whichimg == headerimageURLs.length - 1) ? 0 : ++whichimg;
}
window.name = String(\'headerimage_\' + whichimg);
document.write(\' <a  href="', $context['forum_url'], '" title="discussion forum"><img border="0"  alt=" ', $context['forum_name'], '" border="0" align="center" src="\' + headerimageURLs[whichimg] + \'" /></a>\');

</script>

SleePy

I use 2 different types of ranodmizing with a option in forum profile to change it.
Heres the first one I do:

<?php 
////////////////////////////////////////////////////////////////////// 
/*                  Matt Sims' PHP Sig Rotator V1.3                 */ 
/* This script takes an inventory of all image files in a directory */ 
/* and displays one at random. All you need to do is save this code */ 
/* in a directory that contains your images (name it what you will, */ 
/* but make sure it has a ".php" extension). Then just link to this */ 
/* script to get your random image.                                 */ 
/* I would recommend naming this file "index.php," and then you can */ 
/* just link to the directory itself (like I do in my example).     */ 
/* [email protected]      www.evilmerc.com             */ 
////////////////////////////////////////////////////////////////////// 

if ($dir opendir(".")) 
    
$list buildimagearray($dir); displayrandomimage($list); 

// This function reads all the files in the current directory and adds all image files to the array $list[] 
function buildimagearray($dir

     while (
false !== ($file readdir($dir))) 
     { 
          if (!
is_dir($file) && getimagesize($file)) 
               
$list[] = $file
     } 
     return 
$list


// This function selects a random image, determines the mime type, opens the file for reading, and then outputs the image 
function displayrandomimage($list

     
srand ((double) microtime() * 10000000); $sig array_rand ($list); $size getimagesize ($list[$sig]); $fp fopen($list[$sig], "rb"); 
     if (
$size && $fp
          
header("Content-type: {$size['mime']}"); fpassthru($fp); exit; 

?>

and for that you put for the image url:
http://yoursite.com/path/index.php

the next one requires you to edit your index.template.php and put it inplace of were your logo would be. remember to get out of any echos. This is like kegobeer's script. But I made it so it auto updated the images for me.
It just does a while command and finds out if a file exists or not and counts.
I added some comments to explain the code a bit.

$h = 1; // have to set this to 1 other else it wont work correctly.
$st = 0; // Set this so an error isn't reported so that the while statement doesn't report the error x ammount of times.
while($st == 0){
if(!file_exists('/relative/path/to/banners/banner'.$h.'.jpg'))
$st = 1; // If there is not file we tell the while loop to stop.
else
$h++;  // The file exists lets increase our number by 1 and continue on.
}
$h = $h -1; // It over counts because h is set to 1. but that is ok
$randomnumber = rand(1, $h);  // Randomly grab a number and prepare it for echo
echo '<img src="http://www.yoursite.com/banners/banner'.$randomnumber.'.jpg" height="200" width="800" />';
Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

Advertisement: