PDA

View Full Version : [3.7.X]Random Logo Changer


TSHNOFX
03-04-2009, 10:02 PM
I'm wondering if anyone could make a modification that works similarly to this script:

<?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 = 'img/';

// 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!
?>

Basically, it'd load whatever images from a given directory and display them randomly. Thanks in advance if anyone is interested.

Mr-Moo
03-04-2009, 11:47 PM
You would have to add something like this to and have it included into your header:
<?php
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}

function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}

$root = '';
// If images not in sub directory of current directory specify root
//$root = $_SERVER['DOCUMENT_ROOT'];

$path = 'forum/images/stylename/newlogos';

// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);

$img = getRandomFromArray($imgList);

?>

Then wherever your header image is place this:
<img src="<?php echo $path . $img ?>" alt="" />

Or at least assign the above snippet code as a Vb template code and display this in your style.

I think I explained it correctly, if anyone else can elaborate please do as my thoughts sometime scatter :p

Hope this helps. Let me know if it does!

BSMedia
03-05-2009, 12:20 AM
You would have to add something like this to and have it included into your header:
<?php
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}

function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}

$root = '';
// If images not in sub directory of current directory specify root
//$root = $_SERVER['DOCUMENT_ROOT'];

$path = 'forum/images/stylename/newlogos';

// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);

$img = getRandomFromArray($imgList);

?>

Then wherever your header image is place this:
<img src="<?php echo $path . $img ?>" alt="" />

Or at least assign the above snippet code as a Vb template code and display this in your style.

I think I explained it correctly, if anyone else can elaborate please do as my thoughts sometime scatter :p

Hope this helps. Let me know if it does!

Other than the fact you can't put php into the templates you nailed it right on :erm:

You'll have to make a plugin at global_start more than likely and give whatever the script outputs a value and then use it in your template

private_ale
03-05-2009, 12:50 AM
What are you guys talking about? Did you even read the script?

First: Make a separate directory for your logos. Lets call it logos/. Now lets say that the logos/ directory is located in your root directory. If that is the case then it would be logos/.

Look at the portion of code below, see where I put logos/, this is where the path to your logo directory goes. (As described above)
*/// 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 = 'logos/';WOW! That's all you need to edit from that script.
Next, create a new file and name it something, lets call it rotate.php. Take the newly modified script and save it into that file. Place that file (rotate.php) into you logos/ directory.

Lets check,
So far you've added the path to your logo directory to the script CHECK
You've saved the script as a php file. In this example we called it "rotate.php" CHECK
You placed your new .php file into that SAME directory as your logos CHECK

Last Step:
Where you want your logo to display, simply put a regular image tag, but instead of putting the source as an image, put it as your .php file.
<img src="path/to/logos/rotate.php" border="0" alt="" />That's it.

Mr-Moo
03-05-2009, 01:02 PM
private_ale, I am completely lost, that code you made makes no sense to me. I am far from a fluent PHP coder, but if you can, please explain this a tad for me.

You are placing the first bit of code into a "rotate.php" file. Which assigns variable "$folder" to the data "logos/". Which means all this is is a variable assigned to specific data.

Then you call a PHP script in the image source which is in no way related to $folder or that variable. I don't even see an echo.

In my eyes, there is no random rotation, no variables on the intervals, no directory to be called from, no variable image type definition, etc.... It would also behoove you to be a little less abrasive with your comments, as you may have offended some people and their intelligence.

---
BSMedia, you are correct. I am somewhat familiar with Vbulletin, but I am still learning how PHP is integrated with VB.

You are indeed correct, he will have to create a plugin calling these scripts and variables. Then he would be able to assign some coding to them and call within his styles/template.

Thank you everyone, I hope this information is useful TSHNOFX.

TSHNOFX
03-09-2009, 09:44 PM
I ended up removing the alt out of the logo call and just using the regular php script, and it seems to work fine, thanks.