Forum Moderators: coopster

Message Too Old, No Replies

PHP Image randomizer

         

cgiupload

3:59 am on Feb 28, 2008 (gmt 0)

10+ Year Member



Hi, I am trying to display the images in a directory randomly but when I post the code on the site, it's just a broken image. I believe this is an issue with my PHP settings. Does anyone know how to fix this? Thank you!

# Copy/paste the code above
# Name the file random.php
# Place in the same directory in which your images reside
# Reference random.php in your src. For instance:
<img src="image_path/random.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);
}
}
?>

bkeep

6:04 am on Feb 28, 2008 (gmt 0)

10+ Year Member



I am not sure exactly sure what you are trying to do but if the images are already in existence in the directory you can just grab them. Here are a couple of functions I use for the exact same thing without using gd.

$path_to_images = "images/randomize/"; // path to your images
$default_img = "images/default.jpg"; // image to display if directory listing fails

function getRandomImage($path, $img) {
if ( $list = getImagesList($path) ) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($list);
$img = $list[$num];
}
return $path . $img;
}

function getImagesList($path) {
$ctr = 0;
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// can add checks for other image file types here
if ( preg_match("/(\.gif¦\.jpg)$/", $img_file) ) {
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
}
return false;
}

$img_src = getRandomImage($path_to_images, $default_img);

If for some reason you have to use gd to create the images that is a whole other story but for brevity I will post that if this doesn't work for you

Best Regards,
Brandon

surrealillusions

9:56 am on Feb 28, 2008 (gmt 0)

10+ Year Member



You can use a simple array and use the random function already built into php to do a simple random image

:)