Jump to content

Image Manipulation ...


bsamson

Recommended Posts

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!

Link to comment
Share on other sites

<?

// 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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);

?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.