Random Image

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
requires PHP. Its only accepts the Images of .gif .jpg and .png...

You may save this as anything, some parts of the code you have to edit, messages and image dir's.

Upload it to the root dir.

Code:
<?php

// Image Randomiser script by o0slowpaul0o of x10hosting.
// ************************************************************************/
// MAKE SURE $dir IS SET TO THE CORRECT DIRECTORY.
// IF THE IMAGES ARE IN THE SAME FOLDER AS randimg.php
// SET IT TO "./"
// ************************************************************************/
// If you are paranoid about people stealing your other images,
// uncomment (remove the //) the next three lines.
//if(!$HTTP_SERVER_VARS["HTTP_REFERER"]){
//        die("<p>Not allowed here, buddyboy. :)</p>\n<p><small><b><a href=\"http://Your URL">Page Name</a> script by Suspended o0slowpaul0o.(Can Cgange)</b></small></p>");
//}

// Set directory relative to this script.
// Doesn't matter if it has a trailing slash or not,
// it will be checked later.
// ie: in the directory "images" below this one:
// $dir = "./images/"; 
$dir = "./images/"; (Select Image Dir)

// Get the last one character in the variable "dir"
if($dirstr = substr($dir,strlen($dir)-1)){
        // Does the var not equal "/"? If so, add /
        // to the end of the existing variable "dir"
        if($dirstr !== "/Yourdir"){
                $dir .= "/Yourdir";
        }
}

// If you're using PHP5+, the next line will return TRUE
// and will use scandir(). Else, it will use the old style.
if(function_exists("scandir")){
        // Check to see if the directory actually exists
        if(is_dir($dir)){
                // Scan directory for our files
                $files = scandir($dir);
        }
        // Else echo FALSE
        else {
                die('<a href="Your URL"><b>Page Name</b></a> detected' .
                ' that: <em>The directory does not exist.(Can Change)</em>');
        }
}
else {
        // Check to see if the directory actually exists
        if( is_dir( $dir ) )
        {
                // Scan directory for our files
                $handle = opendir( $dir );
                $i = 0;
                while ( $currFile = readdir( $handle ) )
                {
                        $files[$i] = $currFile;
                        $i++;
                }
        }
        // Else echo FALSE
        else {
                die( '<a href="Your Link"><b>Page Name</b></a> detected' .
                ' that: <em>the directory does not exist.(Can Change)</em>' );
        }
}

// Count number of varibles in $files from our newly scanned directory
$cnt = count( $files );

// Counter for our avatar. Later to be used for randomising.
$av_cnt = 0;

// Get our image files from initial directory scan.
// We start from the 2 (third) entry because the first two are "." and "..".
// "." is the directory you are in. ".." is the previous directory.
for( $i = 2; $i < $cnt; $i++ )
{
        // Get the last four characters from each filename
        $str_ex = explode(".",$files[$i]);
        if( $str = $str_ex[1] )
        {
                // If the last four characters are equal to an image extension,
                // add it to our image file varable. Then add one to our image
                // counter.
                // Edit this by following the pattern.
                if( $str === "gif" or $str === "jpg" or $str === "png" )
                {
                        $av_files[$av_cnt] = $files[$i];
                        $av_cnt++;
                }
        }
}

//if( !$av_files )
//{
//        die( '<a href="Your URL"><b>Page NAme</b></a> detected' .
//        ' that: <em>there are no image files in your defined directory(Set Message).</em>' );
//}

// Randomise
$rnd = mt_rand( 0, count( $av_files ) - 1 );

// Define our path with out new random image
$path = $dir . $av_files[$rnd];

// Get image information like height, width and MIME type
$imgsize = getimagesize( $path );

// Redirect to the file
//header( "Content-type: " . $imgsize[5] );
header( "Cache-Control: no-cache" );
header( "Pragma: no-cache" );
header( "Location: " . $path );

?>

Put this some where in the page:

Code:
<?PHP include("/filenameyousaveditas.php");?>

I haven't had time to fix up a demo.
 

n4tec

Active Member
Messages
3,312
Reaction score
0
Points
36
Wow, i like the Random Image tut but it is lengthy.. i have tried the short tut.. And certainly these long tut will work..
 
Top