You are not logged in! If you have not registered yet then click here to join the community!

TOLRA Community Forums


Go Back   TOLRA Community Forums > Web Development > PHP Forum

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 26-02-2007, 02:00 PM
Paul's Avatar
Paul Paul is offline
Moderator
 
Join Date: May 2006
Location: UK
Posts: 568
Blog Entries: 3
Very simple random image display in PHP

The following code when placed in a .php file will randomly display an image from the list available:
Code:
<img src="images/<?php
$pics = array('image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg');
echo $pics[array_rand($pics)];
?>" />
The picture to show are expected to be in the folder images with the possible pictures all listed in the $pics array.

The code can be modified to randomly set the background image of a div as follows:
Code:
<div style="background-image: url(images/<?php
$pics = array('image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg');
echo $pics[array_rand($pics)];
?>)" class="picture"></div>

Last edited by Paul; 16-05-2008 at 09:00 AM.
Reply With Quote
Sponsored Links
  #2  
Old 16-05-2008, 09:13 AM
Paul's Avatar
Paul Paul is offline
Moderator
 
Join Date: May 2006
Location: UK
Posts: 568
Blog Entries: 3
The following variation looks in the folder mygallery for all files ending in .jpg, it then chooses one at random which is output in an img tag:
Code:
<?php

$pics = glob('mygallery/*.jpg', GLOB_NOSORT);
$pic = $pics[array_rand($pics)];

echo "<img src=\"$pic\" />";

?>
As above but outputting the image as a CSS background:
Code:
<?php

$pics = glob('mygallery/*.jpg', GLOB_NOSORT);
$pic = $pics[array_rand($pics)];

echo "<div style=\"background-image: url($pic);\" style=\"picture\"></div>";

?>
Reply With Quote
  #3  
Old 30-01-2009, 09:48 AM
arun1988 arun1988 is offline
Member
 
Join Date: Jan 2009
Posts: 31
look at this:
The code, with comments:
Code:
< ?
//INITIALIZE RANDOM GENERATOR
srand((float) microtime() *10000000);

//IMAGES TO BE DISPLAYED
$imagefiles = array("image1.jpg","image2.jpg","image3.jpg");

//GRAB A RANDOM FILENAME FROM THE ARRAY
$imagefiles = $imagefiles[rand(0,count($imagefiles)-1)];

//PATH WHERE THE IMAGES ARE STORED ON OUR WEB SERVER
$path = "/home/user/www/" . $imagefiles;

//TELL THE BROWSER WE ARE SENDING AN IMAGE
header("Content-type: image/jpeg");

//OPEN THE IMAGEFILE
$jpeg = fopen($path,"r");

//READ THE IMAGEFILE
$image = fread($jpeg,filesize($path));

//SEND IT DOWN THE PIPE
echo $image;
?>

Voila! Place the code in a seperate file such as image.php, then call the image in your page using the img tag like this:

<img src=image.php>
__________________
New York Web design
Website design
Reply With Quote
  #4  
Old 30-01-2009, 01:51 PM
Paul's Avatar
Paul Paul is offline
Moderator
 
Join Date: May 2006
Location: UK
Posts: 568
Blog Entries: 3
You can do that same trick in a lot less code:
PHP Code:
<?php
$pics 
glob('myimages/*.jpg'GLOB_NOSORT);
$pic $pics[array_rand($pics)];
header("Content-type: image/jpeg");
header("Content-Disposition: filename=\"" basename($pic) . "\"");
readfile($pic);
?>
The above will look in the folder myimages for all the JPG files then pick one at random and return it.

The added benefit here is that simply adding images to the myimages folder adds them as an option to be returned, no code changes needed.

You still call it with <img src="image.php" /> or whatever you name the PHP file.
Reply With Quote
  #5  
Old 16-09-2013, 03:32 PM
teri888 teri888 is offline
Junior Member
 
Join Date: Jul 2013
Posts: 9
Dude i have same problem in php for the image because i am new in php that's why problem is occurred i hope this information is very helpful for me specially...
Prime Scales|Floor Scales

Last edited by teri888; 13-11-2013 at 08:33 AM.
Reply With Quote
Reply

Bookmarks

Tags
display, image, php, random, simple

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 AM.