Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to: random images
#1
Question 
Hello,

I was wondering how to make a page consisting 3 images, where every image is part of a group and randomly shown.

So for example:

7 landscape photo's: 1 is shown randomly every refresh
12 portrait photo's: 1 is shown (next to the other) randomly every refresh
9 macro photo's: 1 is shown (next to other) randomly every refresh

I know there is the random content plugin from Mikey, but it's a bit outdated. For example, it doesn't support the browsing for files in it's editor and also doesn't support combination with other plugins like custom fields.

I was thinking of maybe using i18N search as an alternative, but don't know exactly how to configure it to show random content.

What might also work is using a photo gallery and show only one image per photo gallery, so 3 photo galleries per page. This didn't work with the I18N gallery since it doesn't corectly show every image gallery seperatly (maybe I'm doing something wrong).
Reply
#2
Use: DynPages

with this snippet:

<?php
global $args;
$dir = 'data/uploads/'.$args[0];
$files1 = scandir($dir);
$max = count($files1)-1;
$a=get_site_url(false);
$p=explode("/",$a);
$dir="/".$p[3]."/".$dir;
$a=rand(2,$max);
echo "<img src='".$dir."".$files1[$a]."' width='100%'>"
?>

Call it with: {% rndpic slider/3/ %} assumes of course that you name the component "rndpic" and your pic-folder "slider/3/"

You can put your images in a folder. That's all.

If you need a shadowbox you can add this very easy.

The snippets read any picture in a given folder, put them in an array and select a random pic for display.
Reply
#3
Thanks, I will try it out.

What's also good is that it's based on files instead of pages.
Reply
#4
Yes, it is... I uses only common techniques and no additional plugins. I always try to reduce the amount of used plugins on a few. For simple tasks I try own solutions (as this one)

Of cource with a simple modification you can randomly call some pages in the same way. But I have never thought about it ;-) until now... Would be nice If I think I could create pages (inside GS) with anything you want... and than a random display... perhaps in the head or in footer...

Would be nice.
Reply
#5
(2014-12-12, 19:24:30)Lars Wrote: Use: DynPages

with this snippet:

<?php
global $args;
$dir = 'data/uploads/'.$args[0];
$files1 = scandir($dir);
$max = count($files1)-1;
$a=get_site_url(false);
$p=explode("/",$a);
$dir="/".$p[3]."/".$dir;
$a=rand(2,$max);
echo "<img src='".$dir."".$files1[$a]."' width='100%'>"
?>

Call it with: {% rndpic slider/3/ %} assumes of course that you name the component "rndpic" and your pic-folder "slider/3/"

You can put your images in a folder. That's all.

If you need a shadowbox you can add this very easy.

The snippets read any picture in a given folder, put them in an array and select a random pic for display.
It does not work entirely. the image url shows up like this: http://data/uploads/raimg1/wm12.jpg (folder name of first group is raimg1)
So basicly the site url is not present in the url.
Reply
#6
There is a plugin in extend for random content.
http://get-simple.info/extend/plugin/ran...ntent/303/

For random images from a folder I have used this ancient script in the folder I want to get the image from. (if you check the source below it's from 2003 but still there)

With this saved in the folder along with the images as randompic.php
the usage is then
Quote:img src="/wherever the pics are/randompic.php"


The script
PHP Code:
<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:

http://photomatt.net/scripts/randomimage

*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder '';

// Space seperated list of extensions, you probably won't have to change this.
$exts 'jpg jpeg png gif';

$files = array(); $i = -1// Initialize some variables
if ('' == $folder$folder './';

$handle opendir($folder);
$exts explode(' '$exts);
while (
false !== ($file readdir($handle))) {
foreach(
$exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i'$file$test)) { // faster than ereg, case insensitive
$files[] = $file// it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand mt_rand(0$i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>
Reply
#7
I think i18n gallery supports this, then you can manage your images not just grab them from uploads.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#8
This should work...

<?php
global $args;
$dir1=$args[0];
$dir=GSDATAUPLOADPATH.$args[0];
$files1 = scandir($dir);
$a=rand(2,count($files1)-1);
echo "<img src='".get_site_url(false)."data/uploads/".$dir1."/".$files1[$a]."' width='100%'>"
?>
Reply
#9
(2014-12-13, 02:01:07)shawn_a Wrote: I think i18n gallery supports this, then you can manage your images not just grab them from uploads.

I don't think so. i18N Gallery is for showing galleries, does not work with single images, without linked gallery functionality. At least I searched for it and didn't find.
Reply
#10
I am pretty sure it does support callback or callout functions that allow random selection of a galleries images from a gallery. perhaps i am thinking of thumbs

http://get-simple.info/forums/showthread...9#pid44559

But I also remember reading something else, either way using your own code with galleries is still easier than pulling files from filesystem, as you can setup galleries and pull random from those instead.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#11
(2014-12-15, 04:42:28)shawn_a Wrote: I am pretty sure it does support callback or callout functions that allow random selection of a galleries images from a gallery. perhaps i am thinking of thumbs

http://get-simple.info/forums/showthread...9#pid44559

But I also remember reading something else, either way using your own code with galleries is still easier than pulling files from filesystem, as you can setup galleries and pull random from those instead.

It links to a gallery, so you can post one thumb in any size which links to a gallery, or you can post a thumb which links to a gallery via fancybox-popup. I don't want an image link, I only want a random image. But correct me if I'm wrong, I searched for this info in the thread and Mvlcek's own info-pages, but could not find what I want.

(2014-12-13, 02:44:46)Lars Wrote: This should work...

<?php
global $args;
$dir1=$args[0];
$dir=GSDATAUPLOADPATH.$args[0];
$files1 = scandir($dir);
$a=rand(2,count($files1)-1);
echo "<img src='".get_site_url(false)."data/uploads/".$dir1."/".$files1[$a]."' width='100%'>"
?>
Thanks, that works.
Reply




Users browsing this thread: 1 Guest(s)