From 555c61937bb7ac9e80facd981708d42ab1a26ddd Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 30 Dec 2020 13:02:47 +0100 Subject: [PATCH] Minor Thumbnail refactors from upstream Kabuki. --- models/Thumbnail.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/models/Thumbnail.php b/models/Thumbnail.php index d0b982f..b650854 100644 --- a/models/Thumbnail.php +++ b/models/Thumbnail.php @@ -3,7 +3,7 @@ * Thumbnail.php * Contains key class Thumbnail. * - * Kabuki CMS (C) 2013-2015, Aaron van Geffen + * Kabuki CMS (C) 2013-2020, Aaron van Geffen *****************************************************************************/ class Thumbnail @@ -45,24 +45,29 @@ class Thumbnail $thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix; if (!empty($this->thumbnails[$thumb_selector])) { - if (file_exists(THUMBSDIR . '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector])) - return THUMBSURL . '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector]; + $thumb_path = '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector]; + if (file_exists(THUMBSDIR . $thumb_path)) + return THUMBSURL . $thumb_path; } // Do we have a custom thumbnail on file? $custom_selector = 'custom_' . $this->width . 'x' . $this->height; if (isset($this->image_meta[$custom_selector])) { - if (file_exists(ASSETSDIR . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector])) + $custom_thumb_path = '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector]; + if (file_exists(ASSETSDIR . $custom_thumb_path)) { + // Ensure destination thumbnail directory exists. + if (!file_exists($this->image->getSubdir())) + @mkdir(THUMBSDIR . '/' . $this->image->getSubdir(), 0755, true); + // Copy the custom thumbail to the general thumbnail directory. - copy(ASSETSDIR . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector], - THUMBSDIR . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector]); + copy(ASSETSDIR . $custom_thumb_path, THUMBSDIR . $custom_thumb_path); // Let's remember this for future reference. $this->markAsGenerated($this->image_meta[$custom_selector]); - return THUMBSURL . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector]; + return THUMBSURL . $custom_thumb_path; } else throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!'); @@ -84,7 +89,7 @@ class Thumbnail // Still here..? What are you up to? ..Sneaking? else { - throw new Exception("Trying to generate a thumbnail for values that were not previously initialised by the system?\n" . print_r(func_get_args(), true)); + throw new Exception("Trying to generate a thumbnail for selector " . $thumb_selector . ", which does not appear to have been requested by the system.\n" . print_r(func_get_args(), true)); } } @@ -127,14 +132,14 @@ class Thumbnail if (isset($this->image_meta[$crop_selector])) $this->crop_mode = self::CROP_MODE_BOUNDARY; - // If the original image's aspect ratio is much wider, take a slice instead. - if ($this->image->ratio() > $this->ratio()) - $this->crop_mode = self::CROP_MODE_SLICE_CENTRE; - // We won't be cropping if the thumbnail is proportional to its original. elseif (abs($this->ratio() - $this->image->ratio()) <= 0.025) $this->crop_mode = self::CROP_MODE_NONE; + // If the original image's aspect ratio is much wider, take a slice instead. + elseif ($this->image->ratio() > $this->ratio()) + $this->crop_mode = self::CROP_MODE_SLICE_CENTRE; + // Slice from the top? elseif ($crop === 'top' || $crop === 'ct') $this->crop_mode = self::CROP_MODE_SLICE_TOP; -- 2.46.0