Solved

How can I cache a fetched image from a network and output it on the webserver?

Posted on 2003-11-20
3
246 Views
Last Modified: 2007-12-29
I have a webcam set up on a computer on our University network.  This computer is visible to all other uni computers, including the webserver we use, but not to the outside world.  Currently the webcam is serving an image on port 80 of it's own computer.  I can link to this with an image tag in a file on the webserver and the pic will display when a campus computer requests it, but obviously not when the request is made from the outside world.  Therefore I need some PHP to fetch the image from the webcam server, cache it, and output it on the web page so that all can see the image.  Any ideas how to do this?
0
Comment
Question by:djpeanut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 200 total points
ID: 9788520
hi,

script by a friend of mine seems to suit your needs:

<?

// Sam Overton 2002

$filename = "wcamimg.jpg";                  // file to cache in
$altfile = "images/unavail.gif";                  // alt image to use (if cam is down)
$update_time = 3;                        // fetch new image if last update was more than x seconds
$url = "http://server.com:80/";                  // url to fetch the image from
$maximgsize = 300*1024;                  // maximum amount of data it will read

// check if this is the first time we run the script (evar!)
$makenew = FALSE;
if(!file_exists($filename))
{
      touch($filename);
      $makenew = TRUE;
}


//get file modified time
$timestamp = filemtime($filename);
$thetime = time();
if($thetime - $timestamp > $update_time || $makenew)
{
      // we need to fetch a new copy
      $newfile = @fopen($url,"rb");
      if(!$newfile)
      {
            // couldnt get a new copy!!!
            // use the alt image (webcam unavailable or soemthing)
            $newfile = fopen($altfile,"rb");
      }

      // read in our data from the remote location
      $buffer = fread($newfile,$maximgsize);
      fclose($newfile);

      // store it locally
      $file = fopen($filename,"wb");
      fwrite($file,$buffer);
      fclose($file);

      // spit it out
      header("Content-type: image/jpeg");
      print($buffer);
}
else
{
      // local copy is recent enough so read it in
      $file = fopen($filename,"rb");
      $buffer = fread($file,filesize($filename));
      fclose($file);

      // spit, dont swallow :[
      header("Content-type: image/jpeg");
      print($buffer);
}
?>

then you can just use <img src="webcam.php" border="0"> or something in any other page to get the image

loz
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question