Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: ShowRandomImg
- Plugin URI: http://howlingwolfmedia.com/site1
- 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
- Version: 1.0
- Author: Flavio de Pecol
- Author URI: http://howlingwolfmedia.com
- License: GPL2
- */
- function GetImageName($DirName)
- {
- $file = 'debugtrace.txt';
- $Img = array(); // define array
- // image types accepted: jpg png gif
- $ImgPath = $DirName . '/' . '{*.jpg,*.gif,*.png}';
- //$ImgPath = $DirName . '/' . '*.jpg';
- file_put_contents($file, $ImgPath); // Write trace
- $Img = glob($ImgPath, GLOB_BRACE);
- //$Img = glob($ImgPath);
- /* foreach (glob($ImgPath, GLOB_BRACE) as $filename)
- {
- if (is_readable($filename))
- $Img[] = $filename; // found image file
- }
- */
- file_put_contents($file, "\nlist files:\n", FILE_APPEND ); // Write trace
- //foreach (glob($ImgPath) as $filename)
- //{
- // $Img[] = $filename; // found image file
- // file_put_contents($file, "\n".$filename, FILE_APPEND ); // Write trace
- //}
- file_put_contents($file, "\nFull array = " . $Img, FILE_APPEND ); // Write trace
- $Imgcount = sizeof($Img) - 1; // the index is zero-based
- file_put_contents($file, "\nArray size = ".$Imgcount, FILE_APPEND ); // Write trace
- if ($Imgcount <= 0)
- return "wp-content/themes/custom/img/rotatepics/pic1.jpg";
- else
- {
- $rand_image_ix = rand(0,$Imgcount);
- return $Img[$rand_image_ix]; // return name of image
- }
- }
- // function to print the random image
- function ShowRandomImg($size_x=300, $size_y=228)
- {
- $DirNm = "wp-content/themes/custom/img/rotatepics"; // current directory
- if (is_front_page() || is_page('contact') )
- {
- $DirNm = $DirNm . "/contact";
- $ImgNm = GetImageName($DirNm); // retrieve random image from directory "contact"
- }
- elseif (is_page('references') )
- {
- $DirNm = $DirNm . "/references";
- $ImgNm = GetImageName($DirNm); // retrieve random image from directory "references"
- }
- elseif (is_page('services') )
- {
- $DirNm = $DirNm . "/services";
- $ImgNm = GetImageName($DirNm); // retrieve random image from directory "services"
- }
- else
- {
- $ImgNm = GetImageName($DirNm); // retrieve random image from current directory
- }
- $file = 'debugtrace.txt';
- file_put_contents($file, $DirNm, FILE_APPEND ); // Write trace
- file_put_contents($file, $ImgNm, FILE_APPEND ); // Write trace
- $ImgTag = '<img src="/' . $ImgNm . '" width="' . $size_x . '" height="' . $size_y . '" alt="' . $ImgNm . '">';
- file_put_contents($file, $ImgTag, FILE_APPEND ); // Write trace
- echo $ImgTag;
- $content = ob_get_contents();
- ob_end_clean();
- return $content;
- }
- add_shortcode("ShowRandomImg", "ShowRandomImg");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement