Refactor work for Thumbnail class #23

Merged
Aaron merged 1 commits from thumbnail-refactor into master 2020-12-30 20:08:04 +01:00

View File

@ -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;