Example #1
0
 private function handleRequest($offset, $time)
 {
     Log::SetPrefix(sprintf("Thumbnail(%d): ", getmypid()));
     $hash = $this->getHash();
     if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $hash) {
         header(getenv("SERVER_PROTOCOL") . " 304 Not Modified");
         header("Content-Length: 0");
         exit;
     }
     $cachedir = CACHE_DIR;
     umask(077);
     if (!is_dir($cachedir)) {
         Log::Info("Creating cache directory %s", $cachedir);
         @mkdir($cachedir);
     }
     $file = $cachedir . '/' . $this->getHash() . '-' . $offset;
     if (is_file($file)) {
         $lm = filemtime($file);
         header("Content-Type: image/jpeg");
         header("Etag: \"" . $this->getHash() . "-" . $offset . "\"");
         header("Last-Modified: " . gmdate('D, d M Y H:i:s T', $lm));
         header("Expires: " . gmdate('D, d M Y H:i:s T', $lm + 86400));
         if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER) && $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
             header(getenv("SERVER_PROTOCOL") . " 304 Not Modified");
             header("Content-Length: 0");
             Log::Debug("request already satisfied by HTTP_IF_MODIFIED_SINCE header, file=%s", $file);
             exit;
         }
         echo file_get_contents($file);
         exit;
     }
     $outdir = $cachedir . '/' . getmypid();
     if (!is_dir($outdir)) {
         Log::Debug("Creating work directory %s", $outdir);
         mkdir($outdir, 0700);
     }
     $tempfile = new TempFile(".ts");
     $infile = new File($this->stream, false);
     $infile->seek($offset);
     $data = $infile->read(1024 * 1024);
     $infile->close();
     $tempfile->write($data);
     $cmd = sprintf("%s -nolirc -really-quiet -zoom -quiet -xy %d -vo jpeg:outdir=%s:maxfiles=2 -ao null %s -frames 2 &> /dev/null", MPLAYER, $this->width(), $outdir, escapeshellarg($tempfile->Filename()));
     $ts = microtime(true);
     Log::Debug("command=%s", $cmd);
     exec($cmd);
     $elapsed = microtime(true) - $ts;
     Log::Debug("command executed, duration=%.6f sec", $elapsed);
     $tempfile = $this->chooseBestImage($outdir);
     Log::Debug("using image %s", $tempfile);
     if (!is_file($tempfile)) {
         Log::Error("command failed, command was %s", $cmd);
         header("HTTP/1.0 404 not found");
         exit;
     }
     $im = @imagecreatefromjpeg($tempfile);
     $timestring = sprintf("%02d:%02d:%02d", floor($time / 3600), floor($time % 3600 / 60), $time % 60);
     $this->writeTextAligned($im, self::ALIGN_LEFT, self::ALIGN_TOP, $timestring);
     ob_start();
     imagejpeg($im, '', 60);
     $data = ob_get_contents();
     ob_end_clean();
     $this->cleanDirectory($outdir);
     if ($data != '') {
         Log::Debug("finished generation, Size=%d", strlen($data));
         header("Content-Type: image/jpeg");
         file_put_contents($file, $data);
         $lm = filemtime($file);
         header("Last-Modified: " . gmdate('D, d M Y H:i:s T', $lm));
         header("Etag: \"" . $this->getHash() . "-" . $offset . "\"");
         header("Expires: " . gmdate('D, d M Y H:i:s T', $lm + 86400));
     } else {
         Log::Error("oops, data is empty, should not happen");
     }
     print $data;
     exit;
 }