WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: hansis100 on June 19, 2013, 08:13:08 AM

Title: Random Image
Post by: hansis100 on June 19, 2013, 08:13:08 AM
Hello!

I have tested a lot of droplets for random Images, but nothing found.
I need a droplet for random images that can include subfolders.
example
Code: [Select]
/media/photos/
 |-- /photos1/
         |-- photo1.jpg
         |-- photo2.jpg
 |-- /photos2/
         |-- photo3.jpg
         |-- photo4.jpg

anyone knows a droplet?

thx.
hansi

Title: Re: Random Image
Post by: hansis100 on June 19, 2013, 11:14:06 AM
I have one on my HP.

Is it possible to post the code?
Title: Re: Random Image
Post by: hansis100 on June 19, 2013, 03:12:12 PM
Danke!

hansi
Title: Re: Random Image
Post by: astricia on June 12, 2015, 01:33:46 PM
Leider wurden hier offensichtlich Beiträge entfernt. Ich habe gerade den gleichen Wunsch - ein Droplet "Random Image", das auch Subfolder durchsucht. Kann mir da jemand helfen?

LG,
Astricia

Title: Re: Random Image
Post by: astricia on June 12, 2015, 01:50:42 PM
Sorry, just realized this is the English part of the forum. I currently have the same problem - I need a droplet "Random Image" which also looks through the subfolders and picks any picture from any of the many subfolders.

Obviously there was a solution in this thread, but it has been removed. Can somebody help me please?

Thanks,
Astricia
Title: Re: Random Image
Post by: Gast on June 13, 2015, 12:16:04 PM
here the droplet called "RandomPicture", its a Part of the actual WB-2.8.3-SP4-Download-Package incl SP4, all droplets in this package are repaired and testit. Its not possible with this droplet, to show random pics from multiple folders

german: Hier das aktuelle Droplet "RandomPicture" aus dem aktuellem WB-2.8.3-SP4-Paket. Alle Droplets in diesem Paket  wurden repariert und getestet. Es ist mit diesem Droplet nicht möglich, Bilder aus verschiedenen Ordnern zu zeigen

Dropletname: RandomImage

Beschreibung / Description: Get a random image from a folder in the MEDIA folder.

Droplet-Aufruf / Usage:
[[RandomImage]] - show a random image in media-directory
[[RandomImage?dir=bakery/images/item2]] - show a random image in media-subdirectory

Code
Code: [Select]
$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%" />';


Title: Re: Random Image
Post by: astricia on June 13, 2015, 12:50:53 PM
Of course I know the droplet "RandomImage" and I know that it is not possible to show random pics from multiple folders.

But reading the above thread, I figured that somebody (BlackBird?) had found a solution to this problem and had posted it at the time, but later deleted it for whichever reason. That's why I'm asking ...

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...

Anyone has an idea?

Thanks,
Astricia
Title: Re: Random Image
Post by: Gast on June 13, 2015, 01:39:41 PM
google is your friend  ;-)

here the Droplet RandomOrLatest (http://wbaddons.webbird.de/?do=item&item=10) from Bianka aka BlackBird

but dont forget: if you have a lot of picture folders, the droplet scan a lot of folders

Title: Re: Random Image
Post by: DarkViper on June 14, 2015, 11:50:54 AM
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...
30.000 pics is heavy....  if you think straight.. ;)
But it's possible to scan all of these images very fast. the solution for, you gave it as you described how the pics are organises.
It's not needed to scan all of these 30.000 pics. Simply step by step scan for folders and choose one of it. In the last folder only you must scan for all files located in this folder and take one of them. This kind of scanning is very fast.

Take this example and modify it to your concrete needs.
Code: [Select]
<?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($aYears1);
        
// 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($aCategories1);
            
// 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($aImages1);
                
// build full URL for this image
                
$sRetval $sStartUrl.$aImages[$iImage];
            }
        }
    }
// restore working directory
    
chdir($sOldDir);
// now return the result
    
return $sRetval;

Edit: corrected retval of array_rand()
( no guaranty, no warranty... test it..)

Manuela
Title: Re: Random Image
Post by: Gast on June 14, 2015, 02:24:49 PM
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
2. the return of array_rand is only a key, so it needs a little modification

Title: Re: Random Image
Post by: DarkViper on June 14, 2015, 03:41:31 PM
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/' ;-)
I use WB_PATH.MEDIA_DIRECTORY.'/'.$dir
So it's possible to use any subdir or subsubdir of media to start.
The only mandatory is, that the start directory must be somewhere behind media/.

Manuela
Title: Re: Random Image
Post by: Gast on June 14, 2015, 06:16:57 PM
I don't start in 'media/' ;-)

not you, but i (for a test - Droplet-Call without Parameters)
Title: Re: Random Image
Post by: astricia on June 15, 2015, 09:56:27 AM
Thanks both of you!!! I tried both solutions - the droplet from BlackBird and the solution from DarkViper. Both of them work, and are quite fast. Interestingly enough the solution from BlackBird always uses the fg-thumbs folders (and thus produces thumbnails), while DarkViper's version uses the bigger versions of the pictures.

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...?

Also, I need to figure out how to extract part of the URL to display as text underneath those pictures, so that it indicates from which year and which party in that year these pictures are from. And possibly even link the image to the foldergallery of that album...

Thanks,
Astricia
Title: Re: Random Image
Post by: astricia on June 15, 2015, 10:04:35 AM
Correction - the version from Blackbird does not "always" use the fg-thumbs folder, but it includes them as well. But it restricts the height and width of the pictures, so if it takes one of the bigger versions, which are not square formatted, then it will stretch or compress it... which makes it look funny.

So I suppose I prefer the version from Manuela...
Title: Re: Random Image
Post by: DarkViper on June 15, 2015, 10:47:29 AM
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...?

This is result of the kind what droplets are and how it works. General droplets are a simple Search&Replace. All identical droplet tags will be replaced with the result of the first founded at once.
But don't worry... you can get multiply, different images anyway. ;-)
All you have to do is modify the droplet tags a little bit.

[[RandomImageFromTree?dir=foldergalery/archive/&x=1]]
[[RandomImageFromTree?dir=foldergalery/archive/&x=2]]
[[RandomImageFromTree?dir=foldergalery/archive/&x=3]]

'x' is a unused argument.. it has no effect to the droplet itself.. but It makes each droplet tag unique. So each of them will be executed and you get different results of each.
A little trick with a good effect. ;-)

Manuela
Title: Re: Random Image
Post by: BlackBird on June 15, 2015, 10:54:38 AM
Correction - 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?
Title: Re: Random Image
Post by: astricia on June 15, 2015, 11:36:38 AM
Correction - 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?

Oops - no, admittedly, I did not read to the end. So, yes, it works now to display only images from the thumbs-folders.

Also thank you for the trick with different images, Manuela. It works the same way with the droplet from BlackBird.

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...
Title: Re: Random Image
Post by: astricia on June 15, 2015, 04:27:26 PM
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...

Figured it out... woohoo! :D Actually just some basic PHP string functions, but tricky for me as I don't do this very often.

Thanks everyone for your help!