Skip to content

Instantly share code, notes, and snippets.

@peterhil
Last active August 6, 2016 10:42
Show Gist options
  • Save peterhil/6f14bf3fdda62b73c30c4ca90b3a3fb9 to your computer and use it in GitHub Desktop.
Save peterhil/6f14bf3fdda62b73c30c4ca90b3a3fb9 to your computer and use it in GitHub Desktop.
Random image with Php
<?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];
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) {
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];
}
$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