-
AuthorPosts
-
Nathan L Zabaldo Friend
Nathan L Zabaldo
- Join date:
- September 2014
- Posts:
- 12
- Downloads:
- 0
- Uploads:
- 0
- Thanked:
- 2 times in 1 posts
May 19, 2007 at 8:58 pm #120349Tested on JA Kruger, but may be useful on other templates where a random header image is desired.
[PHP] <?php
$dir = “/images/headerimages/”; //You could change this to the folder store your header images.$abs_path = $mosConfig_absolute_path.$dir;
$live_path = $mosConfig_live_site.$dir;
$default_img = “sh1.jpg”;function getRandomImage($path, $img, $lp) {
if ( $list = getImagesList($path) ) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($list);
$img = $list[$num];
}
return $lp . $img;
}function getImagesList($path) {
$ctr = 0;
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
if ( preg_match(“/(.gif|.jpg)$/”, $img_file) ) { // add more conditions here if you want
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
}
return false;
}
?>
<div id=”ja-sh” class=”clearfix” style=”background:url(<?php echo getRandomImage($abs_path,$default_img,$live_path); ?>) no-repeat top right;”>
[/PHP]1 user says Thank You to Nathan L Zabaldo for this useful post
March 20, 2008 at 5:32 am #242653Really helpful code. It works like a dream 😀
I used it in a ja_antares template, with a little modification in order to display a higher picture in frontpage :[PHP]
<?php
# Image HEADER aléatoire ##########$dir = “/templates/ja_antares/images/header/”; //You could change this to the folder store your header images.
$a51_height= ‘200px’; // Image height for all pages except frontpage
$default_img = “a.jpg”; // Changer le nom de l’image par défaut si besoinif($option==”com_frontpage”) // variables if frontpage
{
$dir = “/templates/ja_antares/images/header/accueil/”;
$a51_height= ‘278px’; // Image height for frontpage
$default_img = “grand-header01.jpg”;
}$abs_path = $mosConfig_absolute_path.$dir;
$live_path = $mosConfig_live_site.$dir;function getRandomImage($path, $img, $lp) {
if ( $list = getImagesList($path) ) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($list);
$img = $list[$num];
}
return $lp . $img;
}function getImagesList($path) {
$ctr = 0;
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
if ( preg_match(“/(.gif|.jpg)$/”, $img_file) ) { // add more conditions here if you want
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
}
return false;
}?>
<div id=”ja-banner” style=”height: <?php echo $a51_height; ?>; background:url(<?php echo getRandomImage($abs_path,$default_img,$live_path); ?>) no-repeat top left;”>
[/PHP] -
AuthorPosts
This topic contains 2 replies, has 2 voices, and was last updated by pmarty 16 years, 9 months ago.
We moved to new unified forum. Please post all new support queries in our New Forum