• Welcome to ForumKorner!
    Join today and become a part of the community.

How to Make a Rotating Image

Mxlitary

Active Member
Reputation
0
This PHP script will randomly select an image file from a folder of images on your webserver. You can then link to it as you would any standard image file and you'll see a random image each time you reload.

When you want to add or remove images from the rotation-pool, just add or remove them from the image rotation folder. There is no need to edit the original file every time you add a new image.

____________________________


Instructions

1. Copy and paste the code below into a txt file. Rename the file to be 'index.php'.

2. Create a folder on your webserver called 'rotate.jpeg'.

3. Upload the file (index.php) to your rotate.jpeg folder.

4. Upload all the images you want to be in the cycle into that same folder.

5. Link to the file as you would any normal image file, like this:

<img src="http://example.com/rotate.jpeg">
or on a web forum
{img}http://example.com/rotate.jpeg{/img}

You can also specify the image to display like this:

<img src="http://example.com/rotate.php?img=gorilla.jpg">

This would specify that an image named "gorilla.jpg" located
in the image-rotation folder should be displayed.

That's all, enjoy.

____________________________


Code

PHP:
<?php


	$folder = '.';


    $extList = array();
	$extList['gif'] = 'image/gif';
	$extList['jpg'] = 'image/jpeg';
	$extList['jpeg'] = 'image/jpeg';
	$extList['png'] = 'image/png';
	

$img = null;

if (substr($folder,-1) != '/') {
	$folder = $folder.'/';
}

if (isset($_GET['img'])) {
	$imageInfo = pathinfo($_GET['img']);
	if (
	    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
		$img = $folder.$imageInfo['basename'];
	}
} else {
	$fileList = array();
	$handle = opendir($folder);
	while ( false !== ( $file = readdir($handle) ) ) {
		$file_info = pathinfo($file);
		if (
		    isset( $extList[ strtolower( $file_info['extension'] ) ] )
		) {
			$fileList[] = $file;
		}
	}
	closedir($handle);

	if (count($fileList) > 0) {
		$imageNumber = time() % count($fileList);
		$img = $folder.$fileList[$imageNumber];
	}
}

if ($img!=null) {
	$imageInfo = pathinfo($img);
	$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
	header ($contentType);
	readfile($img);
} else {
	if ( function_exists('imagecreate') ) {
		header ("Content-type: image/png");
		$im = @imagecreate (100, 100)
		    or die ("Cannot initialize new GD image stream");
		$background_color = imagecolorallocate ($im, 255, 255, 255);
		$text_color = imagecolorallocate ($im, 0,0,0);
		imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
		imagepng ($im);
		imagedestroy($im);
	}
}

?>
 

Marijuana

Onyx user!
Reputation
0
Thanks i got it

rotate.jpeg
 
Top