Hello,

I am trying to find a script, or write one, that will randomly display a different image from a folder, similar to this script below, only i need the script to refresh only the images and not do a meta refresh, is this possible? I know javascript can do it, but I would like to stay away from client side code as much as possible.

Thanks

<script language="php">
// =============================================================
// Script:    Image Rotation Script
//            With Adjustable Timing and Unlimited Images
//            Utilizes META REFRESH
// Function:  Displays random images continuously in a slideshow
//            presentation format
// PHP ver:   3.x, 4.x
// Author:    Jarrod Major
// Email:     jarrod@nucleus.com
// Copyright: (C)2001 Nucleus Information Service Inc.
// Licence:   Free, drop me a line letting me know where you
//            used the script. Please keep this header attached.
// =============================================================
// Disclaimer:
// Use of this code is at the programmer's own risk. Damage
// to any property as a result of the script's use is the sole
// responsibility of the programmer, no blame shall be laid on
// the author of this script or his employer.
// =============================================================

function rotate_images()
{
	// open directory, images must be in a separate directory
	$dir_pointer = dir( "directory_name" );

// make an array of filenames
while( $entryName = $dir_pointer->read() )
{
	// don't include parent/current directory listing /. /..
	if( !ereg( "^\.", $entryName ) )
		$file_array[] = $entryName;

		// could be done this way to simplify some code below
		# $file_array[] = "directory_name/$entryName";
}

// close directory
$dir_pointer->close();

$count_array = count( $file_array ) - 1;

// intialize random seed
srand( time() );

// get a random index number to pull from array
$random_no = rand( 0, $count_array );

// make a file pointer
$file_name = "directory_name/$file_array[$random_no]";

// or if directory added to array element
#$file_name = $file_array[$random_no];

// get image dimensions
$image_size = getimagesize( $file_name );

// create dynamic image tag
$out = sprintf( '<img src="%s" %s alt="image." />%s', $file_name, $image_size[3], "\n" );

return $out;
}
</script>
<html>
<head>
<!-- to adjust timing change 'content' value below -->
<meta http-equiv="Refresh" content="3" />
<title>Test Image shuffler</title>
</head>
<body>
<?php print rotate_images(); ?>
</body>
</html>

    PHP can't randomly change the image. You have to either use Javascript to change the src attribute of the <img> tag; or, use a meta refresh to change the entire page and display it with a new image.

    One thing you could do is to put the image in an iFrame and then refresh the iFrame every so often using a meta-refresh in the iFrame and you've got a refreshing image. But, iFrames are old tech, and javascript would be much better.

      Yeah, I figured as much after googling for an hour, but I came up with something easy that works.. animated gifs, i get rotating images, and it completes the effect, only problem is adding more images to the rotation..

      I am working on a website, and wanted to make it so images could just be uploaded, and would automatically get set into the rotation.. which.. may work with the animated's..

      take the php script that loads random images.. use that to pull the gifs, which are animated, and every time a user browses the site, they get a new set of animated gifs...

      everytime i wanted to add more images, I would just create them into an animated gif and upload...

      andyways.. bpat1434 thank you for the reply.

        Write a Reply...