From aafdbb5e1fa33e16a8a111a7688df8fb5f6d487a Mon Sep 17 00:00:00 2001 From: Ingolf Steinhardt Date: Mon, 28 Oct 2024 21:13:25 +0100 Subject: [PATCH] Add check for brocken image files --- src/Helper/ToolboxFile.php | 64 +++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/Helper/ToolboxFile.php b/src/Helper/ToolboxFile.php index f9d2a7296..47745bad7 100644 --- a/src/Helper/ToolboxFile.php +++ b/src/Helper/ToolboxFile.php @@ -1115,14 +1115,25 @@ private function processFile($fileName) // Prepare GD images. if ($information['isGdImage'] = $file->isGdImage) { - $information['src'] = \urldecode($this->resizeImage($fileName)); + try { + $information['src'] = urldecode($this->resizeImage($fileName)); + } catch (\Throwable $exception) { + // Broken image, keep original path. + $information['src'] = urldecode($fileName); + $information['isGdImage'] = false; + } + $information['lb'] = 'lb_' . $this->getLightboxId(); - if (\file_exists($this->rootDir . '/' . $information['src'])) { - $size = \getimagesize($this->rootDir . '/' . $information['src']); + + if ( + file_exists($this->rootDir . '/' . $information['src']) + && (false !== ($size = getimagesize($this->rootDir . '/' . $information['src']))) + ) { $information['w'] = $size[0]; $information['h'] = $size[1]; $information['wh'] = $size[3]; } + $information['imageUrl'] = $fileName; } @@ -1133,30 +1144,33 @@ private function processFile($fileName) } // Prepare the picture for provide the image size. - if ($file->isImage && ($information['isPicture'] = (int) ($this->resizeImages[2] ?? 0))) { + if ($file->isImage && ($information['isPicture'] = (bool) ($this->resizeImages[2] ?? false))) { $projectDir = $this->rootDir; /** @psalm-suppress InternalMethod */ - $staticUrl = $this->filesContext->getStaticUrl(); - $picture = $this->pictureFactory->create($projectDir . '/' . $file->path, $this->getResizeImages()); - - $information['picture'] = [ - 'alt' => $altText, - 'title' => $title, - 'img' => $picture->getImg($projectDir, $staticUrl), - 'sources' => $picture->getSources($projectDir, $staticUrl) - ]; - - $information['imageUrl'] = $fileName; - - if (isset($GLOBALS['objPage']->layoutId)) { - $lightboxSize = StringUtil::deserialize( - (LayoutModel::findByPk($GLOBALS['objPage']->layoutId)->lightboxSize ?? null), - true - ); - $lightboxPicture = - $this->pictureFactory->create($projectDir . '/' . $file->path, $lightboxSize); - $information['lightboxPicture'] = $lightboxPicture; - $information['imageUrl'] = $lightboxPicture->getImg($projectDir, $staticUrl)['src']; + $staticUrl = $this->filesContext->getStaticUrl(); + + try { + $picture = $this->pictureFactory->create($projectDir . '/' . $file->path, $this->getResizeImages()); + $information['picture'] = [ + 'alt' => $altText, + 'title' => $title, + 'img' => $picture->getImg($projectDir, $staticUrl), + 'sources' => $picture->getSources($projectDir, $staticUrl) + ]; + + if (isset($GLOBALS['objPage']->layoutId)) { + $lightboxSize = StringUtil::deserialize( + (LayoutModel::findByPk($GLOBALS['objPage']->layoutId)->lightboxSize ?? null), + true + ); + $lightboxPicture = + $this->pictureFactory->create($projectDir . '/' . $file->path, $lightboxSize); + $information['lightboxPicture'] = $lightboxPicture; + $information['imageUrl'] = $lightboxPicture->getImg($projectDir, $staticUrl)['src']; + } + } catch (\Throwable $exception) { + // Unreadable broken image - ignore. + $information['isPicture'] = false; } }