0

i have a directory with 1000+ images and piece of code (by codaddict) which selects only first 10 and display it:

<?php
foreach (array_slice(glob("/directory/*.jpg"),0,10) as $path)
?>

ok this works, but i need to select 10 RANDOM images, not the first 10

yes, i can use shuffle first, then slice, but with 1000+ (or 10k+) images, it's not smart to shuffle long arrays just for 10 images, or maybe it is?

also, 2nd problem is that this is not just for one folder with 1000+ images, i need to use this script in other folders too, and some of them will only have 1 image, so i don't want to see errors if there is less than 10 images in a folder

i saw in php manual code for 2 random items, but i won't know how many images will be in folders - 1, 10, 10k... you see the problem

<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>

thanks!

Sunny
  • 207
  • 2
  • 13
  • it's time for you to think about a database solution for Image management - dirs with 10k+ files are slow by design – Philipp Feb 16 '13 at 19:12
  • This question has a list of Big O values for PHP array functions. It can maybe help you: http://stackoverflow.com/questions/2473989/list-of-big-o-for-php-functions – dethtron5000 Feb 16 '13 at 19:13
  • there will be only a few dirs with over 1k images, mostly they will contain around 100 images, but because i'll use same code for 100 or 1000 images i think shuffle int's that great, 100 is ok, but 1000 not :) – Sunny Feb 16 '13 at 19:19

1 Answers1

1
function imageGlobber($myDir, $imgCount) {
    $globVar = glob($myDir."/*.jpg");
    $imgCount = ($imgCount > count($globVar)) ? $imgCount : count($globVar);

    $randKeys = array_rand($globVar, $imgCount);

    $retArray = array();
    foreach($randKeys as $key)
        array_push($retArray, $globVar[$key]);

    return $retArray;
}

I think this is what you are looking for.

Edit : Added duplicate handling as well.

Edit : Improved performance.

Achrome
  • 7,773
  • 14
  • 36
  • 45
  • This is only a skeleton code. I haven't done any error handling, or duplicate handling here. – Achrome Feb 16 '13 at 19:26
  • Okay, so now there will not be any duplicate images selected. – Achrome Feb 16 '13 at 19:31
  • ok thanks for the code, but where can i set the limit of 10 images? i don't see that in code :\ – Sunny Feb 16 '13 at 19:39
  • @Philipp Not really. The while loop breaks only when there are the correct number of unique elements in the array. – Achrome Feb 16 '13 at 19:40
  • @Sunny You should be able to figure that out. – Achrome Feb 16 '13 at 19:40
  • oh.. i see.. but this part of the code doesn't seem to bee very performant - especially the array_unique in each loop – Philipp Feb 16 '13 at 19:44
  • `$rand = array(); while (count($rand) < $imgCount) { $rnd = rand(0, count($globVar)); if (!isset($rand[$rnd])) { $rand[$rnd] = true; } } $randKeys = array_keys($rand);` – Philipp Feb 16 '13 at 19:48
  • In that case, `array_rand` may be used instead, now that we know both parameters. That would give a complete O(n) solution, since it relies on linear polling. – Achrome Feb 16 '13 at 19:49
  • hmmm where should i put the php file, in image folder or parent folder? because i don't see any result, i'm trying all paths but nothing is displaying, this path "/*.jpg" is for php file in image folder? – Sunny Feb 16 '13 at 19:56
  • sorry i quit, can't figure out the path, i'm total noob sorry, i'm using $globVar = glob($myDir."/home/dfgeg/public_html/folder/folder/*.jpg"); but i don't get any results?? – Sunny Feb 16 '13 at 20:08
  • Do you know how to call a PHP function? – Achrome Feb 16 '13 at 20:14
  • sorry i should tell you that i'm total noob, and never worked with functions before :) – Sunny Feb 16 '13 at 20:16
  • ok when i use echo imageGlobber(); now i get Warning: Missing argument 1 for imageGlobber() Missing argument 2 for imageGlobber() – Sunny Feb 16 '13 at 20:22
  • i was really a noob back then :) imageGlobber("folder", "10"); – Sunny Mar 19 '15 at 05:45