Grabbing a single jpeg frame from mjpeg stream using PHP

I have a webcam on a computer running Motion, a webcam streaming program popular in Linux. Motion basically outputs a mjpeg stream. I’d like to use PHP to grab a single jpeg from the mjpeg stream and display it. The reason I’m doing this is because Motion doesn’t have any built in password protection. With PHP, I can hide the port and IP address of my home server.

I’m trying code provided by the creators of Motion on their website. The code is suppose to grab just a single jpeg from the mjpeg stream, but I get an error when trying to run it on an nginx server locally.


<? $camurl=""; $boundary="\n--"; $f = fopen($camurl,"r") ; if(!$f) { //**** cannot open echo "error"; } else { //**** URL OK while (substr_count($r,"Content-Length") != 2) $r.=fread($f,512); $start = strpos($r,'ÿ'); $end = strpos($r,$boundary,$start)-1; $frame = substr("$r",$start,$end - $start); header("Content-type: image/jpeg"); echo $frame; } fclose($f); ?>


For reference, this is where I got the code from:

Maybe someone could help me out? I have very little understanding of PHP, and don’t understand why the fopen command is returning a fault.

In case you’re wondering, I’ve also tried this code snippet. It works, but it’s grabbing an entire mjpeg stream. When ran locally on my lan, it works, but when I try the code on a shared host, the shared host computer seems to be trying to download the entire mjpeg stream, which would never complete of course. That’s why I’m trying to find code that grabs just a single jpeg instead of streaming the mjpeg.

$fp = fsockopen (“”, 20804, $errno, $errstr, 30);
if (!$fp) {
echo “$errstr ($errno)
} else {
fputs ($fp, “GET / HTTP/1.0\r\n\r\n”);
while ($str = trim(fgets($fp, 4096)))

Sponsor our Newsletter | Privacy Policy | Terms of Service