Last active
August 6, 2016 10:42
-
-
Save peterhil/6f14bf3fdda62b73c30c4ca90b3a3fb9 to your computer and use it in GitHub Desktop.
Revisions
-
peterhil revised this gist
Aug 6, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -88,6 +88,9 @@ function get_image_files($directory, $recursive = false, $prefix = false) { } function randomly_pick($array) { if (empty($array)) { return null; } $index = mt_rand(0, max(0, count($array) - 1)); $reindexed = array_values($array); // Make sure the array has consecutive indexes return $reindexed[$index]; -
peterhil revised this gist
Aug 6, 2016 . 1 changed file with 20 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,25 @@ <?php // Copyright (c) 2016 Peter Hillerström // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. $directory = '.'; $include_sub_directories = true; $prefix = is_web() ? preg_replace('/[^-a-zA-Z0-9_]/', '', $_GET['prefix']) : $argv[1]; -
peterhil revised this gist
Aug 6, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ $directory = '.'; $include_sub_directories = true; $prefix = is_web() ? preg_replace('/[^-a-zA-Z0-9_]/', '', $_GET['prefix']) : $argv[1]; function is_web() { return $_SERVER["SERVER_SOFTWARE"]; -
peterhil revised this gist
Aug 6, 2016 . 1 changed file with 20 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,11 @@ $directory = '.'; $include_sub_directories = true; $prefix = is_web() ? $_GET['prefix'] : $argv[1]; function is_web() { return $_SERVER["SERVER_SOFTWARE"]; } function is_image($file_name, $extensions = ['jpg', 'jpeg', 'png', 'gif']) { $extensions_match = implode('|', $extensions); @@ -21,6 +26,12 @@ function visible_image_file($file_name) { ); } function has_prefix($prefix, $separator = '_') { return function ($file_name) use ($prefix, $separator) { return mb_ereg_match("^{$prefix}{$separator}", $file_name); }; } function get_listing($directory, $recursive = true) { $file_names = []; chdir($directory); // WTF: PHP does not find all files for other directories than '.' @@ -47,11 +58,13 @@ function get_listing($directory, $recursive = true) { return $file_names; } function get_image_files($directory, $recursive = false, $prefix = false) { $file_names = get_listing($directory, $recursive); if ($prefix) { $file_names = array_filter($file_names, has_prefix($prefix)); } return array_values(array_filter($file_names, visible_image_file)); } function randomly_pick($array) { @@ -60,10 +73,10 @@ function randomly_pick($array) { return $reindexed[$index]; } $image_files = get_image_files($directory, $recursive = $include_sub_directories, $prefix); $random_image = randomly_pick($image_files); if (is_web()) { header('Location: ' . $random_image); } else { -
peterhil revised this gist
Aug 6, 2016 . 1 changed file with 19 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,34 +21,35 @@ function visible_image_file($file_name) { ); } function get_listing($directory, $recursive = true) { $file_names = []; chdir($directory); // WTF: PHP does not find all files for other directories than '.' if (!$recursive) { $file_names = array_values(array_filter(scandir('.'), is_file)); } else { $file_infos = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( '.', FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS ), RecursiveIteratorIterator::LEAVES_ONLY, RecursiveIteratorIterator::CATCH_GET_CHILD ); foreach ($file_infos as $ignore) { $file_names[] = $file_infos->getSubPathname(); } } return $file_names; } function get_image_files($directory, $recursive = false) { return array_values(array_filter( get_listing($directory, $recursive), visible_image_file )); } -
peterhil created this gist
Aug 6, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ <?php $directory = '.'; $include_sub_directories = true; function is_image($file_name, $extensions = ['jpg', 'jpeg', 'png', 'gif']) { $extensions_match = implode('|', $extensions); return mb_ereg_match(".*\.({$extensions_match})$", $file_name, 'i'); } function is_visible($file_name) { return $file_name[0] !== '.'; } function visible_image_file($file_name) { return is_visible($file_name) && ( is_dir($file_name) || ( is_file($file_name) && is_image($file_name) ) ); } function get_listing($directory) { chdir($directory); // WTF: PHP does not find all files for other directories than '.' return array_values(array_filter(scandir('.'), is_file)); } function get_recursive_listing($directory) { chdir($directory); // WTF: PHP does not find all files for other directories than '.' $file_infos = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( '.', FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS ), RecursiveIteratorIterator::LEAVES_ONLY, RecursiveIteratorIterator::CATCH_GET_CHILD ); $file_names = []; foreach ($file_infos as $ignore) { $file_names[] = $file_infos->getSubPathname(); } return $file_names; } function get_image_files($directory, $recursive = false) { return array_values(array_filter( $recursive ? get_recursive_listing($directory) : get_listing($directory), visible_image_file )); } function randomly_pick($array) { $index = mt_rand(0, max(0, count($array) - 1)); $reindexed = array_values($array); // Make sure the array has consecutive indexes return $reindexed[$index]; } $image_files = get_image_files($directory, $recursive = $include_sub_directories); $random_image = randomly_pick($image_files); if ($_HOST) { header('Location: ' . $random_image); } else { // echo implode("\n", $image_files); // echo "\n\n"; // echo "--- Your random image is: ---\n"; echo $random_image; }