這是一個為了防止cross-site問題搞得程式碼,把別人的mjpeg串流重新擷取再送一次,這樣就變成自己伺服器輸出的串流了,當然也可以當作防止別人知道原始串流來源的一個方式?
<?php
function get_one_jpeg(){
$camurl="串流"; // Mjpeg URL
$f = fopen($camurl, "r");
$r = "";
if($f) {
// unless 1 frame raw read.
while (substr_count($r, "\xFF\xD8")<2) $r.=fread($f, 8192);
}
// clip frame out raw
$start = strpos($r, "\xFF\xD8");
$end = strpos($r, "\xFF\xD9", $start)+2;
$frame = substr($r, $start, $end-$start);
return $frame;
}
# Used to separate multipart
$boundary = "my_mjpeg";
# We start with the standard headers. PHP allows us this much
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");
# From here out, we no longer expect to be able to use the header() function
print "--$boundary\n";
# Set this so PHP doesn't timeout during a long stream
set_time_limit(0);
# Disable Apache and PHP's compression of output to the client
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
# Set implicit flush, and flush all current buffers
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);
# The loop, producing one jpeg frame per iteration
while (true) {
# Per-image header, note the two new-lines
print "Content-type: image/jpeg\n\n";
# Your function to get one jpeg image
print get_one_jpeg();
# The separator
print "--$boundary\n";
}
?>