PHPBuilder - Dynamic Image Rotation



RSS Twitter
Snippets Graphics

Dynamic Image Rotation

by: Jarrod Major
|
June 4, 2001

Version: 1.0

Type: Full Script

Category: Graphics

License: Other

Description: This code radomly selects and image from a directory and with the aid of META refresh rotates images on a page in a slideshow fashion. The code will function fine without the META refresh enabling a different image to be viewed upon page refresh. The script generates a valid IMG tag including graphic dimensions. Will accept either GIF or JPEG formats. This code should be compliant for both PHP 3.x and PHP 4.x



<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>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Jarrod Major

Comment:



Comment:

(Maximum characters: 1200). You have characters left.