It is currently Thu Mar 28, 2024 10:31 am


All times are UTC




Post new topic Reply to topic  [ 16 posts ] 
Author Message
 Post subject: [PHP] Random Images from Directory / Folder
PostPosted: Wed Mar 29, 2006 2:49 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 11:58 am 
Nice script cipher, I am sure many people will find it useful :)


Top
  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 2:12 pm 
Moderator
Moderator

Joined: Thu Dec 15, 2005 2:59 pm
Posts: 681
Location: England
Nice script, I may use it on my site :)

_________________
Mountain biking forum ;)
Ponds and fishkeeping :D


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 3:59 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 4:44 pm 
Moderator
Moderator

Joined: Thu Dec 15, 2005 2:59 pm
Posts: 681
Location: England
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 :(

_________________
Mountain biking forum ;)
Ponds and fishkeeping :D


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 3:58 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 2:57 pm 
Moderator
Moderator

Joined: Thu Dec 15, 2005 2:59 pm
Posts: 681
Location: England
Shouldn't, but i'd soon get bored. :P

_________________
Mountain biking forum ;)
Ponds and fishkeeping :D


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 01, 2006 7:06 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
Ya. I get bored all the time. Now if you get bored, you will have something to do! :D

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 2:56 am 
Noobie
Noobie

Joined: Tue Jun 27, 2006 2:53 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 4:37 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 6:47 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 7:16 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 5:30 pm 
Noobie
Noobie

Joined: Tue Jun 27, 2006 2:53 am
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 6:13 pm 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 2:19 am 
Noobie
Noobie

Joined: Fri Aug 25, 2006 1:50 am
Posts: 4
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).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 29, 2006 10:22 am 
I have no life!
I have no life!

Joined: Sun Feb 13, 2005 6:12 pm
Posts: 1504
Location: My Office
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.

_________________
Best Regards:
Aaron McGowan (aka: CyberGeek, =cipher=)

CyberGeek's Software Solutions
-- WWW: http://www.cybergeeksoftware.com
-- Tech Line: 1-519-860-4557
-- Email: cybergeek@cybergeeksoftware.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Moderators: fhmagic, KJ, Moderators, Support Team

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
100WebSpace © 2011