Permalink
Browse files

Don’t create Image objects for unsupported types

Check with the return value of GD’s imagetypes() function.
  • Loading branch information...
sabberworm committed Oct 30, 2012
1 parent 881faba commit 86f2765b62b4c2085f7c4225cc2b1f5e071f973e
Showing with 40 additions and 10 deletions.
  1. +16 −9 lib/model/Document.php
  2. +23 −0 lib/model/DocumentType.php
  3. +1 −1 modules/file/display_document/DisplayDocumentFileModule.php
@@ -40,6 +40,10 @@ public function isImage() {
return $this->getDocumentType()->isImageType();
}
public function isGDImage() {
return $this->getDocumentType()->isGDImageType();
}
public function getDisplayUrl($aUrlParameters = array(), $sFileModule = 'display_document') {
return LinkUtil::link(array($sFileModule, $this->getId()), "FileManager", $aUrlParameters);
}
@@ -70,7 +74,7 @@ public function renderListItem($oTemplate, $aUrlParams = array()) {
if($oTemplate->hasIdentifier('size')) {
$oTemplate->replaceIdentifier("size", DocumentPeer::getDocumentSize($this->getDataSize(), 'kb'));
}
if($this->isImage() && $oTemplate->hasIdentifier('dimension', Template::$ANY_VALUE)) {
if($this->isGDImage() && $oTemplate->hasIdentifier('dimension', Template::$ANY_VALUE)) {
$oImage = $this->getImage();
$oTemplate->replaceIdentifier('dimension', $oImage->getOriginalWidth(), 'width');
$oTemplate->replaceIdentifier('dimension', $oImage->getOriginalHeight(), 'height');
@@ -124,16 +128,19 @@ public function getImage() {
}
public function getDimensionsIfImage($sPostfix = "px") {
if(!$this->isImage()) {
if(!$this->isGDImage()) {
return null;
}
$oImage = $this->getImage();
if($oImage && $oImage->getOriginalHeight()) {
$aResult = array();
$aResult[] = $oImage->getOriginalWidth();
$aResult[] = $oImage->getOriginalHeight();
return implode('x', $aResult).$sPostfix;
}
try {
$oImage = $this->getImage();
if($oImage && $oImage->getOriginalHeight()) {
$aResult = array();
$aResult[] = $oImage->getOriginalWidth();
$aResult[] = $oImage->getOriginalHeight();
return implode('x', $aResult).$sPostfix;
}
} catch(Exception $ex) {} //Ignore unrecognized image format
return null;
}
@@ -10,6 +10,29 @@ public function isImageType($sType = '') {
return StringUtil::startsWith($this->getMimetype(), "image/$sType");
}
/**
* Return true if the document is an image and is supported by the installed GD lib.
*/
public function isGDImageType() {
if(!$this->isImageType()) {
return false;
}
$sType = $this->getDocumentKindDetail();
$iTypes = imagetypes();
if($sType === 'vnd.wap.wbmp') {
return ($iTypes & IMG_WBMP) === IMG_WBMP;
}
if($sType === 'x-xpixmap' || $sType === 'image/x-xpm') {
return ($iTypes & IMG_XMP) === IMG_XMP;
}
$sType = strtoupper("IMG_$sType");
if(defined($sType)) {
$iTypeVal = constant($sType);
return ($iTypes & $iTypeVal) === $iTypeVal;
}
return false;
}
public function getDocumentKind() {
$aResult = explode('/', $this->getMimeType());
return $aResult[0];
@@ -29,7 +29,7 @@ protected function isAuthenticated() {
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->getDocumentKind() != 'image') {
if(!$this->oDocument->isGDImage()) {
$mMaxWidth = 'full';
$mMaxHeight = 'full';
}

0 comments on commit 86f2765

Please sign in to comment.