Advertisement
create

[Coding] Rotating Image

Oct 8th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. This PHP script will randomly select an image file from a folder of images on your webserver. You can then link to it as you would any standard image file and you'll see a random image each time you reload.
  2.  
  3. When you want to add or remove images from the rotation-pool, just add or remove them from the image rotation folder. There is no need to edit the original file every time you add a new image.
  4.  
  5.  
  6.  
  7. Instructions
  8.  
  9. 1. Copy and paste the code below into a txt file. Rename the file to be 'index.php'.
  10.  
  11. 2. Create a folder on your webserver called 'rotate.jpeg'.
  12.  
  13. 3. Upload the file (index.php) to your rotate.jpeg folder.
  14.  
  15. 4. Upload all the images you want to be in the cycle into that same folder.
  16.  
  17. 5. Link to the file as you would any normal image file, like this:
  18.  
  19. <img src="http://example.com/rotate.jpeg">
  20. or on a web forum
  21. {img}example.com/rotate.jpeg{/img}
  22.  
  23. You can also specify the image to display like this:
  24.  
  25. <img src="http://example.com/rotate.php?img=gorilla.jpg">
  26.  
  27. This would specify that an image named "gorilla.jpg" located
  28. in the image-rotation folder should be displayed.
  29.  
  30. That's all, enjoy.
  31.  
  32.  
  33.  
  34.  
  35.  
  36. <?php
  37.  
  38.  
  39. $folder = '.';
  40.  
  41.  
  42. $extList = array();
  43. $extList['gif'] = 'image/gif';
  44. $extList['jpg'] = 'image/jpeg';
  45. $extList['jpeg'] = 'image/jpeg';
  46. $extList['png'] = 'image/png';
  47.  
  48.  
  49. $img = null;
  50.  
  51. if (substr($folder,-1) != '/') {
  52. $folder = $folder.'/';
  53. }
  54.  
  55. if (isset($_GET['img'])) {
  56. $imageInfo = pathinfo($_GET['img']);
  57. if (
  58. isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
  59. file_exists( $folder.$imageInfo['basename'] )
  60. ) {
  61. $img = $folder.$imageInfo['basename'];
  62. }
  63. } else {
  64. $fileList = array();
  65. $handle = opendir($folder);
  66. while ( false !== ( $file = readdir($handle) ) ) {
  67. $file_info = pathinfo($file);
  68. if (
  69. isset( $extList[ strtolower( $file_info['extension'] ) ] )
  70. ) {
  71. $fileList[] = $file;
  72. }
  73. }
  74. closedir($handle);
  75.  
  76. if (count($fileList) > 0) {
  77. $imageNumber = time() % count($fileList);
  78. $img = $folder.$fileList[$imageNumber];
  79. }
  80. }
  81.  
  82. if ($img!=null) {
  83. $imageInfo = pathinfo($img);
  84. $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  85. header ($contentType);
  86. readfile($img);
  87. } else {
  88. if ( function_exists('imagecreate') ) {
  89. header ("Content-type: image/png");
  90. $im = @imagecreate (100, 100)
  91. or die ("Cannot initialize new GD image stream");
  92. $background_color = imagecolorallocate ($im, 255, 255, 255);
  93. $text_color = imagecolorallocate ($im, 0,0,0);
  94. imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
  95. imagepng ($im);
  96. imagedestroy($im);
  97. }
  98. }
  99.  
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement