We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6902
    • 126 Posts
    I wrote this for a couple of my sites, and just thought I’d share. I’m using it to generate a random image url on page load for certain elements. It’s pretty straightforward.

    <?php
    
    //******************************************
    // Created by Darren Doyle, Dec. 2010
    //******************************************
    
    // ----- PURPOSE -----
    // Designed to pull a random image (or file)
    // from a sepcified folder. Parent and path are
    // mostly there to help minimize typing on
    // the snippet calls.
    
    // ----- VARIABLES -----
    // $parent = the path to forced parent folder.
    // The path should be relative to the base url.
    // Include a trailing slash.
       $parent = 'assets/images/';
    
    // $types = the types of file extensions to accept
       $types = array('jpg', 'gif', 'png');
    
    // ----- PROPERTY -----
    // Create a property named "path," and set
    // it to the default path within the parent folder.
    // This can be overridden in the snippet call.
    // Do not include a trailing slash.
    
    //******************************************
    
    
    // get full path/url
    $myDir = $parent . $path . '/'; 
    $p = $modx->config['base_path'].$myDir;
    $u = $modx->config['base_url'].$myDir;
    
    // get a list of each image file in the target folder
    $dir = opendir($p);
    while($f = readdir($dir)) {
    	$ext = pathinfo($f, PATHINFO_EXTENSION);
    	if ( in_array($ext, $types) ) $contents[] = $f;
    }
    closedir($dir);
    
    // if empty, return nothing
    if (empty($contents)) return '';
    
    // else return random file
    shuffle($contents);
    return ($u.$contents[0]);
    
      • 24383
      • 12 Posts
      Hi debussy,

      Thank you for the snippet. I will give it a try.
      Just briefly looked to it, and I noticed that your are not closing your php-script at the end (?>)
      Is this intentional? or a little copy/paste fault.

      Regards,
        • 6902
        • 126 Posts
        Just briefly looked to it, and I noticed that your are not closing your php-script at the end (?>)
        Is this intentional? or a little copy/paste fault.

        Actually, MODx strips out the <?php and ?> tags in a snippet. They aren’t necessary--a snippet has to be php.
        I put the <?php at the top so the forum parser would color the code correctly.
          • 6902
          • 126 Posts
          If you named the snippet "randomImage" then the call would look like:

          [[!randomImage? &path=`my/path/in/parent/folder`]]

          I suggest adding a "path" variable to the snippet’s default property set. If you do that, then specifying the path in the snippet call is only necessary if you want to override the default.

            • 6902
            • 126 Posts
            That’s correct! grin
              • 36427
              • 2 Posts
              This is cool.
              Does anybody know how to edit the snippet, to exclude the (automatically created) thumbnails?

              For example, if i add with fck editor an image to a folder:
              mypic.jpg

              my folder will also contain a file called:
              .thumb_mypic.jpg,
              and I would like the snippet to exclude the thumbnails.

              I guess it shouldn't be too difficult, but i can't find a way to do it. Thanks!
                • 33968
                • 863 Posts
                Try this - insert just after closedir($dir):
                // strip out thumbnails
                function strip_thumbs($item) {
                    return !strpos($item,'thumb_');
                }
                $contents = array_filter($contents, 'strip_thumbs');
                
                // if empty, return nothing
                if (empty($contents)) return '';
                
                // else return random file
                shuffle($contents);
                return ($u.$contents[0]);
                
                  • 36427
                  • 2 Posts
                  Thanks Lucas -- it works perfectly
                    • 13135
                    • 30 Posts
                    This is really useful....but would it be possible to have it return all the image urls of the specified folder, and then output them into a tpl parameter? Then you could use a chunk template with one [+url+] placeholder for use with other plugins?
                      • 44736
                      • 1 Posts
                      There's a great new site I saw that lets you look at all the random images you want!!!! It's called http://www.randumimage.com Randum Image and it's super fun.

                      You can click the arrows or use the keyboard to go through an endless list of images, check it out!

                      Scrape images from there all you want!