Beispiel #1
0
 /**
  * Render the image to the output.
  */
 public function render()
 {
     if (!$this->media || !$this->media->canShow()) {
         Log::addMediaLog('Image Builder error: >' . I18N::translate('Missing or private media object.'));
         $this->renderError();
     }
     $serverFilename = $this->media->getServerFilename();
     if (!file_exists($serverFilename)) {
         Log::addMediaLog('Image Builder error: >' . I18N::translate('The media object does not exist.') . '< for path >' . $serverFilename . '<');
         $this->renderError();
     }
     $mimetype = $this->media->mimeType();
     $imgsize = $this->media->getImageAttributes();
     $filetime = $this->media->getFiletime();
     $filetimeHeader = gmdate('D, d M Y H:i:s', $filetime) . ' GMT';
     $expireHeader = gmdate('D, d M Y H:i:s', WT_TIMESTAMP + $this->getExpireOffset()) . ' GMT';
     $type = Functions::isImageTypeSupported($imgsize['ext']);
     $usewatermark = false;
     // if this image supports watermarks and the watermark module is intalled...
     if ($type) {
         $usewatermark = $this->isShowWatermark();
     }
     // determine whether we have enough memory to watermark this image
     if ($usewatermark) {
         if (!FunctionsMedia::hasMemoryForImage($serverFilename)) {
             // not enough memory to watermark this file
             $usewatermark = false;
         }
     }
     $etag = $this->media->getEtag();
     // parse IF_MODIFIED_SINCE header from client
     $if_modified_since = 'x';
     if (!empty(Filter::server('HTTP_IF_MODIFIED_SINCE'))) {
         $if_modified_since = preg_replace('/;.*$/', '', Filter::server('HTTP_IF_MODIFIED_SINCE'));
     }
     // parse IF_NONE_MATCH header from client
     $if_none_match = 'x';
     if (!empty(Filter::server('HTTP_IF_NONE_MATCH'))) {
         $if_none_match = str_replace('"', '', Filter::server('HTTP_IF_NONE_MATCH'));
     }
     // add caching headers.  allow browser to cache file, but not proxy
     header('Last-Modified: ' . $filetimeHeader);
     header('ETag: "' . $etag . '"');
     header('Expires: ' . $expireHeader);
     header('Cache-Control: max-age=' . $this->getExpireOffset() . ', s-maxage=0, proxy-revalidate');
     // if this file is already in the user’s cache, don’t resend it
     // first check if the if_modified_since param matches
     if ($if_modified_since === $filetimeHeader) {
         // then check if the etag matches
         if ($if_none_match === $etag) {
             http_response_code(304);
             return;
         }
     }
     // send headers for the image
     header('Content-Type: ' . $mimetype);
     header('Content-Disposition: filename="' . addslashes(basename($this->media->getFilename())) . '"');
     if ($usewatermark) {
         // generate the watermarked image
         $imCreateFunc = 'imagecreatefrom' . $type;
         $imSendFunc = 'image' . $type;
         if (function_exists($imCreateFunc) && function_exists($imSendFunc)) {
             $im = $imCreateFunc($serverFilename);
             $im = $this->applyWatermark($im);
             // send the image
             $imSendFunc($im);
             imagedestroy($im);
             return;
         } else {
             // this image is defective.  log it
             Log::addMediaLog('Image Builder error: >' . I18N::translate('This media file is broken and cannot be watermarked.') . '< in file >' . $serverFilename . '< memory used: ' . memory_get_usage());
         }
     }
     // determine filesize of image (could be original or watermarked version)
     $filesize = filesize($serverFilename);
     // set content-length header, send file
     header('Content-Length: ' . $filesize);
     // Some servers disable fpassthru() and readfile()
     if (function_exists('readfile')) {
         readfile($serverFilename);
     } else {
         $fp = fopen($serverFilename, 'rb');
         if (function_exists('fpassthru')) {
             fpassthru($fp);
         } else {
             while (!feof($fp)) {
                 echo fread($fp, 65536);
             }
         }
         fclose($fp);
     }
 }