Redistributing images from a network camera

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
I've been working on a network camera project at work and will be starting one at home as soon as my network camera arrives.

My current set up is as follows:

Cisco IP Phone requests XML and images from a web server. The web server must first download a copy of the image from the network camera and save it to the disk. Then the recently downloaded jpg is resized/converted to a png using ImageMagick. After this process is done, PHP reads the contents of the newly created png and echos it out using the correct mime/type.

Sometimes the Cisco IP Phone displays a blank gray image. The phone still successfully connects to the web server via HTTP, but the content being returned is corrupted in some way. I believe this may have something to do with buffering the outputted image. It seems like the phone isn't completely waiting on the PHP script to echo the contents of the png.

Any other ideas on how to reduce the occurrence of errors?

Also, is there anyway to use ImageMagick without "files"? I think it would be much more efficient to skip the saving and deleting of the jpg and png.

The ImageMagick command I'm using right now is "convert file.jpg file.png". I'm wondering if there is something like "convert 'contents of a jpg' -filetype png" and then it just echos out the content of a png file.

Fortunately at home, I'll have no need to convert (and hopefully resize) images on the web server since I'll be using a web browser rather than a phone with no jpg support.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,453
4,289
75
I'm not sure about the gray image; but at least some temp files can definitely be gotten rid of. I haven't done this in PHP specifically, but it looks like you could do:

$handle = popen('wget -O- http://the.image.jpg', 'r');

to get a handle to read the image into memory. Then read the image into $jpgimage and close the handle. Avoiding the next temp file is a little harder:

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';
$env = array();

$process = proc_open('convert - png:-', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt

fwrite($pipes[0], $jpgimage);
fclose($pipes[0]);

// Here, echo the png to the client with the proper mime/type.
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);

// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
}

Edit: There's also the ImageMagick PHP API; but the documentation on it is sparse.
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
Thanks for the help Ken. I think they blank image may be a phone issue rather than a server issue. I've recreated the problem by telling the phone to pull a static image, and it's still bugging out every once in a while.