how to get a snapshot using php for dahua SD49225T-HN

wozzzzza

Pulling my weight
Jan 5, 2015
456
102
trying to write a php script that refreshes every second and displays the image that the SD49225T-HN camera is seeing in your web browser but its not working.
if i put in internet explorer
http://User:Passwd@xxx.xxx.xxx.xxx/cgi-bin/snapshot.cgi
all i get is an error saying windows cannot find it.
yet if i put it in chrome it works.
how do i make a php script return the image of the camera??
 
yes the picture actually works with that url, but now i have come up with the code below after a day of stuffing around.
but now i get from the script that it is "Unable to decode image"
what am i doing wrong here??
PHP:
<?php

$user = "admin";
$pass = "xxxxxxxx";
$basic_auth=false;



$url="http://192.168.xxx.xxx/cgi-bin/snapshot.cgi?";

$options=build_options($url,$user,$pass,$debug=1,$basic_auth);

# execute the curl call
$response=curl_call($options);

header("Content-type: image/jpeg");
print imagejpeg($response);
        
              
# ===== Functions ====
function catchGet($opt){
  if(isset($_GET[$opt])&&trim($_GET[$opt])!==''){
    return trim($_GET[$opt]);
  }else{
    return false;
  }
}

function build_options($url,$user,$pass,$debug,$basic_auth){
  $options = array(
          CURLOPT_URL            => $url,
          CURLOPT_BINARYTRANSFER => true,
          CURLOPT_USERPWD        => $user . ":" . $pass,
          CURLOPT_RETURNTRANSFER => true,
  );
  # auth method
  if($basic_auth){
    $auth_header='Basic '.base64_encode("$user:$pass");
    $options[CURLOPT_HTTPHEADER]=[$auth_header];
  }else{
    $options[CURLOPT_HTTPAUTH]=CURLAUTH_DIGEST;
  }
  # debug headers
  if($debug){
    $options[CURLOPT_HEADER]=true;
    $options[CURLOPT_VERBOSE]=true;
  }
  return $options;
}

function curl_call($options){
  $ch = curl_init();
  curl_setopt_array( $ch, $options);

  try {
    $raw_response  = curl_exec( $ch );
    // validate CURL status
    if(curl_errno($ch)){
        throw new Exception(curl_error($ch), 500);
    }
    // validate HTTP status code (user/password credential issues)
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($status_code != 200)
        throw new Exception("Response with Status Code [" . $status_code . "].", 500);
  } catch(Exception $ex) {
      throw new Exception($ex);
  } finally {
      curl_close($ch);
      return $raw_response;
  }
}

?>
 
Here's what I was using to change image size:

Code:
<!DOCTYPE html>
<html>
<head>
<title>Dog Dog Cam</title>
</head>
<body>
<?php
$orig = file_get_contents("http://10.0.0.49/cgi-bin/snapshot.cgi");
$im = imagecreatefromstring($orig);
$now = date("U");
$newfile = "/tmp/$now.jpg";
file_put_contents($newfile, $orig);
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "/var/www/tires/dogdog/images/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
echo "<img src=\"images/$now-r.jpg\">";
?>
</body>
</html>
 
Another way using curl:

Code:
<!DOCTYPE html>
<html>
<head>
<title>Dog Dog Cam</title>
</head>
<body>
<?php
$login = 'dookiehead';
$password = 'putsomestuffhereithink';
$url = 'http://10.0.0.65/cgi-bin/snapshot.cgi';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("U");
$newfile = "/tmp/$now.jpg";
file_put_contents($newfile, $orig);
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "/var/www/tires/dogdog/images/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
echo "<img src=\"images/$now-r.jpg\">";
?>
</body>
</html>
?>
 
Last edited:
that first script you put up there just gave me a black page but second one worked. i just have to modify it now to suit what i need to d.
 
that first script you put up there just gave me a black page but second one worked. i just have to modify it now to suit what i need to d.
I realized the first one was for the older Dahua cams where you didn't have to authenticate to get the screenshot. The 2nd allows for digest authentication which the newer Dahua cams need. I originally used the script so I could see how the dogs were doing while we were away. I shrunk the image to save bandwidth on mobile. Once you have the "$result", you can pretty much do anything with it.
 
This should auto refresh every 5 seconds. Change the number 5 in:

Code:
<meta http-equiv="refresh" content="5">

to however many seconds you need refreshed. 1 second may be a little optimistic.

Code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="5">
<title>Dog Dog Cam</title>
</head>
<body>
<?php
$login = 'user';
$password = 'password';
$url = 'http://10.0.0.65/cgi-bin/snapshot.cgi';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("U");
$newfile = "/tmp/$now.jpg";
file_put_contents($newfile, $orig);
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "/var/www/tires/dogdog/images/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
echo "<img src=\"images/$now-r.jpg\">";
?>
</body>
</html>
 
Hi
I try your php script but i have an error, it only display
Code:
"; ?>
on the page
I installed libcurl3 libcurl3-dev and php5-curl on a the web server.
The error maybe come from the last line :
Code:
echo "<img src=\"images/$now-r.jpg\">";
Can you help me to resolve it ?

I added :
Code:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
and rename the script into .php instead of .html and get error:
Code:
Notice: Undefined variable: orig in /var/www/html/cam.php on line 22
and
Code:
Warning: imagejpeg(/var/www/html/images/1512431550-r.jpg): failed to open stream: Permission denied in /var/www/html/cam.php on line 26

Thanks
 
Last edited:
Here my script:
Code:
<?php
$login = 'admin';
$password = 'password';
$url = 'http://Cam_IP/cgi-bin/snapshot.cgi';
header("Content-type: image/jpg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("U");
$newfile = "/tmp/$now.jpg";
file_put_contents($newfile, $now);
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "/tmp/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
echo "<img src=\"/tmp/$now-r.jpg\">";

?>

<!DOCTYPE html>
<HTML>
 <HEAD>
  <TITLE>Cam SnapShot</TITLE>
 </HEAD>

<BODY>
<img src="<?php echo $newnewfile; ?>" alt=""/>
</BODY>
</HTML>
 
Here my script:
Code:
<?php
$login = 'admin';
$password = 'password';
$url = 'http://Cam_IP/cgi-bin/snapshot.cgi';
header("Content-type: image/jpg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("U");
$newfile = "/tmp/$now.jpg";
file_put_contents($newfile, $now);
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "/tmp/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
echo "<img src=\"/tmp/$now-r.jpg\">";

?>

<!DOCTYPE html>
<HTML>
 <HEAD>
  <TITLE>Cam SnapShot</TITLE>
 </HEAD>

<BODY>
<img src="<?php echo $newnewfile; ?>" alt=""/>
</BODY>
</HTML>


Take out these 2 lines, I realized they were from an older version that I copy pasted.

Code:
$newfile = "/tmp/$now.jpg";
file_put_contents($newfile, $now);

You're going to need write permissions in your folder

Code:
/var/www/html/images/

There's a few ways to do that, some easier than others. Check the security implications on the way you decide.
 
Hi
I made some modification on the script to get ride of that permission error
I also remove the 2 lines as you said.
Here my final script:
PHP:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$login = 'admin';
$password = 'password';
$url = 'http://Cam_IP/cgi-bin/snapshot.cgi';
header("Content-type: image/jpg");
header ("Content-type: image/jpeg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($result);
$now = date("U");
$newnew = imagecreatetruecolor(960,540);
imagecopyresized($newnew, $im, 0, 0, 0, 0, 960, 540, 1920, 1080);
$newnewfile = "/tmp/$now-r.jpg";
imagejpeg($newnew, $newnewfile);
imagedestroy($im);
imagedestroy($newnew);
?>
<!DOCTYPE html>
<HTML>
<HEAD>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
<TITLE>Test Cam SnapShot</TITLE>
</HEAD>
<BODY>
<img src="<?php echo $newnewfile; ?>" alt=""/>
</BODY>
</HTML>

The picture is correctly recorded on /tmp.

The error i have now is the image cannot be displayed because it contains errors

Any idea ?
 
Are you using Apache? Apache isn't able to use files from /tmp unless you copy the file to /var/www/html/??? or create a symbolic link from /tmp/$file to /var/www/html/images/???. It's probably easier to create the symbolic link.
 
Thanks for your reply
I'm using nginx on a raspberry pi

I will try to sym link an i will let you know

Edit: i made ln -s /tmp/ /var/www/html/ but same issue
Watching the picture via web browser is ok
 
Last edited:
When i created the symbolic link from /tmp to /var/www/html i get the tmp folder inside the /var/www/html/ folder
and i can see the files in
but cannot display the picture through the browser
 
I made some modification
PHP:
$newnewfile = "/var/www/html/images/$now-r.jpg";
and chown www-data:www-data /var/www/html/images/

I'm able to see the saved screenshot on the folder
but still have the same error displaying on browser
 
You can see the image file created in "/var/www/html/images"?

You'll want to:

If your *.php file is in /var/www/html:

Code:
echo "<img src=\"images/$now-r.jpg\">";

If the php file is in /var/www/html/images:

Code:
echo "<img src=\"$now-r.jpg\">";
 
Last edited: