Last active
August 6, 2016 10:42
-
-
Save peterhil/6f14bf3fdda62b73c30c4ca90b3a3fb9 to your computer and use it in GitHub Desktop.
Random image with Php
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 characters
| <?php | |
| $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); | |
| 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 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 '.' | |
| 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, $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) { | |
| $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, $prefix); | |
| $random_image = randomly_pick($image_files); | |
| if (is_web()) { | |
| header('Location: ' . $random_image); | |
| } | |
| else { | |
| // echo implode("\n", $image_files); | |
| // echo "\n\n"; | |
| // echo "--- Your random image is: ---\n"; | |
| echo $random_image; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment