Advertisement
Guest User

Untitled

a guest
May 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.     $folder = '/home/vakense/public_html/themes/carebear/logo/';
  5.  
  6.  
  7.     $extList = array();
  8.     $extList['gif'] = 'image/gif';
  9.     $extList['jpg'] = 'image/jpeg';
  10.     $extList['jpeg'] = 'image/jpeg';
  11.     $extList['png'] = 'image/png';
  12.    
  13.  
  14. // You don't need to edit anything after this point.
  15.  
  16.  
  17. // --------------------- END CONFIGURATION -----------------------
  18.  
  19. $img = null;
  20. // Adds a tailing slash
  21. if (substr($folder,-1) != '/') {
  22.     $folder = $folder.'/';
  23. }
  24.  
  25. //if url query string variable img is set. That image will be choosen if it excist no randomization
  26. if (isset($_GET['img'])) {
  27.     $imageInfo = pathinfo($_GET['img']);
  28.     if (
  29.         isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
  30.         file_exists( $folder.$imageInfo['basename'] )
  31.     ) {
  32.         $img = $folder.$imageInfo['basename'];
  33.     }
  34. //if img var is not set the dirctory will be scanned for files with gif, jpg, jpeg or png extensions. Those files are added to a list.
  35. } else {
  36.     $fileList = array();
  37.     $handle = opendir($folder);
  38.     while ( false !== ( $file = readdir($handle) ) ) {
  39.         $file_info = pathinfo($file);
  40.         if (
  41.             isset( $extList[ strtolower( $file_info['extension'] ) ] )
  42.         ) {
  43.             $fileList[] = $file;
  44.         }
  45.     }
  46.     closedir($handle);
  47.  
  48.  
  49.     if (count($fileList) > 0) {
  50.        
  51. //      Lets randomize the array inteads
  52. //      $imageNumber = time() % count($fileList);
  53.  
  54.         shuffle($fileList);
  55.                
  56.         $img = $folder.$fileList[0];
  57.     }
  58. }
  59.  
  60. // if the $img variable is set. Set the http header content-type to the mime in corresponding to extension. Then output the data.
  61. if ($img!=null) {
  62.     $imageInfo = pathinfo($img);
  63.     $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  64.     header ($contentType);
  65.     readfile($img);
  66. // If imge variable was not set create a white image instead with a text saying IMAGE ERROR
  67. } else {
  68.     if ( function_exists('imagecreate') ) {
  69.         header ("Content-type: image/png");
  70.         $im = @imagecreate (100, 100)
  71.             or die ("Cannot initialize new GD image stream");
  72.         $background_color = imagecolorallocate ($im, 255, 255, 255);
  73.         $text_color = imagecolorallocate ($im, 0,0,0);
  74.         imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
  75.         imagepng ($im);
  76.         imagedestroy($im);
  77.     }
  78. }
  79.  
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement