Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Select and displays a random image from a directory
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--
Displaying A Random Image From A Directory Via PHP, Part 2
Copyright 2007 Doug Vanderweide
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Displaying A Random Image From A Directory Via PHP</title>
<link href="../demo.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#leftCol {
float: left;
padding-right: 10px;
margin-right: 10px;
border-right: solid 1px #000;
}
table#thumbs, table#thumbs td {
border: solid 1px #000;
}
table#thumbs td {
padding: 10px;
text-align: center;
}
table#thumbs td.active {
background: #ffd2d6;
}
p.note {
font-style: italic;
}
p.note, table#thumbs td {
font-size: 10px;
}
h3, p.note {
margin: 5px;
}
</style>
</head>
<body>
<?
$img_path = "images/";
$exts = "/(.jpg)|(.jpeg)|(.gif)|(.png)$/i";
if(is_dir($img_path)) {
$img_handle = opendir($img_path);
while (false !== ($img_file = readdir($img_handle))) {
if (preg_match($exts, $img_file)) {
$img[] = $img_file;
}
}
mt_srand();
$len = count($img) - 1;
$index = mt_rand(0, $len);
$img_src = $img_path . $img[$index];
closedir($img_handle);
}
else {
die("no such directory!");
}
?>
<h1>
Displaying A Dynamic, Random Image From A Directory Via PHP<br />
Example 2: Displaying On A Web Page
</h1>
<div id="container">
<div id="leftCol">
<!-- BEGIN dynamic image from PHP -->
<img src="<? echo $img_src; ?>" alt="<? echo $img_src; ?>" title="<? echo $img_src; ?>" />
<!-- END dynamic image from PHP -->
</div>
<div id="rightCol">
<h3>Images to be shown:</h3>
<p class="note">Currently showing image highlighted in pink.</p>
<table id="thumbs" cellspacing="0">
<?
$i = 0;
$TN_COLS = 3;
$tn_path = "images/tn/";
if(is_dir($tn_path)) {
$tn_handle = opendir($tn_path);
while(false !== ($tn_file = readdir($tn_handle))) {
if(preg_match($exts, $tn_file)) {
$tn_img[] = $tn_file;
}
}
natsort($tn_img);
foreach($tn_img as $thumb) {
if($i % $TN_COLS == 0) {
echo "<tr>";
}
echo "<td";
if($thumb == $img[$index]) {
echo " class=\"active\"";
}
echo ">";
echo "<img src=\"" . $tn_path . $thumb . "\" alt=\"$thumb\" title=\"$thumb\" />";
echo "<br />";
echo $thumb;
echo "</td>\n";
$i++;
if($i % $TN_COLS == 0) {
echo "</tr>\n";
}
}
if($i % $TN_COLS != 0) {
for($x = $i; $x % $TN_COLS > 0; $x++) {
echo "<td>&nbsp;</td>\n";
}
echo "</tr>\n";
}
closedir($tn_handle);
}
else {
die("no such thumbnail image path!");
}
?>
</table>
</div>
</div>
<p><a href="example2.php">Refresh this page</a> to see a new image</p>
<p><a href="http://www.dougv.com/blog/2007/07/03/displaying-a-random-image-from-a-directory-via-php-part-2/">Displaying A Random Image From A Directory Via PHP, Part 2</a></p>
<p>&nbsp;</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.