NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page [PHP] Display random images (from a directory)

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 03-11-2010, 03:49 PM THREAD STARTER               #1 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!

[PHP] Display random images (from a directory)


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 Code:
<?php

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

error_reporting(E_ALL & ~E_NOTICE);

// Random Image function
function random_image($directory)
{
    
$leading substr($directory01);
    
$trailing substr($directory, -11);
    
    if(
$leading == '/')
    {
        
$directory substr($directory1);
    }
    if(
$trailing != '/')
    {
        
$directory $directory '/';
    }
    
    if(empty(
$directory) or !is_dir($directory))
    {
        die(
'Directory: ' $directory ' not found.');
    }
    
    
$files scandir($directory1);
    
    
$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
        {
????: NamePros.com http://www.namepros.com/code/644409-php-display-random-images-from-directory.html
            
$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 Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=644409

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.


Enjoy!
Hitch is offline   Reply With Quote
Old 04-06-2010, 04:20 AM   #2 (permalink)
NamePros Regular
Join Date: Aug 2009
Posts: 313
Obulus is an unknown quantity at this point
 



Thanks for this!
__________________
Available for hire.
Obulus is offline   Reply With Quote
Old 04-09-2010, 12:08 AM   #3 (permalink)
Senior Member
 
Nodws's Avatar
Join Date: Jun 2008
Location: nodws.com
Posts: 1,666
Nodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud ofNodws has much to be proud of
 



Save a Life
lol, just name images as numbers. 1.jpg,2.jpg, etc. will save tons on CPU resources
__________________
Nodws is offline   Reply With Quote
Reply

Tags
cueburst, php, random image


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 07:32 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger