NameSilo

[PHP] Display random images (from a directory)

Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
SpaceshipSpaceship
Watch

Hitch

Established Member
Impact
74
There are many scripts and tutorials out there that let you display random images.
Most of them require you to specify which images to show… which can be a pain writing out what images you want to display, especially if there is a lot of them.

Which is why i wrote this PHP function, to make it simpler.
Instead of listing the files, just specify which directory contain the images you want displaying.

This script will “scan” your specified directory, and display the files randomly. (Given they have a image extension of .jpg, .jpeg, .gif, .png, .ico)

The Code
PHP:
<?php

// Written by Cueburst (http://cueburst.com)

error_reporting(E_ALL & ~E_NOTICE);

// Random Image function
function random_image($directory)
{
	$leading = substr($directory, 0, 1);
	$trailing = substr($directory, -1, 1);
	
	if($leading == '/')
	{
		$directory = substr($directory, 1);
	}
	if($trailing != '/')
	{
		$directory = $directory . '/';
	}
	
	if(empty($directory) or !is_dir($directory))
	{
		die('Directory: ' . $directory . ' not found.');
	}
	
	$files = scandir($directory, 1);
	
	$make_array = array();
	
	foreach($files AS $id => $file)
	{
		$info = pathinfo($dir . $file);
	
		$image_extensions = array('jpg', 'jpeg', 'gif', 'png', 'ico');
		if(!in_array($info['extension'], $image_extensions))
		{
			unset($file);
		}
		else
		{
			$file = str_replace(' ', '%20', $file);
			$temp = array($id => $file);
			array_push($make_array, $temp);
		}
	}
	
	if(sizeof($make_array) == 0)
	{
		die('No images in ' . $directory . ' Directory');
	}
	
	$total = count($make_array) - 1;

	$random_image = rand(0, $total);
	return $directory . $make_array[$random_image][$random_image];

}

?>

To use the function, it's quite simple, type of your HTML Img line, and add our function in the source, for example, lets say our images are located in a folder called cars...

PHP:
<?php

require 'random_image.php';
echo "<img src=" . random_image('cars') . " />";

?>

Final Notes

  • When entering the directory name, there is no-need for training or leading slashes, the function will check and add them in where applicable.
  • Accessing parent directories is also allowed, example, ../../cars/
  • Image's may have spaces in the filenames, they will be replaced with %20.
:tu:

Enjoy!
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Thanks for this!
 
0
Close
lol, just name images as numbers. 1.jpg,2.jpg, etc. will save tons on CPU resources
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
CryptoExchange.com
Domain Recover

NamePros updates

Quick replies: search, your most used replies, and more on every post
Quick replies are easier to find and use now. The quick reply menu has a search box. Type a few letters to find the reply you want. A...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back