Permalink
Browse files

Gracefully handle unsupported images

Closes #139 as fixed
  • Loading branch information...
sabberworm committed Oct 30, 2012
1 parent 86f2765 commit 2ae6ebf5979ba92505dade58d127fbaeaa4508a9
Showing with 27 additions and 19 deletions.
  1. +5 −1 lib/classes/Image.php
  2. +22 −18 modules/file/display_document/DisplayDocumentFileModule.php
@@ -263,7 +263,11 @@ public static function textSize($sFontFilePath, $sText, $iFontSize) {
}
public static function imageFromData($sImageData) {
return new Image(imagecreatefromstring($sImageData));
$rImageResource = @imagecreatefromstring($sImageData);
if(!$rImageResource) {
throw new Exception("imagecreatefromstring: Unrecognized image format");
}
return new Image($rImageResource);
}
public static function imageFromStream($rImageResource) {
@@ -34,6 +34,7 @@ public function renderFile() {
$mMaxHeight = 'full';
}
$sCacheString = 'doc_'.$this->oDocument->getId().'_'.$mMaxWidth.'x'.$mMaxHeight.(isset($_REQUEST['add_text']) ? '_'.$_REQUEST['add_text'] : "");
$oCache = new Cache($sCacheString, DIRNAME_IMAGES);
@@ -52,25 +53,28 @@ public function renderFile() {
$rDataStream = $this->oDocument->getData();
if(is_int($mMaxWidth) || is_int($mMaxHeight)) {
$oImage = Image::imageFromData(stream_get_contents($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);
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());

0 comments on commit 2ae6ebf

Please sign in to comment.