Web Hosting Talk







View Full Version : good random image script?


ThugRacing
04-21-2004, 08:24 PM
whats any easy to use random image script that can easly be set up? and does not require you to rename all the jpg's

P10n33R
04-21-2004, 08:48 PM
<?php
/*
-------------------------------------------------------------
|MD Random Image Generator |
|Version 1.0.0 |
|This program is Copyright (c) Matthew Dingley 2003 |
|For information on how to install or for basic licence |
|information, view below |
| |
| |
|To install, just enter in the directory name that you store|
|the images in to the variable below named $dir. Upload any |
|images to that folder to have them randomly displayed. |
| |
|You can also edit the variable $pattern if you know what |
|you are doing. |
| |
|To display the random image on your web page, you can |
|either copy and paste all of this code into the page where |
|you want it or you can include it by putting in the |
|following code into the page where you want the image: |
|<?php include "yourfile.php"; ?> |
-------------------------------------------------------------
*/
$dir=opendir("/home/you/public_html/folder/");
//This is the directory route to the folder
$directory="";
//This is a relative link to the directory if it is not in the same directory as the file you are displaying the images on

$pattern="\.(gif|jpg|jpeg|png|bmp|swf)$";
if(!$dir)
{
die("Failed to read directory");
}
$s=readdir($dir);
$count="0";
$image;
while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);

//Spit it out
$limit=count($image);
$limit--;
$randNum=rand(0,$limit);
$size=getimagesize("$directory$image[$randNum]");
echo "<br><img src=\"$directory$image[$randNum]\" $size[3]>";
?>


This is a script from
www.matthewdingley.co.uk
please visit there and download the file from them.

Also I have not used this and dirst link http://www.matthewdingley.co.uk/programs/randimage/index.php

Loon
04-22-2004, 08:23 AM
this works well:


<?php
/************************************************\
* Random Signature
* **********************************************
* File Name : sig.php
* Author : devfreak.om
* Purpose : Using PHP to read a random image
* Date : Monday, December 8th, 2003
\************************************************/

//--------------------------------------------------
// Change this to the directory where your images
// are uploaded (image directory must be a
// subdirectory of this file) ex. $img_path = "/img/";
$img_path = "";
//--------------------------------------------------
// Get the Absolute path to this folder
$abs_path = explode($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_FILENAME']);
$abs_path = $abs_path[0];
if ($img_path == "") {
$img_path = "/img/";
}
$path = $abs_path.$img_path; // Combining two paths
$total = count(glob($path.'*')); // Get number of files (images) in path
$image_id = rand(1, $total); // Get random number
$i_counter = 1; // Set counter to 1
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != ".htaccess" && $file != "." && $file != "..") {
if ($i_counter == $image_id) {
$fext = explode('.', $file);
$ext = strtolower($fext[1]); // get files extension
$file = $path.$file;
$download_size = filesize($file);
if ($ext == ".jpeg") { $ext = ".jpg"; }
// Switch determines correct header to send
switch ($ext) {
case "jpg":
header('Content-Type:image/jpeg');
break;
case "gif":
header('Content-Type:image/gif');
break;
case "bmp":
header('Content-Type:image/bmp');
break;
default:
header('Content-Type:image/png');
break;
}
header("Content-Disposition:inline; filename=00");
header("Accept-Ranges:bytes");
header("Content-Length:".$download_size);
readfile($file);

}
$i_counter++; // inc
}
}
closedir($handle); // close the opened directory
}
?>

or as a .zip file (http://netcode.net/files/get.php?cat=php&file=74)