100 Web Space
http://forum.100webspace.com/

[PHP] Random Images from Directory / Folder
http://forum.100webspace.com/viewtopic.php?f=13&t=3992
Page 1 of 1

Author:  =cipher= [ Wed Mar 29, 2006 2:49 am ]
Post subject:  [PHP] Random Images from Directory / Folder

Greetings,

Here is a little PHP script I wrote to easily pull any images and randomize them from a directory in your rootpath / webserver.

Instructions:
1. Create a new PHP file called randomimage.php
2. In this file, add the following code:
Code:
<?php
/* Name: Random Image Script
*     Version: 1.0.0
* Created by: CyberGeek
* Email: cybergeek@xtremepc-solutions.com
* Website: http://blog.xtremepc-solutions.com
* For Suppor please contact me at my email address.
*/

/*            INSTRUCTIONS
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* Appear: <?php include "randomimage.php"; ?>
*/

// Change this to the total number of images in the folder
$total = "15";

// Change to the type of files to use eg. .jpg or .gif or .png
$file_type = ".jpg";

// Change to the location of the folder containing the images
$image_folder = "images/random";

// Do NOT edit the the following lines of code below.
$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
?>

3. Save and upload this file to your host.
4. Now, for any PHP page that you want to insert / show the random images, insert this line of code there:
Code:
<?php include "randomimage.php"; ?>

5. Save that file, and view it a couple of times and make sure it works flawlessly. :D

Remeber you can change and edit some of the variables in the script. Actually you will most likely need to edit $total = "15"; that one to have the same number of images as you do in your images/random directory.

Have FUN! :D

Author:  rm249 [ Wed Mar 29, 2006 11:58 am ]
Post subject: 

Nice script cipher, I am sure many people will find it useful :)

Author:  Dale [ Wed Mar 29, 2006 2:12 pm ]
Post subject: 

Nice script, I may use it on my site :)

Author:  =cipher= [ Thu Mar 30, 2006 3:59 am ]
Post subject: 

Oh thanks guys. I got bored last night and couldnt stay connected to my account via FTP. So, I was like ... well... maybe I will do some PHP coding ... custom coding away from coding phpBB stuff.

So, I coded this little script. And use it all you want Dale! :D

Author:  Dale [ Thu Mar 30, 2006 4:44 pm ]
Post subject: 

I was thinking I may use it for a random forum screenshot, and the link of the image takes it to the forum.

I'm not too sure now though, as screenshots will take a while :(

Author:  =cipher= [ Fri Mar 31, 2006 3:58 am ]
Post subject: 

Hey,

Dale - Just resize the screenshot in PS and create a 'thumbnail' type size of a image. Shouldnt me that long to load then. :D

Author:  Dale [ Fri Mar 31, 2006 2:57 pm ]
Post subject: 

Shouldn't, but i'd soon get bored. :P

Author:  =cipher= [ Sat Apr 01, 2006 7:06 am ]
Post subject: 

Ya. I get bored all the time. Now if you get bored, you will have something to do! :D

Author:  bizwizkid [ Tue Jun 27, 2006 2:56 am ]
Post subject: 

I use this great script on a site I did as a volunteer.

My question is I put three instances of the script on the same page so that three images would appear stacked on top of each other.

Is there any way to reduce the amount of times that the images appear in duplicate? I'm up to 35 or so pics in the directory and am imagining that's the only way to reduce the duplication chance - by increasing the amount of pictures. Is this the only way?

Thanks much.

Author:  =cipher= [ Tue Jun 27, 2006 4:37 am ]
Post subject: 

Greetings,

Yes bizwizkid there is a way. This would have to have the code altered a lot. Probably a new script 100%. But, I will work on this for you tonight and have a result as soon as possible. :)

By the way, Thank-You for choosing to use CyberGeek's Software Solutions's free scripts and code! :D

Author:  =cipher= [ Tue Jun 27, 2006 6:47 am ]
Post subject: 

Greetings,

Okay. This is just a simple fix and alterations to the previous code. But I suggest replacing the following PHP code in randomimage.php to:

Code:
<?php
/* Name: Random Image Script - Extension
*       Version: 1.0.0
* Created by: CyberGeek
* Email: cybergeek@cybergeeksoftware.com
* Website: http://cybergeeksoftware.com
* For Suppor please contact me at my email address.
*/

/*            INSTRUCTIONS
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* Appear: <?php include "randomimage.php"; ?>
*/
$total = 7;
$file_type = ".jpg";
$image_folder = "images/";
$start = 1;
$required = 3;
$random = array();
for ($i = 0; $i < $required; $i++)
{
   $tmp = mt_rand($start, $total);
   while (in_array($tmp, $random))
   {
      $tmp = mt_rand($start, $total);
   }
   array_push($random, $tmp);   
   $image_name = $random[$i] . $file_type;
   echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" /><br />";
}
?>


Let me know if that works for you. :D

Author:  =cipher= [ Tue Jun 27, 2006 7:16 am ]
Post subject: 

Greetings,

Sorry. I forgot to add, change the 7 in the following line of code:
Code:
$total = 7;

to how ever many pictues you have in your images/random/ directory.

And I also forgot, change the "images/" in the following line of code:
Code:
$image_folder = "images/";

to "images/random" or what ever directory you have your pictures that you would like to be random. Remember not to include the forward slash before the first directory. :D

Author:  bizwizkid [ Tue Jun 27, 2006 5:30 pm ]
Post subject: 

cipher -

thank you for this work. Now, there's three images on the page and never repeat. Great job.

I added a <br /> at the end (see below) so there would be some separation between the images.

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" /><br /><br />";

I really do appreciate this. I'm doing a volunteer site and the client was concerned because it made the page appear funny with the same pic appearing 2x.

Jerry

Author:  =cipher= [ Tue Jun 27, 2006 6:13 pm ]
Post subject: 

Greetings,

bizwiz, You are most welcome! :D Anywho, please feel free to alter or customize the script as you wish. Although, I do recommend you knowing exactly you are doing. If you need any help, feel free to ask.

Also, this is my job to provide the public with Free and Premuim Software, as I am a Windows and Web Based Developer. I own CyberGeek's Software Solutions. :D

Author:  Ethrel [ Fri Aug 25, 2006 2:19 am ]
Post subject: 

Sorry, I saw this and couldn't resist giving it one less step to do when adding random pictures.

Code:
<?php
/* Name: Random Image Script
*     Version: 1.0.0
* Created by: CyberGeek
* Edited for ease of use by: Ethrel (Deaths_Fury@hotmail.com)
* Email: cybergeek@xtremepc-solutions.com
* Website: http://blog.xtremepc-solutions.com
* For Suppor please contact me at my email address.
*/
 
/*            INSTRUCTIONS
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* Appear: <?php include "randomimage.php"; ?>
*/

// Change to the type of files to use eg. .jpg or .gif or .png
$file_type = ".jpg";

// Change to the location of the folder containing the images
$image_folder = "images/random";

// Do NOT edit the the following lines of code below.
$start = "1";
//open a handle to the directory
$handle = opendir($image_folder);
//intitialize our counter
$total = 0;
//loop through the directory
while (false !== ($file = readdir($handle))) {
//evaluate each entry, removing the . & .. entries
if (is_file($file) && $file !== '.' && $file !== '..') {
$total++;
}
}

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
?>


Just put the images in the random directory and the code will do the rest, upping the file count and choosing from the new amount of files. It may be clumsy, and it is definitely untested, as are all of my mods and small codes, however it should work. Cipher, if you have any problems with this, email me at my email and I will do my best to rectify the problem within 24 hours (I check that email once daily unless I have a reason to check it more).

Author:  =cipher= [ Tue Aug 29, 2006 10:22 am ]
Post subject: 

Greetings,

Mhmm. Never thought of coding it that way. Although, I have not tested your adapted code. But, also. By taking a quick glance at your code it looks like it will only loop threw the random images in the directory. What I mean is it will go threw 01.jpg, then to 02.jpg then to 03.jpg and so on. My code does not do that. My code does something like 01.jpg then to 03.jpg to 02.jpg and so on. Mine is random, yours is by the look of it just loops. I only took a quick glance also at your code.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/