WebsiteBaker 2.13.9 is now available!
R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WBhttps://forum.websitebaker.org/index.php/topic,32355.0.html
/media/photos/ |-- /photos1/ |-- photo1.jpg |-- photo2.jpg |-- /photos2/ |-- photo3.jpg |-- photo4.jpg
I have one on my HP.
$dir = ( (isset($dir) && ($dir!='') ) ? $dir : ''); $folder=opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$dir.'/.'); $names = array();while ($file = readdir($folder)) { $ext=strtolower(substr($file,-4)); if ($ext==".jpg"||$ext==".gif"||$ext==".png"){ $names[count($names)] = $file; }}closedir($folder);shuffle($names);$image=$names[0]; $name=substr($image,0,-4);return '<img src="'.WB_URL.MEDIA_DIRECTORY.'/'.$dir.'/'.$image.'" alt="'.$name.'" width="95%" />';
The project I am currently working on (a relaunch for a disco) has almost 30,000 pictures altogether, sorted by year (2003-2014) and about 20-30 albums per year. It is very neat to show all these pictures with the foldergallery. But on the homepage I would like to show 2 or 3 random pictures from all these pictures. Ideally even with the name of the folder they are in and a link to the entire folder...
<?php//:get a random image URL from folder tree//:Commandline to use: [[RandomImageFromTree?dir=foldergalery/archive/]] $sRetval = 'URL to a "NoImageAvail" pic'; $sStartFolder = rtrim(str_replace('\\', '/', WB_PATH.MEDIA_DIRECTORY.'/'.$dir), '/').'/'; $sStartUrl = rtrim(str_replace('\\', '/', WB_URL.MEDIA_DIRECTORY.'/'.$dir), '/').'/';// break if folder not exists or is not readable if (!is_readable($sStartFolder)) { return $sRetval; }// save current working directory $sOldDir = getcwd();// set working directory to $sStartFolder chdir($sStartFolder);// scan for 'years'-folder if (($aYears = glob('*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_MARK))) { // randimize one year if one at least is available $iRandomYear = array_rand($aYears, 1); // scan for 'category'-folders if (($aCategories = glob($aYears[$iRandomYear].'*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_MARK))) { // randimize one category if one at least is available $iRandomCategory = array_rand($aCategories, 1); // scan for images inside selected category if (($aImages = glob($aCategories[$iRandomCategory].'*{png,jpg,gif,PNG,JPG,GIF}', GLOB_NOSORT|GLOB_BRACE))) { // randimize one image if one at least is available $iImage = array_rand($aImages, 1); // build full URL for this image $sRetval = $sStartUrl.$aImages[$iImage]; } } }// restore working directory chdir($sOldDir);// now return the result return $sRetval;
Take this example and modify it to your concrete needs.
1. only a comment... it makes problems and is not recommened, if you start directly in the media folder
I don't start in 'media/'
But - I only can get ONE random picture this way. If I call the same droplet 2 or 3 times, the same random picture will display 2 or 3 times. I guess in order to have 2 or 3 DIFFERENT random pictures, I need to create 3 almost identical droplets, but with different names and then use those...?
Correction - the version from Blackbird does not "always" use the fg-thumbs folder, but it includes them as well.
Quote from: astricia on June 15, 2015, 10:04:35 AMCorrection - the version from Blackbird does not "always" use the fg-thumbs folder, but it includes them as well.Are you sure that you read the docs?
Now if I could figure out how to extract the path from the image URL to display underneath the picture (only the part which describes the year and party) and use it in a link wrapped around the image and path to make it link straight to the foldergallery...