bsamson Posted April 21, 2008 Share Posted April 21, 2008 Good Afternoon, I found the following script which grabs a still image from an mjpeg video stream. <? // an IP address $dest = "123.123.123.123"; if ($_GET['view']=="1") { $view = ":1024"; } elseif ($_GET['view']=="2") { $view = ":1025"; } $camurl="http://".$dest.$view."/img/video.mjpeg"; $boundary="\n--"; $f = fopen($camurl,"r"); if( ! $f ) { //**** Failed opening the socket if( function_exists( 'imagecreatetruecolor' ) && $img = imagecreatetruecolor( 320, 200 ) ) { // Display an image if we have GD $logo = imagecreatefromgif( "http://www.mydomain.com/backend/cams/img/errorConnecting.jpg" ); $font = 1; header( "Content-type: image/jpeg" ); imagealphablending( $img, 1 ); imagealphablending( $logo, 1 ); $img_w = imagesx( $img ); $img_h = imagesy( $img ); $logo_w = imagesx( $logo ); $logo_h = imagesy( $logo ); $bgc = imagecolorallocate( $img, 255, 255, 255 ); $tc = imagecolorallocate( $img, 0, 0, 0 ); $dc = imagecolorallocate( $img, 255, 0, 0 ); imagefill( $img, 0, 0, $bgc ); imagerectangle( $img, 0, 0, $img_w-1, $img_h-1, $dc ); imageline( $img, 0, 0, $img_w-1, $img_h-1, $dc ); imageline( $img, 0, $img_h-1, $img_w-1, 0, $dc ); imagecopy( $img, $logo, 0, 0, 0, 0, $logo_w, $logo_h ); imagestring( $img, $font, 5, $logo_h, "Error accessing $camurl", $tc ); $date = time(); $date = date( 'Ymd-His (O T)', $date ); imagestring( $img, $font, 5, $logo_h + imagefontheight( $font ) + 1, $date, $tc ); imagejpeg( $img ); imagedestroy( $img ); imagedestroy( $logo ); } else { // Display an error if we do not have GD header( "Content-type: text/html" ); echo "<html><body><h1>Error</h1>Error accessing $camurl"; echo "</body></html>"; } } else { //**** URL OK // Transfer the image... 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); ?> And this works well. However, we have 20+ cams and load time is of concern. What I am looking to do is to adjust this script to save the image NOT display it. Any assistance would be greatly appreciated! Thanks! Quote Link to comment Share on other sites More sharing options...
Zhadus Posted April 21, 2008 Share Posted April 21, 2008 From that script it looks like $frame will be the final image. With that said, you should be able to output that variable to a file and that should accomplish what you are looking for. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted April 21, 2008 Share Posted April 21, 2008 <? // an IP address $dest = "123.123.123.123"; $save_directory = "/"; //change to the directory you want the files saved to $filename = "image_".date("Y_m_d_Gi_s_u").".jpg"; if ($_GET['view']=="1") { $view = ":1024"; } elseif ($_GET['view']=="2") { $view = ":1025"; } $camurl="http://".$dest.$view."/img/video.mjpeg"; $boundary="\n--"; $f = fopen($camurl,"r"); if( ! $f ) { //**** Failed opening the socket if( function_exists( 'imagecreatetruecolor' ) && $img = imagecreatetruecolor( 320, 200 ) ) { // Display an image if we have GD $logo = imagecreatefromgif( "http://www.mydomain.com/backend/cams/img/errorConnecting.jpg" ); $font = 1; header( "Content-type: image/jpeg" ); imagealphablending( $img, 1 ); imagealphablending( $logo, 1 ); $img_w = imagesx( $img ); $img_h = imagesy( $img ); $logo_w = imagesx( $logo ); $logo_h = imagesy( $logo ); $bgc = imagecolorallocate( $img, 255, 255, 255 ); $tc = imagecolorallocate( $img, 0, 0, 0 ); $dc = imagecolorallocate( $img, 255, 0, 0 ); imagefill( $img, 0, 0, $bgc ); imagerectangle( $img, 0, 0, $img_w-1, $img_h-1, $dc ); imageline( $img, 0, 0, $img_w-1, $img_h-1, $dc ); imageline( $img, 0, $img_h-1, $img_w-1, 0, $dc ); imagecopy( $img, $logo, 0, 0, 0, 0, $logo_w, $logo_h ); imagestring( $img, $font, 5, $logo_h, "Error accessing $camurl", $tc ); $date = time(); $date = date( 'Ymd-His (O T)', $date ); imagestring( $img, $font, 5, $logo_h + imagefontheight( $font ) + 1, $date, $tc ); imagejpeg( $img ); imagedestroy( $img ); imagedestroy( $logo ); } else { // Display an error if we do not have GD header( "Content-type: text/html" ); echo "<html><body><h1>Error</h1>Error accessing $camurl"; echo "</body></html>"; } } else { //**** URL OK // Transfer the image... 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); $new_file = fopen($save_directory.$file, "w+"); $write_file = fwrite($save_directory, $frame); fclose($write_file); /* header("Content-type: image/jpeg"); echo $frame; */ } fclose($f); ?> this should do it for you (I hope) Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 21, 2008 Author Share Posted April 21, 2008 Thanks for all the help! However, unfortunately the script doesn't want to save the image. Gives me errors indicating folder not found. Anyway, I also tried to use the IMAGEJPEG function ... but errors indicate that it doesn't recognize the format. Any one have any other suggestions as I am not yet too familiar with working with images in PHP. Thanks in advance!!! -Brian Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 21, 2008 Author Share Posted April 21, 2008 Ok. Here's another approach ... (that isn't working) ... <?php $camurl="http://123.123.123.123:1024/img/video.mjpeg"; $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"); imagejpeg($frame); } fclose($f); ?> This script displays this error: Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/grabStill-2.php on line 29 Any suggestions? Im sorry, but I have not worked much w/ PHP & images. Again, it's all I want to do is to save the frame that is captured. That side of the script works. Thanks in advance for any assistance! Best Regards, Brian Quote Link to comment Share on other sites More sharing options...
craygo Posted April 21, 2008 Share Posted April 21, 2008 change this header("Content-type: image/jpeg"); imagejpeg($frame); to something like this // no need for header info if your not outputting the image to the browser $folder = "path/to/image/folder/"; $filename = "somename.jpg"; imagejpeg($frame, $folder.$filename); Make sure the folder exists. Here is the info for imagejpeg() http://us2.php.net/manual/en/function.imagejpeg.php Ray Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 21, 2008 Author Share Posted April 21, 2008 Appreciate the advice. However, I believe the problem is the fact that the image stored in $frame is not jpeg? That is just a shot in the dark because of this error: Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/grabStill-2.php on line 26 Any advise? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
craygo Posted April 22, 2008 Share Posted April 22, 2008 the image is above that. <?php // an IP address $dest = "123.123.123.123"; if ($_GET['view']=="1") { $view = ":1024"; } elseif ($_GET['view']=="2") { $view = ":1025"; } $camurl="http://".$dest.$view."/img/video.mjpeg"; $boundary="\n--"; $f = fopen($camurl,"r"); if( ! $f ) { //**** Failed opening the socket if( function_exists( 'imagecreatetruecolor' ) && $img = imagecreatetruecolor( 320, 200 ) ) { // Display an image if we have GD $logo = imagecreatefromgif( "http://www.mydomain.com/backend/cams/img/errorConnecting.jpg" ); $font = 1; header( "Content-type: image/jpeg" ); imagealphablending( $img, 1 ); imagealphablending( $logo, 1 ); $img_w = imagesx( $img ); $img_h = imagesy( $img ); $logo_w = imagesx( $logo ); $logo_h = imagesy( $logo ); $bgc = imagecolorallocate( $img, 255, 255, 255 ); $tc = imagecolorallocate( $img, 0, 0, 0 ); $dc = imagecolorallocate( $img, 255, 0, 0 ); imagefill( $img, 0, 0, $bgc ); imagerectangle( $img, 0, 0, $img_w-1, $img_h-1, $dc ); imageline( $img, 0, 0, $img_w-1, $img_h-1, $dc ); imageline( $img, 0, $img_h-1, $img_w-1, 0, $dc ); imagecopy( $img, $logo, 0, 0, 0, 0, $logo_w, $logo_h ); imagestring( $img, $font, 5, $logo_h, "Error accessing $camurl", $tc ); $date = time(); $date = date( 'Ymd-His (O T)', $date ); imagestring( $img, $font, 5, $logo_h + imagefontheight( $font ) + 1, $date, $tc ); $folder = "path/to/image/folder/"; // Add this $filename = "somename.jpg"; // Add this imagejpeg( $img, $folder.$filename); // Modify this imagedestroy( $img ); imagedestroy( $logo ); } else { // Display an error if we do not have GD header( "Content-type: text/html" ); echo "<html><body><h1>Error</h1>Error accessing $camurl"; echo "</body></html>"; } } else { //**** URL OK // Transfer the image... 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); ?> Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 23, 2008 Author Share Posted April 23, 2008 I am a bit lost ... what do you mean image is above that? Thanks in advance. --Brian Quote Link to comment Share on other sites More sharing options...
craygo Posted April 23, 2008 Share Posted April 23, 2008 You did imagejpeg($frame). $frame is not the image, the image is being created above that. I put notes next to the lines to change $folder = "path/to/image/folder/"; // Add this $filename = "somename.jpg"; // Add this imagejpeg( $img, $folder.$filename); // Modify this Ray Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 25, 2008 Author Share Posted April 25, 2008 Craygo, Thanks for the help. However, that part of the script is only ran IF the URL (i.e.: http://11.11.11.11:1024/img/video.mjpeg) is not found. Here is the script w/ that section removed that I just tested and works: <?php $camurl="http://11.11.11.11:1024/img/video.mjpeg"; $boundary="\n--"; $f = fopen($camurl,"r"); //**** URL OK // Transfer the image... 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); ?> Thanks again for the help, any other suggestions? Best, Brian Quote Link to comment Share on other sites More sharing options...
craygo Posted April 25, 2008 Share Posted April 25, 2008 so if I get this right $frame would be the image you are getting or just the name of the image? Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 25, 2008 Author Share Posted April 25, 2008 From what I understand $frame is the image. I have searched and searched for a solution. Thanks in advance for the assistance! Best Regards, Brian Quote Link to comment Share on other sites More sharing options...
craygo Posted April 25, 2008 Share Posted April 25, 2008 Looking at the code, it looks like just the image name. What do you get when you echo $frame. Ray Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 25, 2008 Author Share Posted April 25, 2008 The image, without it nothing is displayed. Quote Link to comment Share on other sites More sharing options...
craygo Posted April 25, 2008 Share Posted April 25, 2008 Can you right click on the page and get the page source and post the section for the image. Ray Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 25, 2008 Author Share Posted April 25, 2008 I just copied the script to my personal domain. Here is the URL: http://www.nnylocal.com/phpFreaks/grabStill-3.php. This is a working cam in one of our stores ... Thanks! Best Regards, Brian Samson Quote Link to comment Share on other sites More sharing options...
craygo Posted April 25, 2008 Share Posted April 25, 2008 Looks like you will have to do something like this. <?php $size = getimagesize($frame); $img_src = imagecreatefromjpeg($frame); $img_dst=imagecreatetruecolor($size[0],$size[1]); imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $size[0], $size[1], $size[0], $size[1]); $folder = "path/to/image/folder/"; $filename = "somename.jpg"; imagejpeg($img_dst, $folder.$filename); imagedestroy($img_dst); ?> Other than that you would have to make a different page with the code above it in it and use grabStill-3.php as the source file. So if you hade to do that just add $frame = "grabStill-3.php"; to the top of the above code Ray Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 25, 2008 Author Share Posted April 25, 2008 Ok ... using this script: <?php $camurl="http://111.111.111.111:1024/img/video.mjpeg"; $boundary="\n--"; $f = fopen($camurl,"r"); //**** URL OK // Transfer the image... 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); $size = getimagesize($frame); $img_src = imagecreatefromjpeg($frame); $img_dst=imagecreatetruecolor($size[0],$size[1]); imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $size[0], $size[1], $size[0], $size[1]); $folder = "/home/nnyserve/www/backend/cams/scripts/stills/"; $filename = "somename.jpg"; imagejpeg($img_dst, $folder.$filename); imagedestroy($img_dst); // header("Content-type: image/jpeg"); // echo $frame; fclose($f); ?> I received these errors: Warning: getimagesize(ÿØÿÛ) [function.getimagesize]: failed to open stream: No such file or directory in /home/nnyserve/public_html/backend/cams/scripts/grabStill-4.php on line 17 Warning: imagecreatefromjpeg(ÿØÿÛ) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/nnyserve/public_html/backend/cams/scripts/grabStill-4.php on line 18 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/nnyserve/public_html/backend/cams/scripts/grabStill-4.php on line 19 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/grabStill-4.php on line 20 Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/grabStill-4.php on line 23 Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/grabStill-4.php on line 24 LOL ... Thanks for all the help thus far Craygo ... but any other idea's? Best regards, Brian Samson Quote Link to comment Share on other sites More sharing options...
craygo Posted April 26, 2008 Share Posted April 26, 2008 Well all it takes is the first one to error and the rest just follow. Did you try puting the code I did above in it's own file and call the script which creates the image file called saveimages.php <?php $frame = "grabStill-3.php"; $size = getimagesize($frame); $img_src = imagecreatefromjpeg($frame); $img_dst=imagecreatetruecolor($size[0],$size[1]); imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $size[0], $size[1], $size[0], $size[1]); $folder = "/home/nnyserve/www/backend/cams/scripts/stills/"; $filename = "somename.jpg"; imagejpeg($img_dst, $folder.$filename); imagedestroy($img_dst); ?> Now instead of browsing to grabStill-3 go to saveimages.php Ray Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 27, 2008 Author Share Posted April 27, 2008 Ok ... Just tried your scripts ... and now this: Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home/nnyserve/public_html/backend/cams/scripts/imgTest.php on line 4 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'grabStill-3.php' is not a valid JPEG file in /home/nnyserve/public_html/backend/cams/scripts/imgTest.php on line 4 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/nnyserve/public_html/backend/cams/scripts/imgTest.php on line 5 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/imgTest.php on line 6 Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/imgTest.php on line 9 Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/nnyserve/public_html/backend/cams/scripts/imgTest.php on line 10 I just dont understand PHP & images ... lol ... thanks for all your help though! Any other suggestions?? Thanks! Best Regards, Brian Quote Link to comment Share on other sites More sharing options...
bsamson Posted April 29, 2008 Author Share Posted April 29, 2008 Anyone have any other suggestions? Thanks in advance! Best Regards, Brian Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.