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