Advertisement
lberelson

showrandomimg plugin v5

Aug 8th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.48 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: ShowRandomImg
  4. Plugin URI: http://howlingwolfmedia.com/site1
  5. Description: This function scans thru the directory received and loads the image files names into an array; Then returns a random name to the caller
  6. Version: 1.0
  7. Author: Flavio de Pecol
  8. Author URI: http://howlingwolfmedia.com
  9. License: GPL2
  10. */
  11.        
  12.         function  GetImageName($DirName)
  13.                         {
  14.                          $file = 'debugtrace.txt';
  15.                          $Img = array();  // define array
  16.          
  17.                          // image types accepted:  jpg png gif
  18.                          $ImgPath = $DirName . '/' . '{*.jpg,*.gif,*.png}';
  19.                          //$ImgPath = $DirName . '/' . '*.jpg';
  20.          
  21.          
  22.                          file_put_contents($file, $ImgPath); // Write trace
  23.                          
  24.                          $Img = glob($ImgPath, GLOB_BRACE);
  25.                          //$Img = glob($ImgPath);
  26.          
  27.                         /* foreach (glob($ImgPath, GLOB_BRACE) as $filename)
  28.                          {
  29.                           if (is_readable($filename))
  30.                                         $Img[]  = $filename;  // found image file
  31.                          }
  32.                          */
  33.  
  34.  
  35.                          file_put_contents($file, "\nlist files:\n", FILE_APPEND ); // Write trace
  36.  
  37.                          //foreach (glob($ImgPath) as $filename)
  38.                          //{
  39.                          // $Img[]  = $filename;  // found image file
  40.                          // file_put_contents($file, "\n".$filename, FILE_APPEND ); // Write trace
  41.                          //}
  42.          
  43.                          file_put_contents($file, "\nFull array = " . $Img, FILE_APPEND ); // Write trace
  44.          
  45.                          $Imgcount = sizeof($Img) - 1; // the index is zero-based
  46.                          file_put_contents($file, "\nArray size = ".$Imgcount, FILE_APPEND ); // Write trace
  47.  
  48.                          if ($Imgcount <= 0)
  49.                                 return "wp-content/themes/custom/img/rotatepics/pic1.jpg";
  50.                          else
  51.                          {
  52.                           $rand_image_ix = rand(0,$Imgcount);
  53.                           return $Img[$rand_image_ix];  // return name of image
  54.                          }
  55.         }
  56.          
  57.          
  58.                         // function to print the random image
  59.          
  60.         function ShowRandomImg($size_x=300, $size_y=228)
  61.                         {
  62.                          $DirNm = "wp-content/themes/custom/img/rotatepics";  // current directory
  63.          
  64.                          if (is_front_page() || is_page('contact') )
  65.                          {
  66.                           $DirNm = $DirNm  . "/contact";
  67.                           $ImgNm = GetImageName($DirNm);  // retrieve random image from directory "contact"
  68.                          }
  69.                          elseif (is_page('references') )
  70.                          {
  71.                           $DirNm = $DirNm  . "/references";
  72.                           $ImgNm = GetImageName($DirNm);  // retrieve random image from directory "references"
  73.                          }
  74.                          elseif (is_page('services') )
  75.                          {
  76.                           $DirNm = $DirNm  . "/services";
  77.                           $ImgNm = GetImageName($DirNm);  // retrieve random image from directory "services"
  78.                          }
  79.                          else
  80.                          {
  81.                           $ImgNm = GetImageName($DirNm);  // retrieve random image from current directory
  82.                          }
  83.          
  84.          
  85.                          $file = 'debugtrace.txt';
  86.                          file_put_contents($file, $DirNm, FILE_APPEND ); // Write trace
  87.                          file_put_contents($file, $ImgNm, FILE_APPEND ); // Write trace
  88.          
  89.                          $ImgTag =  '<img src="/' . $ImgNm . '" width="' . $size_x . '" height="' . $size_y . '"  alt="' . $ImgNm . '">';
  90.                          file_put_contents($file, $ImgTag, FILE_APPEND ); // Write trace
  91.          
  92.                          echo $ImgTag;
  93.                          
  94.                         $content = ob_get_contents();
  95.                         ob_end_clean();
  96.                         return $content;
  97.                         }
  98.                        
  99.                         add_shortcode("ShowRandomImg", "ShowRandomImg");
  100.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement