public function renderFile()
 {
     $mMaxWidth = is_numeric(@$_REQUEST['max_width']) ? (int) $_REQUEST['max_width'] : 'full';
     $mMaxHeight = is_numeric(@$_REQUEST['max_height']) ? (int) $_REQUEST['max_height'] : 'full';
     if (!$this->oDocument->isGDImage()) {
         $mMaxWidth = 'full';
         $mMaxHeight = 'full';
     }
     $sCacheString = 'doc_' . $this->oDocument->getId() . '_' . $mMaxWidth . 'x' . $mMaxHeight . (isset($_REQUEST['add_text']) ? '_' . $_REQUEST['add_text'] : "");
     $oCache = new Cache($sCacheString, DIRNAME_IMAGES);
     $sDisplay = "inline";
     if (isset($_REQUEST['download']) && $_REQUEST['download'] == "true") {
         $sDisplay = "attachment";
     }
     header('Content-Disposition: ' . $sDisplay . ';filename="' . $this->oDocument->getFullName() . '"');
     //Don’t base the last-modified off the cache but rather off the document’s updated-at.
     LinkUtil::sendCacheControlHeaders($this->oDocument, $oCache);
     if ($oCache->entryExists() && !$oCache->isOlderThan($this->oDocument)) {
         header("Content-Type: " . $this->oDocument->getDocumentType()->getMimetype());
         $oCache->passContents(true);
         exit;
     }
     $rDataStream = $this->oDocument->getData();
     try {
         if (is_int($mMaxWidth) || is_int($mMaxHeight)) {
             $oImage = Image::imageFromStream($rDataStream);
             if (is_int($mMaxWidth) && is_int($mMaxHeight)) {
                 $oImage->setSize($mMaxWidth, $mMaxHeight, Image::RESIZE_TO_SMALLER_VALUE);
             } else {
                 if (is_int($mMaxWidth)) {
                     $oImage->setSize($mMaxWidth, 0, Image::RESIZE_TO_WIDTH);
                 } else {
                     $oImage->setSize(0, $mMaxHeight, Image::RESIZE_TO_HEIGHT);
                 }
             }
             //Since $bDontBlowUp is true, do a preliminary check whether it’s necessary to even use the image class
             if ($oImage->getScalingFactor() < 1.0) {
                 $oImage->setFileType($this->oDocument->getDocumentType()->getExtension());
                 $oImage->render(true, null, $oCache);
                 exit;
             } else {
                 //Free up space
                 $oImage->destroy();
                 rewind($rDataStream);
             }
         }
     } catch (Exception $ex) {
     }
     //Ignore unrecognized image format
     header("Content-Type: " . $this->oDocument->getDocumentType()->getMimetype());
     header("Content-Length: " . $this->oDocument->getDataSize());
     $oCache->setContents(stream_get_contents($rDataStream));
     rewind($rDataStream);
     fpassthru($rDataStream);
 }
 public function renderFile()
 {
     $sCacheString = 'preview_' . $this->oDocumentType->getId() . '_' . $this->iSize;
     $oCache = new Cache($sCacheString, DIRNAME_IMAGES);
     LinkUtil::sendCacheControlHeaders($this->oDocumentType, $oCache);
     if ($oCache->entryExists() && !$oCache->isOlderThan($this->oDocumentType)) {
         header("Content-Type: " . self::MIME_TYPE);
         $oCache->passContents(true);
         exit;
     }
     $sFileName = "{$this->oDocumentType->getDocumentKind()}.png";
     $sFilePath = ResourceFinder::findResource(array(DIRNAME_MODULES, self::getType(), $this->getModuleName(), ResourceIncluder::RESOURCE_TYPE_ICON, $sFileName));
     if ($sFilePath === null) {
         $sFileName = 'default.png';
     }
     $sFilePath = ResourceFinder::findResource(array(DIRNAME_MODULES, self::getType(), $this->getModuleName(), ResourceIncluder::RESOURCE_TYPE_ICON, $sFileName));
     if ($sFilePath === null) {
         throw new Exception("Error in DocumentTypePreviewFileModule->renderFile: type has unknown kind: {$this->oDocumentType->getDocumentKind()}");
     }
     $sFontFilePath = ResourceFinder::findResource(array(DIRNAME_MODULES, self::getType(), $this->getModuleName(), 'PTS55F.ttf'));
     $oImage = Image::imageFromPath($sFilePath);
     if (Image::supportsText()) {
         $sText = strtoupper($this->oDocumentType->getExtension());
         $fFontSize = 72.0;
         $aSize = Image::textSize($sFontFilePath, $sText, $fFontSize);
         $iDesiredWidth = 300;
         $iDesiredHeight = 145;
         $fWidthRatio = $iDesiredWidth / $aSize[0];
         $fHeightRatio = $iDesiredHeight / $aSize[1];
         $fRatio = min($fWidthRatio, $fHeightRatio);
         $fFontSize *= $fRatio;
         $iDesiredPositionX = 107 - 7 * $fRatio;
         $iDesiredPositionY = 338;
         $iStartPositionX = $iDesiredPositionX + ($iDesiredWidth - $aSize[0] * $fRatio) / 2;
         $iStartPositionY = $iDesiredPositionY + $aSize[1] * $fRatio + ($iDesiredHeight - $aSize[1] * $fRatio) / 2;
         $oImage->addText($sFontFilePath, $sText, 1, $fFontSize, 150, 150, 150, $iStartPositionX, $iStartPositionY);
     }
     if ($this->iSize < 512) {
         $oImage->setSize($this->iSize, $this->iSize, Image::STRETCH);
     }
     $oImage->setFileType('png');
     $oImage->render(true, null, $oCache);
     exit;
 }
Exemple #3
0
 /**
  * Sends the cache control headers Last-Modified and ETag
  * Uses the timestamp of the cache file as base for calculation.
  * Additionally, this method exits if the client sent a matching If-None-Match or If-Modified-Since header
  * You can call this method twice if you created a new cache file and don’t have any other timestamp. It will only output the headers once.
  * @param $iTimestamp deprecated: to use this method without a cache file, call LinkUtil::sendCacheControlHeaders directly
  */
 public function sendCacheControlHeaders($iTimestamp = null)
 {
     if ($iTimestamp !== null) {
         LinkUtil::sendCacheControlHeaders($iTimestamp);
     } else {
         LinkUtil::sendCacheControlHeadersForCache($this);
     }
 }