iollo
06-16-2006, 11:27 AM
You can use this code snippet to rotate your images on your site. You can use it let's say for rotating images beside your adsense ads :D
<?php
$path_to_images = "images/"; // path to your images
$default_img = "turtle.gif"; // 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;
}
?>
You can see sample on http://free-css-templates.com
<?php
$path_to_images = "images/"; // path to your images
$default_img = "turtle.gif"; // 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;
}
?>
You can see sample on http://free-css-templates.com