Help - Search - Members - Calendar
Full Version: Random Image Script
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
EMOruffino
I just implemented this on my site at:

http://emoruffino.com and thought i'd share it...

Author: BRANDON HEYER

1. Make a php file named: basic_image_randomizer.php

2. put this into the file:

CODE
<?

$GLB_IMAGE;

function imageRandom($dir, $types) {

   $images = array();
   $new = array();

    global $GLB_IMAGE;

   if ( !empty($types) ) {
      $types = explode(",", $types);
      foreach ( $types as $t ) {
         $glob = $dir . '*.' . $t;
         $new = glob( $glob );
         if ( !empty( $new ) ) {
            $images = (array)$new + (array)$images;
         }
      }
   } else {
      $images = glob( $dir . '*.*' );
   }

   $key = array_rand($images);
   $image = $images[$key];
   $GLB_IMAGE = $image;

   echo $image;
}

?>

3. Add this to the top of your page (anywhere really)
CODE
<? include("basic_image_randomizer.php"); ?>


4. Enter this code into your page and your done.
CODE
<img src='/<? imageRandom('random/', 'png,jpg'); ?>' />


Were it says random you can put in any directory that has your images. And you can also make it just PNG or JPG or GIF by adding it into coding.
IamShipon1988
Ah that is pretty nice, well here's one a friend of mine gave me once. The authors goes by the webname of Saint_Michael.

QUOTE
Name: Sig Rotator Tutorial
Author: Saint-Michael
Summary: Today I am gonig to teach you how to make a sig rotator.


QUOTE
step 1: create a index.php file with in your images folder. and then paste this code into it, then save now open the index file in your browser and you should see an image. if not pm or pst here and i will help you out.


CODE
<?php

if ($dir = opendir("."))
{
$list = buildimagearray($dir);
displayrandomimage($list);
}

// This function reads all the files in the current directory and adds all image files to the array $list[]
function buildimagearray($dir)
{
while (false !== ($file = readdir($dir)))
{
if (!is_dir($file) && getimagesize($file))
{
$list[] = $file;
}
}
return $list;
}

// This function selects a random image, determines the mime type, opens the file for reading,
// and then outputs the image
function displayrandomimage($list)
{
srand ((double) microtime() * 10000000);
$sig = array_rand ($list);

$size = getimagesize ($list[$sig]);
$fp = fopen($list[$sig], "rb");

if ($size && $fp)
{
header("Content-type: {$size['mime']}");
fpassthru($fp);
exit;
}
}
?>



Step 2:change your images folder like this images>images.png in order for the script to work properly

NOTEi had to make another copy of my gift folder in order for this to work due to the fact that those who had gift sigs from me could still be seen just as a image. but to get it rotating i had to change the folder into a png format to get it to work.


Step3: then paste this code below to get it working. mind you put our own url in their not mine


CODE
For Web-sites
<img src="http://www.gfxtrap.com/sig.png" alt="Random Image">
For Forum
[IMG]http://www.gfxtrap.com/sig.png[/IMG]


Ed
I'm failing to see the point of the global, also, why would you be supplying types, wouldn't the directory in a typical situation only contain the random images you wanted?

I threw together a more condensed version:

CODE
<?php
function randomImage($dir)
{
   $dir = (substr($dir, -1) != DIRECTORY_SEPARATOR)
   ? $dir . DIRECTORY_SEPARATOR
   : $dir;

   if(file_exists($dir))
   {
      $images = glob($dir . '*{.gif,.GIF,.jpg,.JPG,.jpeg,.JPEG,.png,.PNG}', GLOB_BRACE);

      return $images[array_rand($images)];
   }
}
?>

<img src="<?php echo randomImage('./images'); ?>" />
Brandon
QUOTE(Bread @ Dec 31 2007, 04:38 PM) *
I'm failing to see the point of the global, also, why would you be supplying types, wouldn't the directory in a typical situation only contain the random images you wanted?

I threw together a more condensed version:

CODE
<?php
function randomImage($dir)
{
   $dir = (substr($dir, -1) != DIRECTORY_SEPARATOR)
   ? $dir . DIRECTORY_SEPARATOR
   : $dir;

   if(file_exists($dir))
   {
      $images = glob($dir . '*{.gif,.GIF,.jpg,.JPG,.jpeg,.JPEG,.png,.PNG}', GLOB_BRACE);

      return $images[array_rand($images)];
   }
}
?>

<img src="<?php echo randomImage('./images'); ?>" />

Umm I like this is there away you could do it so its a certin size ? and when you click the image it take you 2 the full size ?
EMOruffino
QUOTE(Bread @ Dec 31 2007, 10:38 AM) *
I'm failing to see the point of the global, also, why would you be supplying types, wouldn't the directory in a typical situation only contain the random images you wanted?

I threw together a more condensed version:

CODE
<?php
function randomImage($dir)
{
   $dir = (substr($dir, -1) != DIRECTORY_SEPARATOR)
   ? $dir . DIRECTORY_SEPARATOR
   : $dir;

   if(file_exists($dir))
   {
      $images = glob($dir . '*{.gif,.GIF,.jpg,.JPG,.jpeg,.JPEG,.png,.PNG}', GLOB_BRACE);

      return $images[array_rand($images)];
   }
}
?>

<img src="<?php echo randomImage('./images'); ?>" />

maybe your site has .gif images and your pictures are .pngs (idk)
Alex
Crown, yeah, it is. In fact, there are three ways I can think of off the top of my head:
- Seperate thumbs/ and images/ directories, output thumbs/ in the <img> and have a link around it pointing to images/
- Use GD to produce thumbnails server-side
- Just define dimensions in the <img> tag.

EMO, do try to make these tutorials as widely compatible as possible. What I'm getting at is that you really should avoid the use of short-tags (<?) as opposed to <?php for anything which is going to be showcased publicly.
Brandon
QUOTE(Alex @ Dec 31 2007, 05:29 PM) *
Crown, yeah, it is. In fact, there are three ways I can think of off the top of my head:
- Seperate thumbs/ and images/ directories, output thumbs/ in the <img> and have a link around it pointing to images/
- Use GD to produce thumbnails server-side
- Just define dimensions in the <img> tag.

EMO, do try to make these tutorials as widely compatible as possible. What I'm getting at is that you really should avoid the use of short-tags (<?) as opposed to <?php for anything which is going to be showcased publicly.

Ok Thanks Alex smile.gif
EMOruffino
QUOTE(Alex @ Dec 31 2007, 11:29 AM) *
EMO, do try to make these tutorials as widely compatible as possible.


yes i do, tho im still learning php so ill start using that from now on... thanx
xbile
try this dude...
http://xbile.clanteam.com/stuff
doughnut
um. grrr>?

lol hey to all i see we are all working hard on get'n this to work.
o ya im moved in to my new apt and got the net back up so woot for me
IamShipon1988
LOL welcome back bud...I was wondering what happened to you...darn that murder attempt failed, lol.
doughnut
OMG some one post the full code...
has any one got this to work?
i lost???!!!... wacko.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.