Skip to content

Commit

Permalink
fix(themeing): Add error handling to ImageManager
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
  • Loading branch information
susnux authored and backportbot-nextcloud[bot] committed Jan 18, 2024
1 parent 00db3c2 commit a2a8555
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,32 +234,50 @@ public function updateImage(string $key, string $tmpFile): string {
}

if ($key === 'background' && $this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) {
// Optimize the image since some people may upload images that will be
// either to big or are not progressive rendering.
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));

// Preserve transparency
imagesavealpha($newImage, true);
imagealphablending($newImage, true);

$tmpFile = $this->tempManager->getTemporaryFile();
$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
$outputImage = imagescale($newImage, $newWidth, $newHeight);

imageinterlace($outputImage, 1);
if (strpos($detectedMimeType, 'image/jpeg') !== false) {
imagejpeg($outputImage, $tmpFile, 90);
} else {
imagepng($outputImage, $tmpFile, 8);
}
imagedestroy($outputImage);
try {
// Optimize the image since some people may upload images that will be
// either to big or are not progressive rendering.
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));
if ($newImage === false) {
throw new \Exception('Could not read background image, possibly corrupted.');
}

$target->putContent(file_get_contents($tmpFile));
} else {
$target->putContent(file_get_contents($tmpFile));
// Preserve transparency
imagesavealpha($newImage, true);
imagealphablending($newImage, true);

$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));

Check notice

Code scanning / Psalm

PossiblyFalseOperand Note

Left operand cannot be falsable, got false|int

Check notice

Code scanning / Psalm

PossiblyFalseOperand Note

Left operand cannot be falsable, got false|int
$outputImage = imagescale($newImage, $newWidth, $newHeight);
if ($outputImage === false) {
throw new \Exception('Could not scale uploaded background image.');
}

$newTmpFile = $this->tempManager->getTemporaryFile();
imageinterlace($outputImage, 1);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of imageinterlace expects resource, possibly different type resource|true provided
// Keep jpeg images encoded as jpeg
if (strpos($detectedMimeType, 'image/jpeg') !== false) {
if (!imagejpeg($outputImage, $newTmpFile, 90)) {

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of imagejpeg expects resource, possibly different type resource|true provided
throw new \Exception('Could not recompress background image as JPEG');
}
} else {
if (!imagepng($outputImage, $newTmpFile, 8)) {

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of imagepng expects resource, possibly different type resource|true provided
throw new \Exception('Could not recompress background image as PNG');
}
}
$tmpFile = $newTmpFile;
imagedestroy($outputImage);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of imagedestroy expects resource, possibly different type resource|true provided
} catch (\Exception $e) {
if (is_resource($outputImage) || $outputImage instanceof \GdImage) {

Check notice

Code scanning / Psalm

DocblockTypeContradiction Note

Cannot resolve types for $outputImage - docblock-defined type resource|true does not contain GdImage

Check notice

Code scanning / Psalm

PossiblyUndefinedVariable Note

Possibly undefined variable $outputImage defined in try block

Check notice

Code scanning / Psalm

PossiblyUndefinedVariable Note

Possibly undefined variable $outputImage defined in try block

Check notice

Code scanning / Psalm

DocblockTypeContradiction Note

Cannot resolve types for $outputImage - docblock-defined type true does not contain GdImage
imagedestroy($outputImage);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of imagedestroy expects resource, possibly different type GdImage|resource provided
}

$this->logger->debug($e->getMessage());

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\ILogger::debug has been marked as deprecated
}
}

$target->putContent(file_get_contents($tmpFile));

return $detectedMimeType;
}

Expand Down

0 comments on commit a2a8555

Please sign in to comment.