PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/phpThumb/demo/phpThumb.demo.random.php

http://github.com/Datawalke/Coordino
PHP | 95 lines | 74 code | 3 blank | 18 comment | 15 complexity | 5b105e3d89bbd4e7e1534b09ac1203f7 MD5 | raw file
  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. function SelectRandomImage($dirname='.', $portrait=true, $landscape=true, $square=true) {
  15. // return a random image filename from $dirname
  16. // the last 3 parameters determine what aspect ratio of images
  17. // may be returned
  18. $possibleimages = array();
  19. if ($dh = opendir($dirname)) {
  20. while ($file = readdir($dh)) {
  21. if (is_file($dirname.'/'.$file) && eregi('\.(jpg|jpeg|gif|png|tiff|bmp)$', $file)) {
  22. if ($gis = @GetImageSize($dirname.'/'.$file)) {
  23. if ($portrait && ($gis[0] < $gis[1])) {
  24. // portrait
  25. $possibleimages[] = $file;
  26. } elseif ($landscape && ($gis[0] > $gis[1])) {
  27. // landscape
  28. $possibleimages[] = $file;
  29. } elseif ($square) {
  30. // square
  31. $possibleimages[] = $file;
  32. }
  33. }
  34. }
  35. }
  36. closedir($dh);
  37. }
  38. if (empty($possibleimages)) {
  39. return false;
  40. }
  41. if (phpversion() < '4.2.0') {
  42. mt_srand(time());
  43. }
  44. $randkey = mt_rand(0, count($possibleimages) - 1);
  45. return realpath($dirname.'/'.$possibleimages[$randkey]);
  46. }
  47. if (@$_REQUEST['dir']) {
  48. if (is_dir($_REQUEST['dir'])) {
  49. if (!@$_REQUEST['o']) {
  50. $_REQUEST['o'] = 'PLS';
  51. }
  52. $_REQUEST['o'] = strtoupper($_REQUEST['o']);
  53. $portrait = (strpos(@$_REQUEST['o'], 'P') !== false);
  54. $landscape = (strpos(@$_REQUEST['o'], 'L') !== false);
  55. $square = (strpos(@$_REQUEST['o'], 'S') !== false);
  56. $randomSRC = SelectRandomImage($_REQUEST['dir'], $portrait, $landscape, $square);
  57. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  58. $randomSRC = str_replace('\\', '/', eregi_replace('^'.realpath(@$_SERVER['DOCUMENT_ROOT']), '', realpath($randomSRC)));
  59. } else {
  60. $randomSRC = str_replace(realpath(@$_SERVER['DOCUMENT_ROOT']), '', realpath($randomSRC));
  61. }
  62. $otherParams = array();
  63. foreach ($_GET as $key => $value) {
  64. if (($key == 'dir') || ($key == 'o')) {
  65. continue;
  66. }
  67. if (is_array($value)) {
  68. foreach ($value as $vkey => $vvalue) {
  69. $otherParams[] = urlencode($key).'['.urlencode($vkey).']='.urlencode($vvalue);
  70. }
  71. } else {
  72. $otherParams[] = urlencode($key).'='.urlencode($value);
  73. }
  74. }
  75. header('Location: ../phpThumb.php?src='.urlencode($randomSRC).'&'.implode('&', $otherParams));
  76. exit;
  77. } else {
  78. die($_REQUEST['dir'].' is not a directory');
  79. }
  80. } else {
  81. 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>';
  82. echo '<li>'.basename($_SERVER['PHP_SELF']).'?./images/&o=L <i>(landscape images only)</i></li>';
  83. echo '<li>'.basename($_SERVER['PHP_SELF']).'?./images/&o=PS <i>(portrait or square images only)</i></li>';
  84. echo '</ul></body></html>';
  85. }
  86. ?>