From 086102d0078a9cdf06de797a36b8d1e1457a69dd Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Thu, 7 Jul 2022 13:51:03 +0200 Subject: [PATCH] Thumbnail class: minor refactor of generate method --- models/Thumbnail.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/models/Thumbnail.php b/models/Thumbnail.php index c7c45ba..5eb562c 100644 --- a/models/Thumbnail.php +++ b/models/Thumbnail.php @@ -255,14 +255,18 @@ class Thumbnail '_' . $this->width . 'x' . $this->height . $this->filename_suffix . '.' . $ext; // Ensure the thumbnail subdirectory exists. - if (!is_dir(THUMBSDIR . '/' . $this->image->getSubdir())) - mkdir(THUMBSDIR . '/' . $this->image->getSubdir(), 0755, true); + $target_dir = THUMBSDIR . '/' . $this->image->getSubdir(); + if (!is_dir($target_dir)) + mkdir($target_dir, 0755, true); + + if (!is_writable($target_dir)) + throw new Exception('Thumbnail directory is not writable!'); // No need to preserve every detail. $thumb->setImageCompressionQuality(80); // Save it in a public spot. - $thumb->writeImage(THUMBSDIR . '/' . $this->image->getSubdir() . '/' . $thumb_filename); + $thumb->writeImage($target_dir . '/' . $thumb_filename); // Let's remember this for future reference... $this->markAsGenerated($thumb_filename); @@ -273,7 +277,6 @@ class Thumbnail // Finally, return the URL for the generated thumbnail image. return THUMBSURL . '/' . $this->image->getSubdir() . '/' . $thumb_filename; } - // Blast! Curse your sudden but inevitable betrayal! catch (ImagickException $e) { throw new Exception('ImageMagick error occurred while generating thumbnail. Output: ' . $e->getMessage()); @@ -331,12 +334,11 @@ class Thumbnail if ($success) { $thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix; - $this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : ''; + $this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : null; + return $success; } else throw new UnexpectedValueException('Thumbnail queuing query failed'); - - return $success; } private function markAsQueued()