PHP – Sample of random pictures from directory at the server
Random files with requested extensions; entered count or all,
2013-06-17
PHP function for random selection of entered number of files (or all ones, if there is not enough of them; or all ones, if such request is entered)
with requested extensions from entered directory at server (webhosting).
Typically for selection and insertion of needed number of random pictures, e.g. with partners of project, sponsors, references to another projects, examples, or illustration and decorative graphics.
//Martin Adámek, www.adamek.cz ///////////////////////////////////// //$extensions List of allowed extensions (per 3 or 4 characters) //$path Path to the directory to exploration at the server (without slash at the end) //$how_much Number of names of files to result (0 == all) ///////////////////////////////////// //Finding of names of requested number of random files with requested extensions in requested directory function return_random_filenames_from_directory($extensions = array('jpg','jpeg','png'), $path = '', $how_much = 1) // V definici funkce uvedeny výchozí hodnoty vstupních proměnných, které se implicitně použijí, když dané proměnné nebudou při volání funkce explicitně zadány { //Initialization $how_much = intval($how_much); //To treat texts, decimal numbers and another unpleasant values //After error or with array at the input returns 0 or 1 if ($how_much < 0) $how_much = 0; //To treat negative input $files = array(); //For found suiting filenames $which_ones = array(); //For keys referring to selected names of files $result = array(); //For sample to return (selected names of files) //Loading of names of suiting files $directory = opendir($path); while ($file = readdir($directory)) if ( in_array( substr($file, strlen($file)-3, 3), $extensions) or in_array( substr($file, strlen($file)-4, 4), $extensions) ) // Note: For list of files with all extensions (any extension) without this restriction is necessary to filter out directory and upper directory: $file!='.' and $file!='..' $files[] = $file; closedir($directory); //Selection of values to return if (/*count($files) <= 1 or */ $how_much == 0 or count($files) <= $how_much) // Necessary to return all found filenames // Platné podmínky: Chce všechny; nebo se jich našlo méně než je požadováno // Zakomentovaná podmínka: Našel se žádný nebo jeden (netřeba samostatně řešit, řeší to další dvě podmínky, když nahoře zajišťujeme, že $how_much>=0 a je celé číslo) $result = $files; // We are returning all ones else // Necessary to select just requested count { $which_ones = array_rand($files,$how_much); if ($how_much == 1) // array_rand returns directly key $result[] = $files[$which_ones]; // …but this function will return it as first value of array, to it be possible to process results always the same way else // array_rand returns array of keys foreach ($which_ones as $ktery) $result[] = $files[$ktery]; } return $result; //Array with filenames with requested extensions in requested directory } //Insertion of images with logotypes of partners of project function insert_partners_logotypes() { $files=return_random_filenames_from_directory(array('jpg','jpeg','png','gif'), '/img/logotypes', 2); echo '<div id="project_partners">'; foreach ($files as $file) echo '<img src="img/logotypes'.$file.'" alt="Partner projektu" title="Partner projektu" />'; // I když v tomto případě by se do altu správně měl psát přímo konkrétní název firmy (text viditelný na obrázku), což se tu neděje //V případě čistě dekorativního nebo jinak obsahově opravdu bezvýznamného obrázku nechte alt prázdný echo '</div>'; }
Although function array_rand() returns various results during various running (if it is possible to choose from more possibilities and isn't necessary to return all items),
but the items in result are always in the same mutual order of chosen items.
It is possible to use function shuffle() for randomizing of items' order.