We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12695
    • 13 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:
    I wannt to show how to simple select random pictures for each page id, for example main logo will change each time when you open other page smiley
      • 14644
      • 6 Posts
      This is exactly what I need.

      Problem: instead of the correct image to appear only the code shows up. On the page with id 61 this text shows up: style="background:url(/jontte/assets/images/headers/pic_61.jpg)"

      Here’s the snippet code:

      <?php
      /*imgById*/
      /*
      	Parameters: &picId  - set current picture for selected page (undependent from page id)
      					 &folder - pictures folder destinations.    like ('assets/img/pic')
      					 &label   - pictures prefix. If picture like pic_8.jpg, prefix will be 'pic_'
      */
      
      $page_id = isset($picId)?$picId:$modx->documentObject['id'];
      $folder = (isset($folder))?$folder:'assets/images/headers';
      $label = isset($label)?$label:'pic_';
      $imgDir = opendir($folder);
      $current_img = $label.$page_id;
      $i = 0;
      	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);
        		
      		$file_conntent=substr($file, 0, strpos($file, "."));
      		$is_img_file = eregi($current_img, $file_conntent);
      		if ($file != '.' && $file != '..' && $is_image && $is_img_file){  
      			$good_img = $file;
      			$good_img = 'style="background:url([(base_url)]'.$folder.'/'.$good_img.')"';
      			closedir ($imgDir);
      			return $good_img;
      		}elseif($file != '.' && $file != '..' && $is_image){
      			$images[$i++] = $file;
      		}
      	}
      		
         closedir ($imgDir);
        return 'style="background:url([(base_url)]'.$folder.'/'.$images[rand(1, count($images))-1].')"';
      ?>
        • 14644
        • 6 Posts
        Can anyone help? :’(

        I need the feature to show a different image on each page.
          • 7455
          • 2,204 Posts
          I made one that is still in development that does random image and also mooslideshow and refection images.

          http://modxcms.com/forums/index.php/topic,21962.0.html
            follow me on twitter: @dimmy01
            • 14644
            • 6 Posts
            Quote from: Dimmy at Mar 11, 2008, 03:03 PM

            I made one that is still in development that does random image and also mooslideshow and refection images.

            http://modxcms.com/forums/index.php/topic,21962.0.html

            Very nice! laugh I will use it on some of my sites.
            But this one site needs a defined header image on each page. Anyone?
              • 4385
              • 372 Posts
              Here is how I am doing random headers. I have a container for the "header" documents. It is a simple template with one TV for the photo. I use the content are for a small caption. Then I have a snippet that use those documents as the source for my headers in my main pages. When I need a new header, I just create a new document in the Banner directory and add the photo to the TV, and it goes into the rotation. Another advantage is that you can schedule those documents for publishing as well. I hacked a snippet from sotwell and made it work. I had tried different things until I git it to work and never changed it. It could use some cleaning up...


              In my main template:

              [!ShowRandom? &startDoc=`8` &numItems=`1`!]


              My "Banner" chunk:
              		<div id="value-statement">
              			<div id="value-padding">
              				<div id="centered">
              					<p>[+bio+]</p>
              				</div>
              			</div>
              		</div>
              		<div id="photo-top">
              			[+bannerphoto+]
              		</div>
              


              My modified snippet (I made changes to suit my needs, some information is hardcoded):

              <?php
              // snippet ShowRandom
              // display content from random documents
              // sottwell 01-2006
              // arguments:
              //    &startDoc - ID of folder to select items from
              //    &numItems - number of random items to display
              //    &tpl = chunk name for item display template
              
              $startDoc = isset($startDoc) ? $startDoc : $modx->documentIdentifier;
              $numItems = isset($numItems) ? $numItems : 1;
              $tpl = isset($tpl) ? $modx->getChunk($tpl) : '
                  <div class="randItem">
                    [+content+]
                  </div>
              ';
              $resource = $modx->getActiveChildren($startDoc, 'createdon', 'DESC', $fields='id, pagetitle, description, introtext, content, createdon, createdby, hidemenu');
              $limit = count($resource);
              if(!function_exists(randArray)) {
                function randArray($size, $min, $max) {
                   if ($size > $max)
                       $size = $max;
                   $v = array();
                   while ( count($v) < $size ) {
                       do {
                           $n = rand( $min, $max );
                       } while ( array_search($n, $v) !== false);
                           $v[] = $n;
                   }
                   return $v;
                }
              }
              $list = randArray($numItems,0,$limit-1);
              $items = '';
              foreach($list as $item) {
                  $items .= str_replace('[+bio+]',$resource[$item]['content'],$tpl);
                  $tvoutput = $modx->getTemplateVarOutput(array('photo'), $resource[$item]['id']);
              }
              $chunkArr = array(
              'bio' => $resource[$item]['content'],
              'bannerphoto' => $tvoutput['photo']
              );
              
              $output .= $modx->parseChunk('Banner', $chunkArr, '[+', '+]');
              return $output;
              // return $items;
              ?>
                DropboxUploader -- Upload files to a Dropbox account.
                DIG -- Dynamic Image Generator
                gus -- Google URL Shortener
                makeQR -- Uses google chart api to make QR codes.
                MODxTweeter -- Update your twitter status on publish.
                • 14644
                • 6 Posts
                Anyone using this snippet successfully? I reaaaally need a defined header image on each page! Are there other solutions to achieve it?
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  So, make a TV with an input type of "image", then edit it for each document. Put the TV tags where you want the image to be. If it’s a background image, make a small <style type="text/css> #header {background: url([*tvname*]);} </style> block in the head of your template.

                  The TV will output the full relative path to the given image: assets/images/imagename.jpg or whatever. The only thing is, with the image input type the image needs to be somewhere under the assets/images folder in your filesystem.
                    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
                    • 4385
                    • 372 Posts
                    @Jon

                    What is it that you need? What do you mean by defined images? To me that sounds like the opposite of random.
                      DropboxUploader -- Upload files to a Dropbox account.
                      DIG -- Dynamic Image Generator
                      gus -- Google URL Shortener
                      makeQR -- Uses google chart api to make QR codes.
                      MODxTweeter -- Update your twitter status on publish.
                      • 14644
                      • 6 Posts
                      sottwell: Thanks, i’ll try that!

                      bwente: Yes, I need opposite of random. This snippet called RandomImage says it can show an image for each page id, but I can’t get it to work.

                      Edit: Yey, using a chunk works like a charm! Thanks sottwell!