Created
May 5, 2015 17:40
-
-
Save nitzel/822e13bf52519cb77a00 to your computer and use it in GitHub Desktop.
for a school project, makes a remote server take a shot with a webcam, copies it to the local server and transfers it to the client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Overwrites the local copy of an image after at least TIME_DELTA seconds | |
// Images are first captures by camera (HEAD request to other website) | |
// and then copied here from the other server. | |
// Next they are transfered to the user as an image, so you can treat this PHP-File as an image. | |
// | |
// By Jan Schnitker, 2015, published under the UNLICENSE, read about it here: http://unlicense.org/ | |
// send the right headers | |
header("Content-Type: image/jpg"); // transmit as jpg | |
//header("Content-Length: " . filesize($localImage)); (could be done later, but not necessary) | |
$TIME_DELTA = 5; // get new image after at least 5 seconds | |
$imgNos = [188,189]; | |
$imgNo = $imgNos[0]; // default value: first image | |
// if given by GET, extract the image id the user wants to get (...php?i=X), X is the ID | |
// then we take the X-th element from the array $imgNos, and thats our image number :) | |
if ( isset($_GET["i"]) ) { | |
$i = intval($_GET["i"]); // this is the number given by GET, eq: ....php?i=5 would make i=5 | |
if( $i>=0 and $i<count($imgNos) ) { // not exceeding the array | |
$imgNo = $imgNos[$i]; // set imgNo to the corresponding image number | |
} | |
} | |
// construct filenames/urls | |
$urlUpdate = "http://it-projekt-muenster.de/images/ip-cam".$imgNo.".php"; | |
$urlImage = "http://it-projekt-muenster.de/images/".$imgNo."/webcam.jpg"; | |
$localImage = "./".$imgNo.".jpg"; | |
// if the file-time is greater or equal to 5seconds, we will request a new picture from the webcam and download it | |
if( time() - filemtime ( $localImage ) >= $TIME_DELTA ) { | |
// send request to update the image on serverside | |
$ch = curl_init(); // create curl connection | |
curl_setopt($ch, CURLOPT_URL, $urlUpdate); // set url | |
curl_setopt($ch, CURLOPT_NOBODY, true); // dont wanna have the body | |
curl_setopt($ch, CURLOPT_HTTPHEADER, []); // set: header only | |
$resultHead = curl_exec($ch); // execute request (connect, send, receive) | |
curl_close($ch); // close connection | |
// download new image from webcam | |
$resultCopy = file_put_contents( $localImage, file_get_contents( $urlImage )); | |
} | |
// either way, open the file and stream it as an image to the user | |
// open the file in a binary mode | |
$fp = fopen($localImage, 'rb'); | |
// dump the picture and stop the script | |
fpassthru($fp); | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment