We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32982
    • 674 Posts
    added png
    the snippet is
    $folder = ’images’;

    $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 )];

    //ends script if no images found
    if ( $i == 0 )
    die();

    return "background=’$image_name’" ;
    usage
    put the call to the snippet in the table, div or background into the <>
    example
    <td width="30%" height="100%" valign="top" [[RandomImage]] bgcolor="#F4F4F4">content</td>
    put [! !] if you want to update on browser reload (dont tested i dont want it)
    change the folder to the direction of your image folder
    [based on etomite random image snippet]
      Jabiertxof (formerly XYZVISUAL)
      My bussines: http://marker.es
      https://www.youtube.com/user/jabiertxof/videos
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Quote from: xyzvisual at Oct 02, 2005, 05:08 AM

      $is_image = eregi( "jpg|gif",$file_type );

      Might want to add png to that.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 33337
        • 3,975 Posts
        Zaigham (aka zi) Reply #3, 21 years ago
        Great snippet !

        Thanks for sharing.

        Best regards,

        zi
          Zaigham R - MODX Professional | Skype | Email | Twitter

          Digging the interwebs for #MODX gems and bringing it to you. modx.link
          • 7455
          • 2,204 Posts
          Is it posible to add a chek to see if the image is not a thumb? fck editor generates thubs in the same folder.
          somthines i like to set the image my self using fckeditor but then the random image snippet also display’s those .thumb_ files.
          my PHP knowledge is not so good that i can do that, but i think it ist not to hard to do somthing like if .thumb_* then do not put in array but how to translate my sessemistreet language into php.

          hope that somone can help me.

          Greets Dimmy
            follow me on twitter: @dimmy01
            • 10487 MODX Staff
            • 1,535 Posts
            Hi Dimmy,

            The easiest way I can think to exclude the thumb images is to check the filename against a regexp for ’.thumb*’. If you can change this line of code:

            if ( $file != '.' && $file != '..' && $is_image )


            for:

            if ( !preg_match('/\.thumb*/',$file) && $file != '.' && $file != '..' && $is_image )


            that should work.

            I’ve tested it quickly and seems to do the job but you’ll have to let me know if you have any problems.

            Cheers, Garry
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You can kill three birds with one regexp if you just exclude all files that begin with a .
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 7455
                • 2,204 Posts
                Thanks a lot this works great
                  follow me on twitter: @dimmy01
                  • 5139
                  • 1 Posts
                  Hi all!

                  How can I set the image size? thanks!