Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_broken_image_file' into 'release/2.3.0'
Browse files Browse the repository at this point in the history
Add check for brocken image files

See merge request metamodels/core!348
  • Loading branch information
zonky2 committed Oct 28, 2024
2 parents 7a85614 + aafdbb5 commit 53fc72e
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions src/Helper/ToolboxFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}

Expand Down

0 comments on commit 53fc72e

Please sign in to comment.