Using Perl to Display a File Creation Date
Getting back to my post on displaying the file creation date and time, a couple of readers have sent in helpful suggestions. Before I get to one, let me briefly recap what I am trying to do.
Via a security webcam/server at home, I periodically ftp an image to my website, seto.org. A web page I've created then displays the image. Every once in awhile, I check the web page to see what's going on at home. But want I wanted was a way of time stamping the image so I would know when it was taken. I found that a Linux program called Stamp! would do that. Sort of. What Stamp! does is uses the current system time and overlays the image with that. This causes a problem for me in that Stamp! may be updating even if the webcam is turned off. This means the image that is sitting on seto.org could be several hours old, but by just viewing the web page, you would have very little, if any way of knowing this.
So I searched for a program that would overlay the test.jpg file with the file's creation date and time. I have been unsuccessful in finding such a program. So, I tried finding some Perl script that would at least read the creation date and time of test.jpg and then insert/output the result into the web page that also displays the image.
By searching the web, I found a code snippet that supposedly reads the file date and time but ran into problems. Which is where we left off.
A reader by the name of Peter, sorry I don't have is last name so I can't give him full credit but he knows who he is, suggested the following:
#!/usr/bin/perl
use File::stat;
use Time::localtime;
$file='/path/to/test.jpg';
$date_string = ctime(stat($file)->mtime);
print "file $file updated at $date_string\n";
Making this executable and running this from the command line displays the wanted date and time of the file. Now I could use some help in figuring out how to display this information in a web page that also displays the test.jpg file so that, using a web browser, I can see the test.jpg image and the date and time of the test.jpg file (If this information can be overlayed on the test.jpg itself like Stamp! does would be a bonus but I don't know of any programs that will do that. So, just displaying the date and time of the file in HTML would be better than what I have now.).
The present web page code itself is below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Seto.org Upstairs</title>
<meta http-equiv="refresh" content="60">
</head>
<body>
<img src="/path/to/test.jpg" width="640" height="480" alt="
Upstairs Web Cam.">
<p>Refreshes automatically every 60 seconds.</p>
</body>
</html>
A big thank you to Peter and to fellow Daynoter John Dominik who suggested a different way of doing it.
Aloha!
Comments
Dan,
It seems to me that the date and time of a jpg file's creation are stored in the EXIF data in the file. How to make write this on the image itself is well beyond my knowledge, but any EXIF viewer, like the one included in Irfanview will show this information.
As I read this, it isn't at all clear that it is helpful, but I suppose it worth what I charge .
Frank
Posted by: Frank Fitz | November 2, 2005 08:50 AM
You'll need SSI turned on, but once you have, just dump something like the following into your page where you want the info:
If you get blank output where that tag is, you've either got SSI turned off, or haven't named the page .shtml (if your server's setup to require that).
BTW: you should be able to script any shell commands, or combos of into a shell file and then run that from the webpage, eg. tail procmail.log, or uptime, or similar.
Posted by: Phil | November 2, 2005 10:00 AM
Hmm... The more command line tools could prove fun here...
For instance:
jhead from http://www.sentex.net/~mwandel/jhead/
This will let you do stuff with the EXIF data on the image; stuff includes extracting the EXIF data, and renaming the file based on the internal time stamp info.
THe there is ImageMagick from http://www.imagemagick.org/script/index.php
This is another very comprehensive suite of tools for image manipulation. An intereseting article at http://www-128.ibm.com/developerworks/library/l-graf/?ca=dnt-428 then goes into some examples using ImageMagix such as the command line
# convert -font helvetica -fill white -pointsize 36 \
-draw 'text 10,50 "Floriade 2002, Canberra, Australia"' \
floriade.jpg comment.jpg
that adds the text "Floriade ..." to the file floriade.jpg saving the output in comment,jpg.
What you could do is use jhead to extract the EXIF data, find the date/time in that list of output, and then use convert to insert that into the file.
Posted by: Peter Thomas | November 3, 2005 03:47 AM