Merge pull request 'Refactor work for Thumbnail class' (#23) from thumbnail-refactor into master

Reviewed-on: Public/pics#23
This commit is contained in:
Aaron van Geffen 2020-12-30 20:08:02 +01:00
commit 162d14b35f

View File

@ -3,7 +3,7 @@
* Thumbnail.php * Thumbnail.php
* Contains key class Thumbnail. * Contains key class Thumbnail.
* *
* Kabuki CMS (C) 2013-2015, Aaron van Geffen * Kabuki CMS (C) 2013-2020, Aaron van Geffen
*****************************************************************************/ *****************************************************************************/
class Thumbnail class Thumbnail
@ -45,24 +45,29 @@ class Thumbnail
$thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix; $thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix;
if (!empty($this->thumbnails[$thumb_selector])) if (!empty($this->thumbnails[$thumb_selector]))
{ {
if (file_exists(THUMBSDIR . '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector])) $thumb_path = '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
return THUMBSURL . '/' . $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? // Do we have a custom thumbnail on file?
$custom_selector = 'custom_' . $this->width . 'x' . $this->height; $custom_selector = 'custom_' . $this->width . 'x' . $this->height;
if (isset($this->image_meta[$custom_selector])) 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 the custom thumbail to the general thumbnail directory.
copy(ASSETSDIR . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector], copy(ASSETSDIR . $custom_thumb_path, THUMBSDIR . $custom_thumb_path);
THUMBSDIR . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector]);
// Let's remember this for future reference. // Let's remember this for future reference.
$this->markAsGenerated($this->image_meta[$custom_selector]); $this->markAsGenerated($this->image_meta[$custom_selector]);
return THUMBSURL . '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector]; return THUMBSURL . $custom_thumb_path;
} }
else else
throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!'); 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? // Still here..? What are you up to? ..Sneaking?
else 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])) if (isset($this->image_meta[$crop_selector]))
$this->crop_mode = self::CROP_MODE_BOUNDARY; $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. // We won't be cropping if the thumbnail is proportional to its original.
elseif (abs($this->ratio() - $this->image->ratio()) <= 0.025) elseif (abs($this->ratio() - $this->image->ratio()) <= 0.025)
$this->crop_mode = self::CROP_MODE_NONE; $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? // Slice from the top?
elseif ($crop === 'top' || $crop === 'ct') elseif ($crop === 'top' || $crop === 'ct')
$this->crop_mode = self::CROP_MODE_SLICE_TOP; $this->crop_mode = self::CROP_MODE_SLICE_TOP;