The uber geeks

Cam Status Detection

Written by Colin Devroe on Wednesday, March 24th, 2004 at 11:59 am. Colin is the founder of ChanceCube and the Community Evangelist for Viddler.

Cam Status Detection Script. I decided since I was going to make the webcam a permenant part of TUG that I would come up with a few features for it. The first feature that I am adding is a Status Icon which can be found to the right of my name under the cam image. The green icon displays when the cam image is live, the red when it isn’t.

How does it work? Currently, and as previously stated, my cam is set to 5 second intervals, which means that the image you see on the site is never more than 5 seconds old (give or take). So, I created a PHP script that would determine whether the image was more than 5 seconds old or not. Pretty simple in it’s conception, but it works out nicely. If the image is 8 seconds old, the cam status is set to off, if it’s less than 5 seconds old, than the cam status is obviously on. Even though the script is rather simple, I haven’t been able to ‘break’ it. It seems to work all the time, and has never given me incorrect results.

The code. The code to accomplish this is as follows, and can be downloaded here. The package includes 2 images for on and off.

// Project: Cam Status Detect
// Purpose: The purpose of this PHP
// script is to determine whether
// a specific file has been updated
// within the last X seconds.
// Results: Displays Status
// By: Colin D. Devroe
// Date: March 23, 2004
// Version: 0.1
// Contact: cdevroe@theubergeeks.net

// FileName to check
$strFileName = ‘cam_1.jpg’;

// Difference in Seconds
// Note: This can be changed to minutes,
// hours or even days.
// Info: http://us4.php.net/function.filemtime
$intInterval = 5;

// Results to Return
$strCamOn = ‘<img src=”wp-images/cam_on.gif” border=”0″ alt=”Image: Cam On” />’;
$strCamOff = ‘<img src=”wp-images/cam_off.gif” border=”0″ alt=”Image: Cam Off” />’;

// Check to see if file exists
if (file_exists($strFileName)) {

// If the file exists
// Gather the Difference in Time
// between the time the file was
// modified and the time it is currently.
$Diff = (time() - filemtime(”$strFileName”));

// Check to see if the time
// difference is greater than your
// allotted difference.
if ($Diff > $intInterval) {
// If the cam is off
echo $strCamOff; }
else {

// If the cam is on
echo $strCamOn;
}
}

If you have any comments about the script, or see some improvements that can be made, please send me an email. Also, since I am not graphics guru, if you would like to create some new on and off icons for me, I’d appreciate it. Submit them to my email address - cdevroe (at) theubergeeks.net.

Enjoy.

Update: Version 0.2 is now available.

Previous and next posts

« My Cam Settings

My 10 Gallon Ocean »

Related sites

Recent features

Song of the week

Sara Bareilles - Between the Lines
Sara Bareilles' "Between the Lines" is the Song of the Week for April 20 to 26.

The Deal Log