We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7260
    • 107 Posts
    Hello I was trying to get the randombackgroundimage snippet to work.
    I would like a different header image displayed (or the appearance of at least) as someone navigates the site.
    Now, I was going to just add a script but then I saw there was a snippet. However I cannot get the snippet to work right.

    Instead of showing the image, it shows the path to the image. It does appear to at least call a different path as I navigate. But obviously thats not what I want it to do. The output is html. I dont have any tables so Im not sure how to implement it.

    Anyone have any success with this snippet or similar?

    TY
      • 7923
      • 4,213 Posts
      The snippet returns background=’path/to/image.jpg’ width="69" height="69" to use in table cells, but change it to the desired output in the last line of the snippet code, so for example:

      Change it to:
      return 'background:url($image_name)';
      

      So you could use it in the style attribute of your div.. <div style="[[RandomBackgroundImage]]"></div>

      Or change it to:
      return 'src='.$image_name.' '.$imgSize[3];
      

      So you can use it in a <img> tag.. <img [[RandomBackgroundImage]] />

      Or change it just to:
      return $image_name;
      

      So that it returns just the path and you can use it anywhere you want..


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 7260
        • 107 Posts
        Thanks DOze!
        Your solution worked perfectly!
        I did the first method.

        Only thing is (you were probably using shorthand) it took me awhile to realize I should put

        return "background:url($image_name)";

        With double quotes...After I did that, worx like a charm. Thanks!

        What a handy snippet...
          • 7923
          • 4,213 Posts
          Oh yea, sorry, that supposed to be return ’background:url(’.$image_name.’)’; but double quotes works just as well.. glad that I could help.


            "He can have a lollipop any time he wants to. That's what it means to be a programmer."
            • 9110
            • 4 Posts
            nice snippet - thanks

            Snippet changed a bit:
            1. folder:
            2. return slightly different

            $folder = 'assets/images/subpages';
            $i = 0;
            $imgDir = opendir ($folder);
               while ( $file = readdir( $imgDir ) )
               {   
                  //checks that file is an image
                  $file_type = strrchr( $file, "." );
                  $is_image = eregi( "jpg|gif|png",$file_type );
                  
                  if ( $file != '.' && $file != '..' && $is_image )
                  { $images[$i++] = $file; }
               }
            closedir ($imgDir);
            
            srand( (double) microtime()*1000000 );
            $image_name = $folder. '/' . $images[rand( 0,sizeof( $images ) -1 )];
            $imgSize = GetImageSize( $image_name );
            
            //ends script if no images found
            if ( $i == 0 )
               die();
            
            return "background-image:url($image_name)";


            Wrote this in HTML-doc:
            <DIV id=SubpageContent style='[[RandomBackgroundImage]]'>


            Rest is in CSS-file - so dont think the CSS validates 100%

            Works nice