Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Nathan L Zabaldo Friend
    #120349

    Tested 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]

    pmarty Friend
    #242653

    Really 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 besoin

    if($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]

Viewing 2 posts - 1 through 2 (of 2 total)

This topic contains 2 replies, has 2 voices, and was last updated by  pmarty 16 years, 8 months ago.

We moved to new unified forum. Please post all new support queries in our New Forum