Advertisement
lberelson

Site 1 - rotate pic function v1 edit_7.25.13

Jul 25th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. /*
  3.     * Functions file
  4.     * Includes all necesary files
  5.     *
  6.     * @package custom
  7.     */
  8.  
  9. ini_set('display_errors', 'On');
  10. error_reporting(E_ALL);
  11.  
  12. function register_my_menus() {
  13.      register_nav_menus(
  14.        array(
  15.          'primary-menu' = __( 'Primary Menu' ),
  16.          'secondary-menu' = __( 'Secondary Menu' )
  17.        )
  18.      );
  19. }
  20. add_action( 'init', 'register_my_menus' );
  21.  
  22. function custom_widgets_init() {
  23.     register_sidebar(
  24.         array(
  25.             'name' = 'Rotate Pics Sidebar',
  26.             'id' = 'rotate_pics',
  27.             'before_widget' = '<div',
  28.             'after_widget' = '</div',
  29.             'before_title' = '<h2 class="rounded"',
  30.             'after_title' = '</h2',
  31.         )
  32.     );
  33.  
  34.     register_sidebar(
  35.         array(
  36.             'name' = 'Articles Sidebar',
  37.             'id' = 'articles',
  38.             'before_widget' = '<div',
  39.             'after_widget' = '</div',
  40.             'before_title' = '<h2 class="rounded"',
  41.         'after_title' = '</h2',
  42.         )
  43.     );
  44. }
  45.  
  46. // this function scans thru the directory received and loads the image
  47. file // names into an array; Then returns a random name to the caller
  48. // If no directory is passed it will use the default dir
  49.  
  50. function  GetImageName($DirName)
  51.     {
  52.      $Img = array();  // define array
  53.  
  54.      // jpg
  55.      foreach (glob("$DirName/*.jpg") as $filename)
  56.      {
  57.       if (is_readable($filename))
  58.         $Img[]  = $filename;  // found image file
  59.      }
  60.  
  61.      // png
  62.      foreach (glob("$DirName/*.png") as $filename)
  63.      {
  64.       if (is_readable($filename))
  65.         $Img[]  = $filename;  // found image file
  66.      }
  67.  
  68.      // gif
  69.      foreach (glob("$DirName/*.gif") as $filename)
  70.      {
  71.       if (is_readable($filename))
  72.         $Img[]  = $filename;  // found image file
  73.      }
  74.  
  75.             $Imgcount = sizeof($Img) - 1; // the index is zero-based
  76.             if ($Imgcount <= 0)
  77.               return "/wp-content/themes/custom/img/rotatepics/pic1.jpg";
  78.             else
  79.             {
  80.              $rand_image_ix = rand(0,$Imgcount);
  81.              return $Img[$rand_image_ix];  // return name of image
  82.             }
  83. }
  84.  
  85.  
  86.     // function to print the random image
  87.  
  88. function ShowRandomImg($size_x=300, $size_y=228)
  89.     {
  90.      $DirNm = "/wp-content/themes/custom/img/rotatepics";  // current
  91. directory
  92.      if (is_front_page() || is_page('contact') )
  93.      {
  94.       $DirNm = $DirNm  . "/contact";
  95.       $ImgNm = GetImageName($DirNm);  // retrieve random image from
  96. directory "contact"
  97.      }
  98.      elseif (is_page('references')
  99.      {
  100.       $DirNm = $DirNm  . "/references";
  101.       $ImgNm = GetImageName($DirNm);  // retrieve random image from
  102. directory "references"
  103.      }
  104.      elseif (is_page('services')
  105.      {
  106.       $DirNm = $DirNm  . "/services";
  107.       $ImgNm = GetImageName($DirNm);  // retrieve random image from
  108. directory
  109.     "services"
  110.      }
  111.      else
  112.      {
  113.       $ImgNm = GetImageName($DirNm);  // retrieve random image from
  114. current directory
  115.      }
  116.  
  117.      // print image (or if preferred change to return
  118.          $ImgPath = $DirNm . "/" . $ImgNm;
  119.       echo "<img src=\"$ImgNm\" width=\"$size_x\" height=\"$size_y\"
  120.     alt=\"$ImgNm\"> ";
  121.  
  122.      // return $DirNm.$ImgNm;  // use this to simply return the image path
  123.  
  124. }
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement