GetDotted Domains

Viewing Thread:
"Random Image display"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Thu 04/10/07 at 16:20
Regular
"www.funrunner.co.uk"
Posts: 19
Does anyone know of any random image scripts that are any good?

I have seen the one provided by freeola in the PHP essentials but I need one where the filenames of the images does not need to be 1,2,3 etc.

Anyone know of any decent ones?
Thu 11/10/07 at 17:46
Moderator
"Are you sure?"
Posts: 5,000
cakey1,
Did my Freeola PHP Random Image demo help???
If you're still having problems don't be afraid to ask...

But if you are all sorted, it would be nice to know.



Search Freeola Chat
Wed 10/10/07 at 12:30
Moderator
"Are you sure?"
Posts: 5,000
cakey1 wrote:
> I put
>
>
>
> into the code where I would have normally put the image source.
> When I say it doesn't work it is just that, no error messages,
> no little image icons in the corner, just white space.
>
> Does anyone know of a site that uses the Freeola code above so I
> can see what I should be getting.
>
> Thanks

Hi cakey1,
I've created a demo page using Freeola PHP script for you:
www.hmmm.ip3.co.uk/freeola_random

Hope this helps...


Search Freeola Chat
Wed 10/10/07 at 11:19
Regular
"Peace Respect Punk"
Posts: 8,069
I made a topic ages ago that detailed how to write a random content (specifically images) script in PHP, and I had a look because I thought I saved it to a text file before posting it on Freeola... and I did. So here it is:



<?php

if ($dir = opendir("random_content")) {
// If random_content folder can be opened

$imgList = array();

while (false !== ($file = readdir($dir))) {
// While there are still files in the directory

if (preg_match('/.jpg$/', $file)) {
// If current file is a .jpg file
array_push($imgList, $file);
}

}

$key = array_rand($imgList, 1);
// Get a random key from the imgList array

$img = $imgList[$key];


// Do what you want with random image/content, eg:
echo('<img src="random_content/' . $img . '" alt="Random Image" />');

}

?>



You obviously have to change the "random_content" bit in the opendir() function to the actual folder you'll be getting the content from, and also alter the img src to the correct folder too.

You can change the preg_match() first parameter so it matches any filetype, or a range of filetypes.

.txt files = preg_match('/.txt$/', $file)
.gif files = preg_match('/.gif$/', $file)
.png files = preg_match('/.png$/', $file)
etc.


Or, to match multiple file types (perhaps you want to match a range of image types), use code like the following:
preg_match('/(\.jpg | \.gif | \.png | \.bmp)$/', $file)


The preg_match() check isn't strictly necessary if you know only one type of file will be in the directory of random things.



If you want random content other than images, for example, a random message, you can use the same code and store each message in a text file, and just change the output, eg:
Instead of echo('<img src="random_content/' . $img . '" alt="Random Image" />');
Use echo('<p>' . $img . </p>);

You may want to change the names of the $imgList & $img variables for clarity too.


Perhaps easier to maintain than seperate text files though is to just hardcode random messages into the PHP, eg:

<?php

$msgList[0] = "Random Text 1";
$msgList[1] = "Random Text 2";
$msgList[2] = "Random Text 3";

$key = array_rand($msgList, 1);

$msg = $msgList[$key];

echo('<p>' . $msg . '</p>');

?>



Finally, if you want to obtain multiple random... things. The second parameter of array_rand() has always been 1 thus far. This specifies how many keys you want from the specified array. If it's set to 1 you get a single random key back. Otherwise you get an array of keys back.
So, rather than accessing your random item by something of the form:
$randomThing = $randomList[$key];

Now you do it by something like:

$randomThing1 = $randomList[$keys[0]];
$randomThing2 = $randomList[$keys[1]];

etc. up to however many elements you had.

A simpler way of doing this is with a for loop, eg.

for ($i=0; $i<sizeof($keys); $i++) {
// For each element of $keys

$random[$i] = $randomList[$keys[$i]];

}

Which puts each random element into an array $random for you to access as you like.


A final note about the array_rand() function... Make sure your random array contains greater than or equal to the number of random elements you are requesting otherwise array_rand() won't work!


Hopefully that will be useful to someone!
Tue 09/10/07 at 23:04
Regular
Posts: 4
I put



into the code where I would have normally put the image source.
When I say it doesn't work it is just that, no error messages, no little image icons in the corner, just white space.

Does anyone know of a site that uses the Freeola code above so I can see what I should be getting.

Thanks
Fri 05/10/07 at 15:38
Staff Moderator
"Aargh! Broken..."
Posts: 1,408
:) It's more fun when the code works though!
Fri 05/10/07 at 13:05
Regular
"Devil in disguise"
Posts: 3,151
Eccles wrote:
> Try putting your PHP in proper PHP tags, <?php code
> ;?>
. You should always avoid shorthand tags, it
> doesn't save time anyway! The web servers are setup to parse
> code in full PHP tags as PHP <? ?> can just
> be interpreted as XML or other language tags.

Nag nag nag.....
its people like you that take the fun out of coding. :p
Fri 05/10/07 at 10:07
Staff Moderator
"Aargh! Broken..."
Posts: 1,408
Try putting your PHP in proper PHP tags, <?php code ;?>. You should always avoid shorthand tags, it doesn't save time anyway! The web servers are setup to parse code in full PHP tags as PHP <? ?> can just be interpreted as XML or other language tags.
Thu 04/10/07 at 21:29
Regular
"Devil in disguise"
Posts: 3,151
Can you elaborate on "it doesnt work"? Some kind of description of what it does in its broken state would help.
Thu 04/10/07 at 21:14
Regular
Posts: 4
Sorry to change the thread slightly but I canīt get the PHP in Freeola to work for Random Image Generator. I have done all requested - PHP in HTDocs,images folder, 1,2,3etc and it doesnīt work. Any suggestions please?
Thu 04/10/07 at 19:39
Regular
"Devil in disguise"
Posts: 3,151
I think this will work (not really tested it)...


<?php

$imageDirectory='images';

if (substr($imageDirectory,-1)!='/') {
$imageDirectory.='/';
}

$images = array();
$cnt = -1;

$dir = opendir($imageDirectory);

while (false !== ($img = readdir($dir))) {
if (preg_match ('/^(.+)\.(jpg|gif|png|jpeg)$/i', $img)) {
$images[] = $img;
$cnt++;
}
}

closedir($dir);

echo '<img src="'.$imageDirectory. $images[mt_rand(0,$cnt)].'" alt="Random Image"/>';

?>


Save it as randomImage.php. Change the $imageDirectory variable with the path to your image directory. And where ever you want the random image to appear on your page, just do
<?php include('randomImage.php') ?>
(ie you use it exactly as you would the freeola version)

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Thank you very much for your help!
Top service for free - excellent - thank you very much for your help.
First Class!
I feel that your service on this occasion was absolutely first class - a model of excellence. After this, I hope to stay with Freeola for a long time!

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.