- Displays a random image from images folder. The contenttype will be the images, so you can use this image for an image source in HTML, BBCode, etc.
Random Image
1 <?php
2
3 $folder = 'images/';
4
5 $extList = array();
6 $extList['gif'] = 'image/gif';
7 $extList['jpg'] = 'image/jpeg';
8 $extList['jpeg'] = 'image/jpeg';
9 $extList['png'] = 'image/png';
10
11 $img = null;
12
13 if (isset($_GET['img'])) {
14 $imageInfo = pathinfo($_GET['img']);
15
16 if (isset($extList[strtolower($imageInfo['extension'])]) && file_exists($folder.$imageInfo['basename']))
17 $img = $folder.$imageInfo['basename'];
18 } else {
19 $fileList = array();
20 $handle = opendir($folder);
21
22 while (false !== ($file = readdir($handle))) {
23 $file_info = pathinfo($file);
24 if (isset( $extList[ strtolower( $file_info['extension'])])) {
25 $fileList[] = $file;
26 }
27 }
28 closedir($handle);
29
30 if (count($fileList) > 0) {
31 $imageNumber = time() % count($fileList);
32 $img = $folder.$fileList[$imageNumber];
33 }
34 }
35
36 if ($img!=null) {
37 $imageInfo = pathinfo($img);
38 $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
39 header($contentType);
40 readfile($img);
41 } else {
42 if (function_exists('imagecreate')) {
43 header ("Content-type: image/png");
44 $im = @imagecreate (100, 100) or die ("Cannot initialize new GD image stream");
45 $background_color = imagecolorallocate ($im, 255, 255, 255);
46 $text_color = imagecolorallocate ($im, 0,0,0);
47 imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
48 imagepng ($im);
49 imagedestroy($im);
50 }
51 }
52
53 ?>
Comments
Sign in to leave a comment.

