OSDN Git Service

original file
[nucleus-jp/nucleus-plugins.git] / trunk / NP_TinyMCE2j / tinymce2j / plugins / ibrowser / scripts / random.php
1 <?php
2 //////////////////////////////////////////////////////////////
3 ///  phpThumb() by James Heinrich <info@silisoftware.com>   //
4 //        available at http://phpthumb.sourceforge.net     ///
5 //////////////////////////////////////////////////////////////
6 ///                                                         //
7 // phpThumb.demo.random.php                                 //
8 // James Heinrich <info@silisoftware.com>                   //
9 //                                                          //
10 // Display a random image from a specified directory.       //
11 // Run with no parameters for usage instructions.           //
12 //                                                          //
13 //////////////////////////////////////////////////////////////
14
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
18         // may be returned
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])) {
25                                                 // portrait
26                                                 $possibleimages[] = $file;
27                                         } elseif ($landscape && ($gis[0] > $gis[1])) {
28                                                 // landscape
29                                                 $possibleimages[] = $file;
30                                         } elseif ($square) {
31                                                 // square
32                                                 $possibleimages[] = $file;
33                                         }
34                                 }
35                         }
36                 }
37                 closedir($dh);
38         }
39         if (empty($possibleimages)) {
40                 return false;
41         }
42         if (phpversion() < '4.2.0') {
43                 mt_srand(time());
44         }
45         $randkey = mt_rand(0, count($possibleimages) - 1);
46         return realpath($dirname.'/'.$possibleimages[$randkey]);
47 }
48
49 if (@$_REQUEST['dir']) {
50         if (is_dir($_REQUEST['dir'])) {
51
52                 if (!@$_REQUEST['o']) {
53                         $_REQUEST['o'] = 'PLS';
54                 }
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)));
62                 } else {
63                         $randomSRC = str_replace(realpath(@$_SERVER['DOCUMENT_ROOT']), '', realpath($randomSRC));
64                 }
65
66                 $otherParams = array();
67                 foreach ($_GET as $key => $value) {
68                         if (($key == 'dir') || ($key == 'o')) {
69                                 continue;
70                         }
71                         if (is_array($value)) {
72                                 foreach ($value as $vkey => $vvalue) {
73                                         $otherParams[] = urlencode($key).'['.urlencode($vkey).']='.urlencode($vvalue);
74                                 }
75                         } else {
76                                 $otherParams[] = urlencode($key).'='.urlencode($value);
77                         }
78                 }
79                 header('Location: ./phpThumb/phpThumb.php?src='.urlencode($randomSRC).'&'.implode('&', $otherParams));
80                 exit;
81
82         } else {
83                 die($_REQUEST['dir'].' is not a directory');
84         }
85
86 } else {
87
88         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>';
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>';
92
93 }
94
95 ?>