We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    Take a hint from the filedownload snippet, and have a chunk (or a file, for that matter) with the text for each image that gets parsed when the image is loaded. Of course, that means that the chunk or file needs to be updated when images are uploaded.
      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
      • 16879
      • 87 Posts
      Yes, I was thinking about something possibly like that or even adapting the code available at http://alistapart.com/articles/betterrotator into a snippet possibly.
        • 12110
        • 122 Posts
        Hello,

        I just enhanced the snippet a bit with generating the alt attribute by a page variable and defining the folder to retrieve images from not in the snippet, but in the snippet call:

        <?php
        $i = 0;
        $imgDir = opendir ($folder);
        	while ( $file = readdir( $imgDir ) )
        	{	
        		//checks that file is an image
        		$file_type = strrchr( $file, "." );
        		$is_image = eregi( "jpg|gif",$file_type );
        		
        		if ( $file != '.' && $file != '..' && $is_image && substr($file,1,5)!='thumb')
        		{ $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( "<img src=\"" . $image_name . "\"" . $imgSize[3] . " alt=\"[*pagetitle*]\" />\n" );
        ?>


        Please note the alt-Attribute generated by the page title (may be something else of course, even a variable that you pass to the snippet as a parameter in the snippet call like the folder parameter/variable). I removed the first line "$folder = ’directory to folder here’;" as when calling the snippet, I wrote
        [!randomImage? &folder=`assets/images/random`!]

        That’s working for me, in .9.6.1p2
          • 29076
          • 615 Posts
          Is there a way to modify the code to show images from a folder within a folder?

          Let say I have images in
          assets/images/random/ and in
          assets/images/random/folder1/ as well as in
          assets/images/random/folder2/

          and I want to show random images from everywhere in assets/images/random/.

          How can I do that? (As it is now I get a blank screen when there’s now images in assets/images/random/ and only in assets/images/random/folder1 or assets/images/random/folder2).

          Thanks. smiley
            I think, thererfor I am! But what I am, and why...?
            • 7923
            • 4,213 Posts
            Yes, there is a way to do that... smiley

            Look at the comments in PHP’s readdir function manual page for good examples.


              "He can have a lollipop any time he wants to. That's what it means to be a programmer."
              • 20853
              • 14 Posts
              I also hit the fail to save the snippet problem.
              I’m hosted with 34SP; looks like they’ve got their security cranked way up too.
              The problem is the presence of ’strrchr’ in the posted data.

              I had a flash of inspiration and I’ve got a solution.
              Simply replace:
              $file_type = strrchr( $file, "." );

              With:
              $file_type = substr( $file, -3 );


              Works a treat grin grin grin
                • 17705
                • 501 Posts
                Mmmm, I am trying to use this snippet, but just show nothing...

                I have tried the joeindio modificated snippet too
                and the Nacnud changed line
                without luck...

                I have also tried to use full server path, to the random directory... no luck...

                any idea?
                  • 17705
                  • 501 Posts
                  mmm, I just made a php file, using that code... and echoing the outp... and it works!

                  but why it is not working in modx?? sad

                  in the template I have called it using:
                  [[!RandomImage? &folder=`randomredondo` &border=`0`!]]

                  this code works ok as a regular .php file
                  <?php
                  #::::::::::::::::::::::::::::::::::::::::
                  # Snippet Name: RandomImage
                  # Short Desc: Displays a random image from a folder
                  # Version: 1.0a
                  # Created by: Mike Fairbrother
                  # Adapted by: Wim Beerens ([email protected])
                  #
                  # Date: January 3, 2008
                  #
                  # Changelog: 
                  # January 3th, 2008 -- added &folder parameter
                  #                   -- added &border parameter
                  #                   -- added <div id="rndImage"> for css formatting
                  #
                  #::::::::::::::::::::::::::::::::::::::::
                  
                  # Snippet customize settings
                  $folder	= isset($folder)? $folder:'randomredondo';
                  $border	= isset($border)? $border:'0';
                  
                  $i = 0;
                  $imgDir = opendir ($folder);
                  	while ( $file = readdir( $imgDir ) )
                  	{	
                  		//checks that file is an image
                  		$file_type = strrchr( $file, "." );
                  		$is_image = eregi( "jpg|gif",$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( '<div id="rndImage"><img src="' . $image_name . '" ' . $imgSize[3] . ' alt="" border="' . $border . '" /></div>' );
                  
                  echo '<div id="rndImage"><img src="' . $image_name . '" ' . $imgSize[3] . ' alt="" border="' . $border . '" /></div>';
                  ?>


                  any idea??
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Is the [[!...!]] part a typo, or your actual snippet tags? It should be either [[...]] or [!...!] (unless this is on Revo, of course)
                      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
                      • 17705
                      • 501 Posts
                      Quote from: sottwell at Apr 23, 2010, 03:53 PM

                      Is the [[!...!]] part a typo, or your actual snippet tags? It should be either [[...]] or [!...!] (unless this is on Revo, of course)

                      susan, you are my new god!