1<?php
2//////////////////////////////////////////////////////////////
3//   phpThumb() by James Heinrich <info@silisoftware.com>   //
4//        available at http://phpthumb.sourceforge.net      //
5//         and/or https://github.com/JamesHeinrich/phpThumb //
6//////////////////////////////////////////////////////////////
7///                                                         //
8// phpThumb.demo.random.php                                 //
9// James Heinrich <info@silisoftware.com>                   //
10//                                                          //
11// Display a random image from a specified directory.       //
12// Run with no parameters for usage instructions.           //
13//                                                          //
14//////////////////////////////////////////////////////////////
15die('For security reasons, this demo is disabled by default. Please comment out line '.__LINE__.' in '.basename(__FILE__));
16
17function SelectRandomImage($dirname='.', $portrait=true, $landscape=true, $square=true) {
18	// return a random image filename from $dirname
19	// the last 3 parameters determine what aspect ratio of images
20	// may be returned
21	$possibleimages = array();
22	if ($dh = opendir($dirname)) {
23		while ($file = readdir($dh)) {
24			if (is_file($dirname.'/'.$file) && preg_match('#\\.(jpg|jpeg|gif|png|tiff|bmp)$#i', $file)) {
25				if ($gis = @getimagesize($dirname.'/'.$file)) {
26					if ($portrait && ($gis[0] < $gis[1])) {
27						// portrait
28						$possibleimages[] = $file;
29					} elseif ($landscape && ($gis[0] > $gis[1])) {
30						// landscape
31						$possibleimages[] = $file;
32					} elseif ($square) {
33						// square
34						$possibleimages[] = $file;
35					}
36				}
37			}
38		}
39		closedir($dh);
40	}
41	if (empty($possibleimages)) {
42		return false;
43	}
44	if (PHP_VERSION < '4.2.0') {
45		mt_srand(time());
46	}
47	$randkey = mt_rand(0, count($possibleimages) - 1);
48	return realpath($dirname.'/'.$possibleimages[$randkey]);
49}
50
51if (@$_REQUEST['dir']) {
52	if (is_dir($_REQUEST['dir'])) {
53
54		if (!@$_REQUEST['o']) {
55			$_REQUEST['o'] = 'PLS';
56		}
57		$_REQUEST['o'] = strtoupper($_REQUEST['o']);
58		$portrait  = (strpos(@$_REQUEST['o'], 'P') !== false);
59		$landscape = (strpos(@$_REQUEST['o'], 'L') !== false);
60		$square    = (strpos(@$_REQUEST['o'], 'S') !== false);
61		$randomSRC = SelectRandomImage($_REQUEST['dir'], $portrait, $landscape, $square);
62		if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
63			$randomSRC = str_replace('\\', '/', preg_replace('#^'.realpath(@$_SERVER['DOCUMENT_ROOT']).'#i', '', realpath($randomSRC)));
64		} else {
65			$randomSRC = str_replace(realpath(@$_SERVER['DOCUMENT_ROOT']), '', realpath($randomSRC));
66		}
67
68		$otherParams = array();
69		foreach ($_GET as $key => $value) {
70			if (($key == 'dir') || ($key == 'o')) {
71				continue;
72			}
73			if (is_array($value)) {
74				foreach ($value as $vkey => $vvalue) {
75					$otherParams[] = urlencode($key).'['.urlencode($vkey).']='.urlencode($vvalue);
76				}
77			} else {
78				$otherParams[] = urlencode($key).'='.urlencode($value);
79			}
80		}
81		header('Location: ../phpThumb.php?src='.urlencode($randomSRC).'&'.implode('&', $otherParams));
82		exit;
83
84	} else {
85		echo htmlentities($_REQUEST['dir']).' is not a directory';
86		exit;
87	}
88
89} else {
90
91	echo '<html><body>Usage: <b>'.basename($_SERVER['PHP_SELF']).'?dir=<i>&lt;directory&gt;</i>&amp;<i>&lt;phpThumb parameters&gt;</i></b>&amp;o=<i>(P|L|S)</i><br><br>Examples:<ul>';
92	echo '<li>'.basename($_SERVER['PHP_SELF']).'?./images/&o=L <i>(landscape images only)</i></li>';
93	echo '<li>'.basename($_SERVER['PHP_SELF']).'?./images/&o=PS <i>(portrait or square images only)</i></li>';
94	echo '</ul></body></html>';
95
96}
97