PDA

View Full Version : Rotate your logos


Sparkz
05-06-2002, 04:36 PM
This template mod randomly picks an image from a specified folder to be used as the forum logo.

In your phpinclude-template, add this code:

$logos = array();
$logodir = "images/";

function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());

if ($handle = opendir($logodir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
array_push ($logos, $logodir . $file);
}
}
closedir($handle);
// $logo = rand(0, count ($logos));
$logo = rand(0, count ($logos) - 1); // Corrected a small bug
$titleimage = $logos[$logo];
}


Change the value of $logodir to the directory where you have your logos.

and in your header-template, change { titleimage} to $titleimage

That should be it, really.

tkieffer
05-06-2002, 08:43 PM
Thanks again Sparkz. It's installed and working great.

tkieffer
05-06-2002, 08:55 PM
Hmm. I've noticed that it occasionally pushes a null to the client (img src=""). I'm thinking this could either be because the random function goes beyond the bounds of the array or the directory read is actually picking up a blank. I'm afraid to just go fooling around with coding languages I haven't learned yet so I'm going to stay away from modifying your code.

Please help.

Crazy Mofo
05-07-2002, 02:26 AM
Nice but maybe need some work on it as when u refresh a few times a blank image wit the red cross appears ;)

Sparkz
05-07-2002, 08:40 AM
Hmm, sorry about that.
Change

$logo = rand(0, count ($logos));


to


$logo = rand(0, count ($logos) - 1);


Didn't check it properly before I posted, I guess :)

geniuscrew
05-07-2002, 12:52 PM
You can also use DHTML by putting the code in the head bit :-/

Sparkz
05-07-2002, 01:08 PM
There are probably tons of way to do this.
I just happen to know and like PHP, so that's why I chose to do it like this.

Sparkz
05-07-2002, 01:09 PM
Besides, this saves bandwidth, as it is server side.

Using DHTML, you'd have to pass the whole list if images to the client, plus the code to actually do the pick which image to show.

Crazy Mofo
05-07-2002, 04:39 PM
nice work sparkz very impressive :D

Crazy Mofo
05-07-2002, 04:42 PM
well works like a charm i gave this 10 outta 10 m8 well done :) nice simple and very effective :D

tkieffer
05-07-2002, 05:15 PM
Nifty! It's all fixed.

You get a big shiny golden star in my book!

geniuscrew
05-08-2002, 12:26 PM
Originally posted by Sparkz
Besides, this saves bandwidth, as it is server side.

Using DHTML, you'd have to pass the whole list if images to the client, plus the code to actually do the pick which image to show.

That's why i don't use it lol

Nice 'hack' though :bunny:

Sparkz
05-08-2002, 01:30 PM
If you only have a few images to rotate, you could just set the content of $logos manually. Would result in a bit less processing time.

Something like:

$logodir = "images/";
$logos = array
(
$logodir . "filename.png",
$logodir . "filename2.png"
);

function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());

$logo = rand(0, count ($logos) - 1);
$titleimage = $logos[$logo];


Would save the server from having to list the directory every time a page was loaded.

tkieffer
05-10-2002, 01:13 PM
Excellent! I upgraded to the predefined array and it is noticably faster.

afterlab
06-23-2002, 09:35 PM
Is there anyway that I can get it show the random image plus a link for each one. Basically, all I need is another variable that just displays the filename, without the directory, and extensions.

afterlab
06-28-2002, 12:13 PM
Anyone? [BUMP]

Sparkz
06-28-2002, 03:21 PM
Try this:

$logodir = "images/";
$logos = array
(
array ($logodir . "filename.png","http://mylink/"),
array ($logodir . "filename2.png","http://anotherlink/")
);

function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());

$logo = rand(0, count ($logos) - 1);
list ($titleimage, $titleurl) = $logos[$logo];


Now you can use $titleimage and $titleurl in your templates.

afterlab
06-28-2002, 09:52 PM
Worked perfect, thanks.

Sparkz
06-28-2002, 11:53 PM
My pleasure :D

Sparkz
06-28-2002, 11:56 PM
Just looked at your site... Like what you did with this mod :)

Dyntheos
07-07-2002, 02:08 PM
I have gotten this working succesfully on another skin and works beautifully.

I have tho another skin that it does not work on.

The code from the skin that doesn't work DOES work in any of the pre existing things so I know its not the array that is killing it here.

Is there anything that would interfere with this mod from working?

Sparkz
07-09-2002, 11:08 AM
Hmm - as long as you have the code in the header it _should_ work. I don't really use it myself, so I haven't tested it to much.

maestrosdelweb
12-04-2002, 01:52 AM
Great hack..

I'm using it under my website for christmas logos

http://www.forosdelweb.com/