We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19499
    • 30 Posts
    This is an auto-generated support/comment thread for RandomImage.

    Use this forum to post any comments about this addition or any questions you have regarding its use.

    Brief Description:
    RandomImage enables you to display a random image from a selected folder.
      • 33149
      • 28 Posts
      OK, I might be losing my mind, BUT I can save the entire snippet except for ONE line. I keep getting
      "Service Temporarily Unavailable
      The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later. "

      I copied and pasted the code line by line, partially and in full. I’ve tried it MANY different ways and as long as I remove the following line of code, it will save. If this line is left in, I get the error.

      $file_type = strrchr( $file, "." );


      Anyone else have this problem? Can I edit a snippet directly through my FTP program?

      Here is the entire code I am using for randomImage updated by Scotty D.

      $folder = (isset($folder))? $folder : 'assets/images/random';
      $i = 0;
      $imgDir = opendir ($folder);
      	while ( $file = readdir( $imgDir ) )
      	{	
      		//checks that file is an image
      		$file_type = strrchr( $file, "." );
      		$is_image = eregi( "jpg|jpeg|JPEG|JPG|gif|GIF|png|PNG",$file_type );
      		
      		if ( $file != '.' && $file != '..' && $is_image )
      		{ $images[$i++] = $file; }
      	}
      closedir ($imgDir);
      
      srand( (double) microtime()*1000000 );
      $image_name =  $images[rand( 0,sizeof( $images ) -1 )];
      $imageid = substr($image_name,0,strpos($image_name,'.'));
      $image_name = $folder . '/' . $image_name ;
      //ends script if no images found
      if ( $i == 0 )
      	die();
      
      echo "<a href=index.php?id=$imageid>";
      echo( "<div class=\"randomAd\"><img src=\"" . $image_name . "\" alt =\"" . $image_id . "\" /><br />\n" );
      @include("$folder/$imageid.txt") ; 
      echo "</div></a>";
      return ;
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Sounds like a badly configured mod_security on the server. You can load the snippet code directly in your database via your database management utility (usually phpmyadmin). Browse the site_snippets table first to see how a snippet is stored.

        Or, save the snippet as a .php file (make sure the file has the opening and closing <?php and ?> tags) and in the snippet itself in the Manager just put
         include "assets/snippets/randomimage/Randomimage.php";
        return '';
        
          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
          • 33149
          • 28 Posts
          Thanks! I’ll try saving it as a .php file and see if that works.

          Most of that was over my head. grin But I’m starting to catch on. I’m LOVING MODx so far. I have actually used some of your templates to see how things work.

          Would a badly confirgured mod_security be something I should send to my hosting support contact to take a look at? I’ll have to google it. It sounds serious.
            • 7923
            • 4,213 Posts
            @Ylice,

            Read this from MODx wiki: What is mod security and how does it affect me


              "He can have a lollipop any time he wants to. That's what it means to be a programmer."
              • 33149
              • 28 Posts
              Thanks doze.

              I host with Dreamhost and they told me that with their "enhanced security" option turned on -

              A few specific web applications do not work properly with this option enabled, but most sites will work normally (but with extra security!). At this time, it appears that phpstats, some PHP-based BitTorrent trackers, and possibly some binary image upload scripts may not work.


              So, I think I’ll leave it on and try some of the suggestions in the Wiki.

              Thanks for the reply!

              ~~~~~~~~~~~~~~

              I’m back. I’m messing this up, so am considering looking for a different snippet for this.

              I tried Susan’s suggestion and it places TWO random images on the page. One above my top menu and one in the place I want it. But, it DID work! It’s just that I need to figure out how to get it to 1 random image in the right spot.

              Then, I tried the Wiki suggestions. I’m sure it’s just something in the coding that I’m messing up, so no laughing if it equates to walking into a room and not knowing how to turn the light on!

              So, I do the first update to my .htaccess file - only the one at the root, not the one in the "manager" directory too.

              I add -
              SecFilterEngine On
              SecFilterSelective "REQUEST_URI" "/manager/index.php" "allow,nolog"


              Save and try to add the full code in the snippet. Same error.

              So, I leave the above and add -
              SecFilterEngine On
              SecFilterSelective "POST_PAYLOAD" "(( $file, "." );)" "allow,nolog"


              Didn’t work.

              I delete the First code and leave the POST PAYLOAD code.

              Didn’t work.

              I tried it with no ( ) and with 1 set of ( ). Nothing.

              I get the following error with the POST PAYLOAD code added. -
              Internal Server Error
              The server encountered an internal error or misconfiguration and was unable to complete your request.
              Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

              More information about this error may be available in the server error log.


              Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


              I’ve googled, but haven’t found the answer yet.

              Thanks for the advice though.
                • 14412
                • 74 Posts
                Hi there,

                I wasn’t getting this to work in MODx 0.9.5 so I resorted to a good old php file to rotate images and adapted it into a snippet.

                Ylice, I’m hosting with Dreamhost too and this solved the problem. Here’s the code:

                <?php
                // Random Image - snippet for MODx
                // Display a random image from a selected folder.
                // License GPL
                // Portions of code from AUTOMATIC IMAGE ROTATOR
                //	Version 2.2 - December 4, 2003
                //	Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd.
                //	All Rights Reserved.
                
                // Parameters:
                // &folder - folder where images are located. defaults to assets/images
                // &imgId - Id to add to the image.
                // &imgCls - classes to add to the image. separate multiple classe with a space
                // &altText - alternative text for the image
                
                //set parameters
                $folder = isset($folder)? $folder : 'assets/images';
                $imageId = isset($imageId)? $imageId : '';
                $imageCls = isset($imageCls)? $imageCls : '';
                $altText = isset($altText)? $altText : '';
                
                //set allowed extensions
                $extList = array();
                $extList['gif'] = 'image/gif';
                $extList['jpg'] = 'image/jpeg';
                $extList['jpeg'] = 'image/jpeg';
                $extList['png'] = 'image/png';
                
                //read folder and scan for images
                if (substr($folder,-1) != '/') {
                	$folder = $folder.'/';
                }
                $fileList = array();
                $handle = opendir($folder);
                while ( false !== ( $file = readdir($handle) ) ) {
                	$file_info = pathinfo($file);
                	if (
                			isset( $extList[ strtolower( $file_info['extension'] ) ] )
                	) {
                		$fileList[] = $file;
                	}
                }
                closedir($handle);
                
                //choose image to display at random
                $img = null;
                if (count($fileList) > 0) {
                	$imageNumber = time() % count($fileList);
                	$img = $folder.$fileList[$imageNumber];
                }
                
                //generate <img> tag
                if ($imageId != ''){ $imageId = ' id="'.$imageId.'"'; }
                if ($imageCls != ''){ $imageCls = ' class="'.$imageCls.'"'; }
                if ($altText != ''){ $altText = ' alt="'.$altText.'"'; }
                
                $output= '<img'.$imageId.$imageCls.' src="'.$img.'"'.$altText.' />';
                
                return $output;
                ?>
                


                To make it work, put the code inside a snippet and call it like this:
                [tt][[RandomImage? &folder=`assets/templates/TESE/banners` &imageId=`banner` &altText=`Random Image`]][/tt]

                I’m no coder, just a LEGO maker smiley I’m sure there are plenty of more efficient ways to do this - feel free to add.

                Cheers, JP
                  Got Music? Try XSPFjukebox - Skinnable MODx Media Player
                  • 33149
                  • 28 Posts
                  Thanks JP.

                  I’ll give it a try as soon as my headache goes away. grin

                  Before I read your post, I thought I had found a pretty clean script to use, but I can’t get it to work.

                  The script is here - http://photomatt.net/scripts/randomimage

                  I’ll let you know if I can get yours to work! I wouldn’t hold my breath.
                    • 19083
                    • 15 Posts
                    I installed joeindio’s snippet last night. Works like a charm.
                      • 16879
                      • 87 Posts
                      Anyone got any bright ideas for incorporating relevant and meaningful alt attributes for each image?