We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29215
    • 4 Posts
    Hi there ModX team,

    This is the first post I have made. I really like ModX and I am using it to develop a website which will ’go live’ soon.
    I have been fiddling with CMS’s for many years and like a lot of people have downloaded most of the free ones. This is the best by far.

    If it is of any use to anyone I will share a few simple changes I made to an existing snippet to make it
    do what I wanted.

    I needed a random picture that was linked to a page on the site. We also wanted some text beneath the
    picture to make it look more like a ’profile’.

    1. Create a new snippet called RandomAd.

    $folder = 'assets/images/ads';
    $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 =  $images[rand( 0,sizeof( $images ) -1 )];
    $imageid = substr($image_name,0,strpos($image_name,'.'));
    $image_name = $folder . '/' . $image_name ;
    $imgSize = GetImageSize( $image_name );
    //ends script if no images found
    if ( $i == 0 )
    	die();
    
    echo "<a href=index.php?id=$imageid>";
    echo( "<center><img border=0 src=\"" . $image_name . "\"" . $imgSize[3] . " /><p>\n" );
    @include("$folder/$imageid.txt") ; 
    echo "</a></center>";
    return ;
    
    



    2. Create a subdirectory of images (I called it ads)
    3. Create the page that you want to link to and note the number that ModX assigns to it. (Eg 106)
    4. Upload a jpg or gif file of the correct size to images/ads and name it based on the number from step 3. (Eg 106.jpg)
    5. Upload a text file to the same directory and also name it after the number assigned in step 3 (106.txt)
    6. Call it as usual from where you want. I use the [!RandomAd!] methodology, but you could use [[RandomAd]]


    You can see it in action at
    www.thespidersart.com/new/1/

    in the area called Advertiser Profile. There are only three ’advertisers’ at the present time.


    I really appreciate the hard work and dedication of all the people associated with ModX

    Geoff

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Very nice! Thank you for sharing!

      And in return I will share a bit of advice.
      Coffee News is a
      free, easy to hold, weekly publication
      which gets delivered
      could better be written
      Coffee News is a
      free, easy to hold, weekly publication
      that gets delivered

      http://www.worldwidewords.org/articles/which.htm
        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
        • 23109
        • 25 Posts
        Hello,

        after calling it in my Template the Sites are replaced by this Message:

        getimagesize() [function.getimagesize]: Read error! ... document.parser.class.inc.php(691) : eval()’d code

        regards,
        Peter
          Grafik & Grossformatdruck: www.druck-schmie.de
          Bildergalerie: www.bilder-schmie.de
          • 29215
          • 4 Posts
          Thankyou Susan for your comments and also for picking up on the bad grammar.


          Hello,
          
          after calling it in my Template the Sites are replaced by this Message:
          
          getimagesize() [function.getimagesize]: Read error!    ...   document.parser.class.inc.php(691) : eval()'d code     
          
          regards,
          Peter 


          I looked at the code that I started with to see if there was something else I had downloaded, and then thought that for what I was doing that code was unnecessary, so changed it to the following

          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( "<center><img border=0 src=\"" . $image_name . "\" /><p>\n" );
          @include("$folder/$imageid.txt") ; 
          echo "</a></center>";
          return ;
          
          

          Hope this helps

          Geoff
            • 4273
            • 356 Posts
            how to use .PNG images ?

            also in your example you show a text link below the image is that part of the snippet ? how to call the text with the image

            will it work with weblinks as the doc link too ?
              SMF Bookmark Mod - check it out
              http://mods.simplemachines.org/index.php?mod=350
              • 26435
              • 1,193 Posts
              I updated this to also include the file extensions jpg, jpeg, JPEG, JPG, gif, GIF, png & PNG.
              added snippet variable "folder". defaults to "assets/images/random".
              call as "[!RandomAd? &folder=`path/to/random/images`!]
              Also cleaned up the output to:
              <a href=index.php?id=677>
              <div class="randomAd">
              <img src="path/to/677.png" alt="677" />

              </div>
              </a>

              here is the code:
              $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 ;
              


              thank you for this snippet, I like it a lot.

              -sD-
              scotty Delicious
                Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
                All of the above... in no specific order.


                I send pointless little messages
                • 6726
                • 7,075 Posts
                My, my I did miss some nice things when I was away on vacation, luckily I stumbled on this thread : thanks for sharing this one grin
                  .: COO - Commerce Guys - Community Driven Innovation :.


                  MODx est l&#39;outil id
                  • 7923
                  • 4,213 Posts
                  scotty, great work! would you be kind enough to add that to the resource repository? wink


                    "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                    • 26435
                    • 1,193 Posts
                    Quote from: doze at Aug 18, 2006, 03:24 PM

                    scotty, great work! would you be kind enough to add that to the resource repository? wink

                    thanks.
                    This code looks awfully familiar. I want to check some resources first before I post it to make sure credit is given to the original author(s). Also, I might take a crack at modifying it to use chunks or better yet TV’s instead of a static folder full of numbered images. Yeah... that would be fierce!

                    -sD-
                      Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
                      All of the above... in no specific order.


                      I send pointless little messages
                      • 11429
                      • 90 Posts
                      Is there a way to put a title=" " attribute in the random ad ?