Advertisement
ekasyahwan

BG Random Kampong

Mar 22nd, 2014
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. DOWNLOADED FROM http://www.marcofolio.net/
  5. Check it out for more interesting scripts & downloads
  6.  
  7. AUTOMATIC IMAGE ROTATOR
  8. Version 2.2 - December 4, 2003
  9. Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd.
  10. All Rights Reserved.
  11.  
  12. http://www.hiveware.com/imagerotator.php
  13.  
  14. http://www.automaticlabs.com/
  15.  
  16.  
  17. DISCLAIMER
  18. Automatic, Ltd. makes no representations or warranties about
  19. the suitability of the software, either express or
  20. implied, including but not limited to the implied
  21. warranties of merchantability, fitness for a particular
  22. purpose, or non-infringement. Dan P. Benjamin and Automatic, Ltd.
  23. shall not be liable for any damages suffered by licensee
  24. as a result of using, modifying or distributing this
  25. software or its derivatives.
  26.  
  27.  
  28. ABOUT
  29. This PHP script will randomly select an image file from a
  30. folder of images on your webserver. You can then link to it
  31. as you would any standard image file and you'll see a random
  32. image each time you reload.
  33.  
  34. When you want to add or remove images from the rotation-pool,
  35. just add or remove them from the image rotation folder.
  36.  
  37.  
  38. VERSION CHANGES
  39. Version 1.0
  40. - Release version
  41.  
  42. Version 1.5
  43. - Tweaked a few boring bugs
  44.  
  45. Version 2.0
  46. - Complete rewrite from the ground-up
  47. - Made it clearer where to make modifications
  48. - Made it easier to specify/change the rotation-folder
  49. - Made it easier to specify/change supported image types
  50. - Wrote better instructions and info (you're them reading now)
  51. - Significant speed improvements
  52. - More error checking
  53. - Cleaner code (albeit more PHP-specific)
  54. - Better/faster random number generation and file-type parsing
  55. - Added a feature where the image to display can be specified
  56. - Added a cool feature where, if an error occurs (such as no
  57. images being found in the specified folder) *and* you're
  58. lucky enough to have the GD libraries compiled into PHP on
  59. your webserver, we generate a replacement "error image" on
  60. the fly.
  61.  
  62. Version 2.1
  63. - Updated a potential security flaw when value-matching
  64. filenames
  65.  
  66. Version 2.2
  67. - Updated a few more potential security issues
  68. - Optimized the code a bit.
  69. - Expanded the doc for adding new mime/image types.
  70.  
  71. Thanks to faithful ALA reader Justin Greer for
  72. lots of good tips and solid code contribution!
  73.  
  74.  
  75. INSTRUCTIONS
  76. 1. Modify the $folder setting in the configuration section below.
  77. 2. Add image types if needed (most users can ignore that part).
  78. 3. Upload this file (rotate.php) to your webserver. I recommend
  79. uploading it to the same folder as your images.
  80. 4. Link to the file as you would any normal image file, like this:
  81.  
  82. <img src="http://example.com/rotate.php">
  83.  
  84. 5. You can also specify the image to display like this:
  85.  
  86. <img src="http://example.com/rotate.php?img=gorilla.jpg">
  87.  
  88. This would specify that an image named "gorilla.jpg" located
  89. in the image-rotation folder should be displayed.
  90.  
  91. That's it, you're done.
  92.  
  93. */
  94.  
  95.  
  96.  
  97.  
  98. /* ------------------------- CONFIGURATION -----------------------
  99.  
  100.  
  101. Set $folder to the full path to the location of your images.
  102. For example: $folder = '/user/me/example.com/images/';
  103. If the rotate.php file will be in the same folder as your
  104. images then you should leave it set to $folder = '.';
  105.  
  106. */
  107.  
  108.  
  109. $folder = '../img';
  110.  
  111.  
  112. /*
  113.  
  114. Most users can safely ignore this part. If you're a programmer,
  115. keep reading, if not, you're done. Go get some coffee.
  116.  
  117. If you'd like to enable additional image types other than
  118. gif, jpg, and png, add a duplicate line to the section below
  119. for the new image type.
  120.  
  121. Add the new file-type, single-quoted, inside brackets.
  122.  
  123. Add the mime-type to be sent to the browser, also single-quoted,
  124. after the equal sign.
  125.  
  126. For example:
  127.  
  128. PDF Files:
  129.  
  130. $extList['pdf'] = 'application/pdf';
  131.  
  132. CSS Files:
  133.  
  134. $extList['css'] = 'text/css';
  135.  
  136. You can even serve up random HTML files:
  137.  
  138. $extList['html'] = 'text/html';
  139. $extList['htm'] = 'text/html';
  140.  
  141. Just be sure your mime-type definition is correct!
  142.  
  143. */
  144.  
  145. $extList = array();
  146. $extList['gif'] = 'image/gif';
  147. $extList['jpg'] = 'image/jpeg';
  148. $extList['jpeg'] = 'image/jpeg';
  149. $extList['png'] = 'image/png';
  150.  
  151.  
  152. // You don't need to edit anything after this point.
  153.  
  154.  
  155. // --------------------- END CONFIGURATION -----------------------
  156.  
  157. $img = null;
  158.  
  159. if (substr($folder,-1) != '/') {
  160. $folder = $folder.'/';
  161. }
  162.  
  163. if (isset($_GET['img'])) {
  164. $imageInfo = pathinfo($_GET['img']);
  165. if (
  166. isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
  167. file_exists( $folder.$imageInfo['basename'] )
  168. ) {
  169. $img = $folder.$imageInfo['basename'];
  170. }
  171. } else {
  172. $fileList = array();
  173. $handle = opendir($folder);
  174. while ( false !== ( $file = readdir($handle) ) ) {
  175. $file_info = pathinfo($file);
  176. if (
  177. isset( $extList[ strtolower( $file_info['extension'] ) ] )
  178. ) {
  179. $fileList[] = $file;
  180. }
  181. }
  182. closedir($handle);
  183.  
  184. if (count($fileList) > 0) {
  185. $imageNumber = time() % count($fileList);
  186. $img = $folder.$fileList[$imageNumber];
  187. }
  188. }
  189.  
  190. if ($img!=null) {
  191. $imageInfo = pathinfo($img);
  192. $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  193. header ($contentType);
  194. readfile($img);
  195. } else {
  196. if ( function_exists('imagecreate') ) {
  197. header ("Content-type: image/png");
  198. $im = @imagecreate (100, 100)
  199. or die ("Cannot initialize new GD image stream");
  200. $background_color = imagecolorallocate ($im, 255, 255, 255);
  201. $text_color = imagecolorallocate ($im, 0,0,0);
  202. imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
  203. imagepng ($im);
  204. imagedestroy($im);
  205. }
  206. }
  207.  
  208. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement