I made a function about random backgrounds
And those images will unique as possible. (Shuffle again after all gone)
My steps is :
- Setup the array
- Shuffle it
- Do function (check if
isset()
so run on)
PHP :
$rand_image_ids = [1048,1049,1061,1064,983,972,964];
shuffle($rand_image_ids);
$rand_images_i = 0;
function rand_image($width,$height){
global $rand_image_ids,$rand_images_i;
if( !isset($rand_image_ids[$rand_images_i]) ) {
shuffle($rand_image_ids);
$rand_images_i = 0;
}
$id = $rand_image_ids[$rand_images_i];
$rand_images_i++;
return 'https://unsplash.it/'.$width.'/'.$height.'?image='.$id;
}
So I am looking for :
- Bugs ?
- Any better way to do ?
- Shorter codes if possible
Possible with PHP 5.6