random image

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
If all the images are in a single directory, you should just be able to use a for loop to read them all into an array, and then use the random number function in PHP to pick one, then display it. 'Course, if you're too lazy to write it out on your own, it would probably look something like this (I didn't test it, so use at your own risk):

PHP:
<?php
    $dir = opendir('imagedir');
    for ($i = 0; $pointer = readdir($dir);) {
        if ($pointer != '.' || $pointer != '..') {
            $images[$i] = 'imagedir/'.$pointer;
            $i++;
        }
    }
    echo '<img src='.$images[rand(0,count($images))].' alt="" />';
?>
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
If all the images are in a single directory, you should just be able to use a for loop to read them all into an array, and then use the random number function in PHP to pick one, then display it. 'Course, if you're too lazy to write it out on your own, it would probably look something like this (I didn't test it, so use at your own risk):

PHP:
<?php
    $dir = opendir('imagedir');
    for ($i = 0; $pointer = readdir($dir);) {
        if ($pointer != '.' || $pointer != '..') {
            $images[$i] = 'imagedir/'.$pointer;
            $i++;
        }
    }
    echo '<img src='.$images[rand(0,count($images))].' alt="" />';
?>

Supposing your images are in "imagedir", code has to be inserted instead of the <img> tag of the random image. This script does NOT output an image, it just gives you an <img> tag with a random src attribute.
 

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
Supposing your images are in "imagedir", code has to be inserted instead of the <img> tag of the random image. This script does NOT output an image, it just gives you an <img> tag with a random src attribute.
I was under the assumption that the OP was looking to display a random image after glancing through the link. This is exactly what it does.
 
Last edited:

massimo

New Member
Messages
42
Reaction score
0
Points
0
thanks for replying guys.

there's no problem with the code, it works fine. just sits in an image folder where you want the random image to be taken from. calling it is no problem either. I do it like this:
Code:
<img src="/images/random_image.php"/>

problems are happening in IE6 because I have the img tag getting included onto the page using a php require, like this:
Code:
<?php require("header.php"); ?>
and my navigation links included too
Code:
<?php require("navigation.php"); ?>
for some reason the image doesn't change when using the nav links.

I've been testing it like crazy and it's very temperamental. if the img tag is in the included nav.php file then it seems to work. I've even tried using the random image as a css background image, which also doesn't work.

here's the random image php code with most of the comments removed:
Code:
<?php


	$folder = '.';


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

// You don't need to edit anything after this point.


// --------------------- END CONFIGURATION -----------------------

$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);
	}
}

?>

any ideas would be most appreciated
 
Top