Skip to content

Commit

Permalink
[FIX] Fortify Thumbnail Creation
Browse files Browse the repository at this point in the history
Avoid errors because of missing files or division by zero.
  • Loading branch information
ThomasWeinert committed Nov 11, 2020
1 parent 8375b8d commit dc73c71
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/system/Papaya/Graphics/GD/GDLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ public function load($fileName) {
* @return int
*/
public function identifyType($fileName) {
list(, , $type) = getimagesize($fileName);
return $type;
list(, , $type) = @getimagesize($fileName);
return $type ?: -1;
}

public function getImageSize($fileName) {
list($width, $height) = getimagesize($fileName);
return [$width, $height];
list($width, $height) = @getimagesize($fileName);
return [(int)$width, (int)$height];
}

/**
Expand Down
30 changes: 23 additions & 7 deletions src/system/Papaya/Media/Thumbnail/Calculation/Contain.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,31 @@ public function getIdentifier() {
return Calculation::MODE_FIX.'_'.$targetSize[0].'x'.$targetSize[1];
}

private function getThumbnailSize() {
$factorX = $this->_width / $this->_maximumWidth;
$factorY = $this->_height / $this->_maximumHeight;
$thumbnailWidth = $this->_maximumWidth;
$thumbnailHeight = $this->_maximumHeight;
if ($factorX >= $factorY) {
private function getThumbnailSize()
{
if (
($this->_width < 1 || $this->_height < 1) ||
($this->_maximumWidth < 1 && $this->_maximumHeight < 1)
) {
return [$this->_width, $this->_height];
} elseif ($this->_maximumHeight < 1) {
$factorX = $this->_width / $this->_maximumWidth;
$thumbnailWidth = $this->_maximumWidth;
$thumbnailHeight = (int)round($this->_height / $factorX);
} else {
} elseif ($this->_maximumWidth < 1) {
$factorY = $this->_height / $this->_maximumHeight;
$thumbnailHeight = $this->_maximumHeight;
$thumbnailWidth = (int)round($this->_width / $factorY);
} else {
$factorX = $this->_width / $this->_maximumWidth;
$factorY = $this->_height / $this->_maximumHeight;
$thumbnailWidth = $this->_maximumWidth;
$thumbnailHeight = $this->_maximumHeight;
if ($factorX >= $factorY) {
$thumbnailHeight = (int)round($this->_height / $factorX);
} else {
$thumbnailWidth = (int)round($this->_width / $factorY);
}
}
return [$thumbnailWidth, $thumbnailHeight];
}
Expand Down
9 changes: 9 additions & 0 deletions src/system/Papaya/Media/Thumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public function createCalculation($targetWidth, $targetHeight, $mode = Calculati
public function createThumbnail(Calculation $calculation, $mimeType = NULL, $useCache = TRUE) {
$gd = $this->gd();
$sourceFileName = $this->getSourceFileName();
if (!$this->fileSystem()->getFile($sourceFileName)->isReadable()) {
$this->logError(
Message::SEVERITY_ERROR,
sprintf(
'Can not create thumbnail, image file not found: %s', $sourceFileName
)
);
return NULL;
}
if (NULL ===$mimeType) {
$mimeType = $gd->identifyType($sourceFileName);
}
Expand Down

0 comments on commit dc73c71

Please sign in to comment.