2 //////////////////////////////////////////////////////////////
3 /// phpThumb() by James Heinrich <info@silisoftware.com> //
4 // available at http://phpthumb.sourceforge.net ///
5 //////////////////////////////////////////////////////////////
7 // phpThumb.demo.random.php //
8 // James Heinrich <info@silisoftware.com> //
10 // Display a random image from a specified directory. //
11 // Run with no parameters for usage instructions. //
13 //////////////////////////////////////////////////////////////
15 function SelectRandomImage($dirname='.', $portrait=true, $landscape=true, $square=true) {
16 // return a random image filename from $dirname
17 // the last 3 parameters determine what aspect ratio of images
19 $possibleimages = array();
20 if ($dh = opendir($dirname)) {
21 while ($file = readdir($dh)) {
22 if (is_file($dirname.'/'.$file) && eregi('\.(jpg|jpeg|gif|png|tiff|bmp)$', $file)) {
23 if ($gis = @GetImageSize($dirname.'/'.$file)) {
24 if ($portrait && ($gis[0] < $gis[1])) {
26 $possibleimages[] = $file;
27 } elseif ($landscape && ($gis[0] > $gis[1])) {
29 $possibleimages[] = $file;
32 $possibleimages[] = $file;
39 if (empty($possibleimages)) {
42 if (phpversion() < '4.2.0') {
45 $randkey = mt_rand(0, count($possibleimages) - 1);
46 return realpath($dirname.'/'.$possibleimages[$randkey]);
49 if (@$_REQUEST['dir']) {
50 if (is_dir($_REQUEST['dir'])) {
52 if (!@$_REQUEST['o']) {
53 $_REQUEST['o'] = 'PLS';
55 $_REQUEST['o'] = strtoupper($_REQUEST['o']);
56 $portrait = (strpos(@$_REQUEST['o'], 'P') !== false);
57 $landscape = (strpos(@$_REQUEST['o'], 'L') !== false);
58 $square = (strpos(@$_REQUEST['o'], 'S') !== false);
59 $randomSRC = SelectRandomImage($_REQUEST['dir'], $portrait, $landscape, $square);
60 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
61 $randomSRC = str_replace('\\', '/', eregi_replace('^'.realpath(@$_SERVER['DOCUMENT_ROOT']), '', realpath($randomSRC)));
63 $randomSRC = str_replace(realpath(@$_SERVER['DOCUMENT_ROOT']), '', realpath($randomSRC));
66 $otherParams = array();
67 foreach ($_GET as $key => $value) {
68 if (($key == 'dir') || ($key == 'o')) {
71 if (is_array($value)) {
72 foreach ($value as $vkey => $vvalue) {
73 $otherParams[] = urlencode($key).'['.urlencode($vkey).']='.urlencode($vvalue);
76 $otherParams[] = urlencode($key).'='.urlencode($value);
79 header('Location: ./phpThumb/phpThumb.php?src='.urlencode($randomSRC).'&'.implode('&', $otherParams));
83 die($_REQUEST['dir'].' is not a directory');
88 echo '<html><body>Usage: <b>'.basename($_SERVER['PHP_SELF']).'?dir=<i><directory></i>&<i><phpThumb parameters></i></b>&o=<i>(P|L|S)</i><br><br>Examples:<ul>';
89 echo '<li>'.basename($_SERVER['PHP_SELF']).'?./images/&o=L <i>(landscape images only)</i></li>';
90 echo '<li>'.basename($_SERVER['PHP_SELF']).'?./images/&o=PS <i>(portrait or square images only)</i></li>';
91 echo '</ul></body></html>';