|
|
|
| Title : |
This script will read all images from a folder and read the files into an array. It uses rand() to get a random number. It will display a random image from the image folder given. |
| Categories : |
PHP, Arrays, Graphics, Filesystem |
 Christian Haensel |
| Date : |
Dec 11th 2007 |
| Grade : |
5 of 5 (graded 1 times) |
| Viewed : |
1689 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Christian Haensel |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
| <?php
/*
Random image display
This script will read all images from a folder and read the files into an array.
It uses rand() to get a random number. It will display a random image from the image folder given.
The check for whether it is an image or not is made by checking the file's extension.
Programmed by Christian Haensel on Tuesday, Dec. 12, 2007.
Visit my website at http://www.chftp.com. Email me at haensel@chftp.com
This script may be redistributed, altered and whatnot. Please, if you like it and/or find it useful, link back to me.
Chris
*/
// Point to the folder containing the images. Relative path without trailing slash!
$imgfolder = "images/mini";
// Checks if the file is an image. If not, it won't be added to the image list
function is_image($filename){
$filename = strtolower($filename) ;
$ext = split("[/\\.]", $filename) ;
$n = count($ext)-1;
$ext = $ext[$n];
// Here you can list the extensions you want to be allowed
if($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $ext == "gif" || $ext == "GIF" || $ext == "png" || $ext == "PNG") {
return true;
} else {
return false;
}
}
// Read the image folder into an array
$dir = opendir($imgfolder);
$array = array();
// Run through the folder
while($file = readdir($dir)) {
// Checks if the file is an image. Check the is_image() function a but up in this file
if(is_image($file)) {
// It's an image!! Put it into the array
array_push($array, $file);
}
}
// Count the amount of images in the array
$count = (count($array)-1);
// Generate a random number
$rand = rand(0,$count);
// Select an image from the array based on the random number
$img = $array[$rand];
// Output the image
echo '<img src="'.$imgfolder.'/'.$img.'" border="1">';
?> | | |
|
| Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | Simple way of scaling any image to fit either given width or height. Categories : PHP, Graphics, Arrays | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | Filling an array with files from any given directory. This example is for the current PHP script's directory. Categories : PHP, Arrays, Filesystem | | | How to ifconfig down/up a list of IP's Categories : Arrays, Strings, Filesystem, PHP | | | This is a simple photo gallery that reads the image files from multiple directories, and generates a web page styled with CSS1. It opens single auto window to view and print a given image.
Categories : Graphics, Filesystem, PHP, Complete Programs | | | Random Image Display Categories : PHP, Filesystem, Graphics, HTML and PHP | | | How to display any array in several rows and columns of a table. Not just
in one column or in alternate rows. This example shows a nice color table
generated with PHP, but can be used with any array values(e.g. Database) Categories : Arrays, PHP, Miscellaneous, Beginner Guides, Graphics | | | Easy slideshow with php. Just place this one file into a subdirectory where jpg files reside. An instant slideshow is yours. MS explorer only. Categories : PHP, Filesystem, Graphics | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | Simple PHP Bar Graph using GD library Categories : PHP, GD image library, Graphics, Arrays | | | HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout. Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs | | | This program implements hot link prevention in php. It is useful for webmasters who do not have access to the server at a level where they can control hot linking can still supply some type of hot link prevention for thier site by using php. Categories : PHP, Filesystem, Graphics, Content Management | | | Simple image counter Categories : PHP, Graphics, Filesystem, Beginner Guides | | | Grab images from one or more URLs and save them to a specified local directory. Categories : PHP, Filesystem, Strings, Arrays | |
|
|
|