randomimg.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. $folder = '../res/images';
  3. $extList = array();
  4. $extList['gif'] = 'image/gif';
  5. $extList['jpg'] = 'image/jpeg';
  6. $extList['jpeg'] = 'image/jpeg';
  7. $extList['png'] = 'image/png';
  8. $img = null;
  9. if (substr($folder,-1) != '/') {
  10. $folder = $folder.'/';
  11. }
  12. /*
  13. //DEBUG: eseguire direttamente lo script, altrimenti non si vede output
  14. var_dump($folder);
  15. $handle = opendir($folder);
  16. var_dump($handle);
  17. exit;
  18. */
  19. if (isset($_GET['img'])) {
  20. $imageInfo = pathinfo($_GET['img']);
  21. if (
  22. isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
  23. file_exists( $folder.$imageInfo['basename'] )
  24. ) {
  25. $img = $folder.$imageInfo['basename'];
  26. }
  27. } else {
  28. $fileList = array();
  29. $handle = opendir($folder);
  30. while ( false !== ( $file = readdir($handle) ) ) {
  31. $file_info = pathinfo($file);
  32. if (
  33. isset( $extList[ strtolower( $file_info['extension'] ) ] )
  34. ) {
  35. $fileList[] = $file;
  36. }
  37. }
  38. closedir($handle);
  39. if (count($fileList) > 0) {
  40. $imageNumber = time() % count($fileList);
  41. $img = $folder.$fileList[$imageNumber];
  42. }
  43. }
  44. if ($img!=null) {
  45. $imageInfo = pathinfo($img);
  46. $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  47. header ($contentType);
  48. readfile($img);
  49. } else {
  50. if ( function_exists('imagecreate') ) {
  51. header ("Content-type: image/png");
  52. $im = @imagecreate (100, 100)
  53. or die ("Cannot initialize new GD image stream");
  54. $background_color = imagecolorallocate ($im, 255, 255, 255);
  55. $text_color = imagecolorallocate ($im, 0,0,0);
  56. imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
  57. imagepng ($im);
  58. imagedestroy($im);
  59. }
  60. }
  61. ?>