Displaying an image file name
I'm not sure if this is possible or not.. but here's what I'm trying to do
I hate a php script that randomly displays images in a directory and I want another script that displays the filename of that image below it.
The purpose of this is... it'll be a picture of someone and the file will be called Bob_Johnson.jpg or something... so the text will match with the random image.
Any help or guidance is appreciated. Thanks.
Busy posted this at 08:51 — 18th January 2007.
He has: 6,151 posts
Joined: May 2001
The script you have should be be calling the image name, just find the variable used and use that to just display the name (don't put it in a
msaz87 posted this at 23:14 — 18th January 2007.
They have: 68 posts
Joined: Jun 2006
I'm really not very good with PHP... I can integrate it, but I don't really know much about it.
Would this be an "echo" command that I would plug in the $variable?
Greg K posted this at 02:45 — 19th January 2007.
He has: 2,145 posts
Joined: Nov 2003
Here is some sample code to that will do what you are looking for. If you want to keep using what you currently have to get the random picture name, then use just the middle section with the GET DISPLAY NAME comments around it, and make sure the image name is placed into the variable $file2use
<?php
define('SERVER_ROOT','/home/httpd/html'); // Server path to domainname
define('WEB_PATH','/pics/staff'); // Path to images under domainname
// In the above example, <a href="http://www.domain.com/" title="http://www.domain.com/">http://www.domain.com/</a> would use files
// located on the server in /home/httpd/html and the images to
// be called by this script are at <a href="http://www.domain.com/pics/staff
" title="http://www.domain.com/pics/staff
">http://www.domain.com/pics/staff
</a>
$files = array();
$d = dir(SERVER_ROOT . WEB_PATH);
while (($filename = $d->read()) !== false)
if (substr($filename,-4)=='.jpg')
$files[] = $filename;
$d->close();
if (count($files)<1) die ('No Images Found');
// Seed Random Number
list($usec,$sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$imgnum = rand(1,count($files));
if (isset($files[0]))
$file2use = $files[$imgnum-1];
else
$file2use = $files[$imgnum];
// START GET DISPLAY NAME
// filename (no path) is in $file2use
$dispname = substr($file2use,0,-4); // Gets rid of the .jpg extension
$dispname = str_replace('_',' ',$dispname); // Converts underscores to spaces
$dispname = ucwords(strtolower($dispname)); // Make sure names are capitolized
// END GET DISPLAY NAME
// Displayed name is in $dispname
$imgpath = WEB_PATH . \"/\" . $file2use;
<img src=\"= $imgpath \" alt=\"Picture of = $dispname \"><br />= $dispname
?>
msaz87 posted this at 03:31 — 19th January 2007.
They have: 68 posts
Joined: Jun 2006
I really appreciate your help ... and I hate to ask for more, but like I said, I really don't know much PHP (especially if it's not ready to go).
This is the random script I currently use:
<?php
<?
//**************************************//
// BASIC IMAGE RANDOMIZER v-1.0 //
// COPYRIGHT (C) 2005 BRANDON HEYER //
// <a href="mailto:[email protected]">[email protected]</a> //
//**************************************//
function imageRandom($dir, $types) {
$images = array();
$new = array();
if ( !empty($types) ) {
$types = explode(",", $types);
foreach ( $types as $t ) {
$glob = $dir . '*.' . $t;
$new = glob( $glob );
if ( !empty( $new ) ) {
$images = (array)$new + (array)$images;
}
}
} else {
$images = glob( $dir . '*.*' );
}
$key = array_rand($images);
$image = $images[$key];
echo $image;
}
?>
?>
And then the tag I put in my HTML is:
<?php
<img width="77" height="87" src="/<? imageRandom('new_employees/', 'jpg,gif,png');
?>
?>
Can you help me get the image name part into that code?
I really appreciate your help.
Greg K posted this at 04:37 — 19th January 2007.
He has: 2,145 posts
Joined: Nov 2003
The following should work for you...
In the code you posted, add the following right after the <? and before the comments below it
<?php
$usedimagename = \"\";
?>
<?php
$dispname = substr($image,strrpos($image,\"/\")+1,-4); // eliminate any path and extention
$dispname = str_replace('_',' ',$dispname); // Converts underscores to spaces
$dispname = ucwords(strtolower($dispname)); // Make sure names are capitolized
$GLOBALS['usedimagename'] = $dispname;
?>
The value of $usedimagename will remain the same until the next time you call the function again.
So you could do something like the following (I broke it up into 3 lines for easier reading here, you can use it all in a singe line):
<img width="77" height="87"
src="/<? imageRandom('new_employees/', 'jpg,gif,png'); ?>"
alt="Picture of <?= $usedimagename ?>" />
<br /><?= $usedimagename ?>
msaz87 posted this at 07:41 — 19th January 2007.
They have: 68 posts
Joined: Jun 2006
I've run into a problem that's slightly unrelated to the earlier issue.
I tried to put the php script into play (minus the image name portion) on the server ... and it's not working.
The server is private and hosted at a business, so I can't link it... but I did try it on another public server and it works fine:
http://www.foothillsbaptist.org/anniversaries.php/
This is the code for the private page:
<?php
<html>
<head>
<meta http-equiv=\"Content-Language\" content=\"en-us\">
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">
<title>New Page 1</title>
<script>
var newwindow;
function suggestionbox(url)
{
newwindow=window.open(url,'name','height=550,width=480,scrollbars=yes');
if (window.focus) {newwindow.focus()}
}
var newwindow;
function rotate(url)
{
newwindow=window.open(url,'name','height=600,width=530,scrollbars=yes');
if (window.focus) {newwindow.focus()}
}
</script>
<style>
a.links_white:link {color: #ffffff; font-style: none;}
a.links_white:active {color: #ffffff; font-style: none;}
a.links_white:visited {color: #ffffff; font-style: none;}
a.links_white:hover {color: #000000; font-style: none;}
</style>
</head>
//**************************************//
// BASIC IMAGE RANDOMIZER v-1.0 //
// COPYRIGHT (C) 2005 BRANDON HEYER //
// <a href="mailto:brandonheyer@gmail.com">[email protected]</a> //
//**************************************//
function imageRandom($dir, $types) {
$images = array();
$new = array();
if ( !empty($types) ) {
$types = explode(\",\", $types);
foreach ( $types as $t ) {
$glob = $dir . '*.' . $t;
$new = glob( $glob );
if ( !empty( $new ) ) {
$images = (array)$new + (array)$images;
}
}
} else {
$images = glob( $dir . '*.*' );
}
$key = array_rand($images);
$image = $images[$key];
echo $image;
}
<body ARGINHEIGHT=\"0\" MARGINWIDTH=\"0\" topmargin=\"0\"
rightmargin=\"0\" leftmargin=\"0\" bgcolor=\"#A60014\">
<table border=\"0\" width=\"161\" id=\"table4\" height=\"130\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td>
<p style=\"margin-top: 0; margin-bottom: 0\" align=\"center\"> <b><font face=\"Verdana\" color=\"#FFFFFF\" size=\"2\">Happy
Anniversary!</font></b></td>
</tr>
<tr>
<td>
<div align=\"center\">
<table border=\"0\" width=\"77\" height=\"87\" id=\"table5\" style=\"border: 2px solid #FFFFFF\" cellspacing=\"0\" cellpadding=\"0\" background=\"anniversary_filler.gif\">
<tr>
<td>
<p style=\"margin-top: 0; margin-bottom: 0\" align=\"center\">
<img width=\"77\" height=\"87\" src=\"/ imageRandom('anniversaries/', 'jpg,gif,png'); \" />
</td>
</tr>
</table>
</div>
<p style=\"margin-top: 2px; margin-bottom: 0\" align=\"center\">
<font size=\"1\" face=\"Verdana\" color=\"#FFFFFF\">
<a class=\"links_white\" href=\"javascript:rotate('a_list.htm');\">
<span style=\"text-decoration: none\">view all for
the month</span></a></font></td>
</tr>
</table>
</body>
</html>
?>
When I view the page on the other server and right click on the broken image, it pops up saying the correct properties (IE: It knows the image in the directory to display, it just won't display). The images are in the folder ... and so I'm not sure, everything is identical between the two servers.
Also, the script is in shorthand php... but when I installed php on this private server, I allowed shorthand tags.
Anyone have any ideas?
Greg K posted this at 15:04 — 19th January 2007.
He has: 2,145 posts
Joined: Nov 2003
when in doubt why an image comes up broken, in firefox you can riht click on the broken image and choose VIEW IMAGE, this will browse you to the image being called and you can see the full URL in the address bar. Sometimes this will let you see where there is a relative path mess up.
-Greg
msaz87 posted this at 21:43 — 20th January 2007.
They have: 68 posts
Joined: Jun 2006
OK, for the sake of ease ... I'm going to try and implement your original code, Greg.
Here it is:
<?php
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">
<title>New Page 1</title>
</head>
<body>
define('SERVER_ROOT','/home/XXXX/public_html/'); // Server path to domainname
define('WEB_PATH','rotate/birthdays/'); // Path to images under domainname
// In the above example, <a href="http://www.domain.com/" title="http://www.domain.com/">http://www.domain.com/</a> would use files
// located on the server in /home/httpd/html and the images to
// be called by this script are at <a href="http://www.domain.com/pics/staff
" title="http://www.domain.com/pics/staff
">http://www.domain.com/pics/staff
</a>
$files = array();
$d = dir(SERVER_ROOT . WEB_PATH);
while (($filename = $d->read()) !== false)
if (substr($filename,-4)=='.jpg')
$files[] = $filename;
$d->close();
if (count($files)<1) die ('No Images Found');
// Seed Random Number
list($usec,$sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$imgnum = rand(1,count($files));
if (isset($files[0]))
$file2use = $files[$imgnum-1];
else
$file2use = $files[$imgnum];
// START GET DISPLAY NAME
// filename (no path) is in $file2use
$dispname = substr($file2use,0,-4); // Gets rid of the .jpg extension
$dispname = str_replace('_',' ',$dispname); // Converts underscores to spaces
$dispname = ucwords(strtolower($dispname)); // Make sure names are capitolized
// END GET DISPLAY NAME
// Displayed name is in $dispname
$imgpath = WEB_PATH . \"/\" . $file2use;
<img src=\"= $imgpath \" alt=\"Picture of = $dispname \"><br />= $dispname
</body>
</html>
?>
You can see it in action here:
http://www.foothillsbaptist.org/r_test.php
But you'll notice the problem of it not finding any images in this directory:
http://www.foothillsbaptist.org/rotate/birthdays/
Sorry to be such a pain.
Greg K posted this at 16:51 — 23rd January 2007.
He has: 2,145 posts
Joined: Nov 2003
Hey there, sorry for the delay, was a hectic weekend.
Think I found the problem with it. I assumed that since you were doing photos, they would be .JPG, so I didn't include .gif's
Change the following code (line 12-16 of the original code I sent you)
<?php
$d = dir(SERVER_ROOT . WEB_PATH);
while (($filename = $d->read()) !== false)
if (substr($filename,-4)=='.jpg')
$files[] = $filename;
$d->close();
?>
<?php
$d = dir(SERVER_ROOT . WEB_PATH);
while (($filename = $d->read()) !== false)
{
$ext = strtolower(substr($filename,-4));
if ($ext=='.jpg' || $ext=='.gif' || $ext=='.png')
$files[] = $filename;
}
$d->close();
?>
for your code it would be:
<?php
define('SERVER_ROOT','/home/XXXX/public_html'); // Server path to domainname
define('WEB_PATH','/rotate/birthdays'); // Path to images under domainname
?>
-Greg
PS. in looking over the image names in the directory you gave, it appears they are in there LATNAME_FIRSTNAME.gif. If this is the way they all will be, let me know and I'll adjust the code for you to reverse the names, or leave them in that order, but add a comma. (LASTNAME, FIRSTNAME) Just let me know what way all the images are stored, and what format you want them displayed.
msaz87 posted this at 07:24 — 24th January 2007.
They have: 68 posts
Joined: Jun 2006
The adjusted script works perfectly ... but as always, there's a problem.
While the script works great on the public server, the server it's intended for doesn't agree with it. Here's the lowdown of the server:
Windows
PHP 5.1.4
mySQL 5.0.22
Apache 2.2
Here's the code I plugged in:
<?php
define('SERVER_ROOT','C:\Program Files\Apache Software Foundation\Apache2.2\htdocs'); // Server path to domainname
define('WEB_PATH','/rotate/birthdays'); // Path to images under domainname
?>
While the server root might be incorrect... the test page doesn't tell me so. I screwed up the root on the public server and it came up with an error letting me know so -- whereas this shows a blank page regardless of what I plug in there.
The source of the page is completely blank:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
Not even the tag (which I assume would appear despite the php variable inside?
The one thing I can think of is that I might not have short tags set up for php ... but I know I ran into that problem previously and resolved it -- I just don't know if I converted it to full tags or enabled short tags.
This is probably getting difficult considering I can't show you the server anymore... but as always, I really appreciate your help.
-------------
EDIT:
I ran an info script for PHP .... so I have all the details of it and everything else to do with the server, if that'll help.
Greg K posted this at 22:42 — 1st February 2007.
He has: 2,145 posts
Joined: Nov 2003
The only thing I can think of is the use of the forward vs. back slashes.
try adding a second define, called SERVER_PATH, set it to the same as WEB_PATH, except use backslashes. Then in the code where it says $d = dir(SERVER_ROOT . WEB_PATH); and change it to $d = dir(SERVER_ROOT . SERVER_PATH);
I'm not to used to using PHP on a windows system, so this is just a guess.
-Greg
msaz87 posted this at 23:44 — 1st February 2007.
They have: 68 posts
Joined: Jun 2006
I actually resolved the issue ... and it may have been changing the slashes. I asked for help on a php forum and someone gave me a snippet of code to insert to get error messages and when I tried it again it worked. So, I'm guessing I changed something and only thought I tested it.
Either way ... it works great now.
I was wondering if it's difficult to add a few more specs into the code.
Basically, what I might want to have it do is list the department the person works for or their years there (for anniversaries).
Is it possible to graft these into the code if the file name includes it? (Ex: john_smith_engineering_13.jpg)
As always, thanks very much for your help.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.