Thumbnail class: refactor getUrl method

This commit is contained in:
Aaron van Geffen 2022-07-07 13:33:40 +02:00
parent 2bcdc5fe6e
commit e3b67c4022

View File

@ -9,6 +9,7 @@
class Thumbnail
{
private $image;
private $image_meta;
private $thumbnails;
private $properly_initialised;
@ -23,7 +24,7 @@ class Thumbnail
const CROP_MODE_SLICE_CENTRE = 4;
const CROP_MODE_SLICE_BOTTOM = 5;
public function __construct($image)
public function __construct(Image $image)
{
$this->image = $image;
$this->image_meta = $image->getMeta();
@ -45,51 +46,45 @@ class Thumbnail
$thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix;
if (!empty($this->thumbnails[$thumb_selector]))
{
$thumb_path = '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
if (file_exists(THUMBSDIR . $thumb_path))
return THUMBSURL . $thumb_path;
$thumb_filename = $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
if (file_exists(THUMBSDIR . '/' . $thumb_filename))
return THUMBSURL . '/' . $thumb_filename;
}
// Do we have a custom thumbnail on file?
$custom_selector = 'custom_' . $this->width . 'x' . $this->height;
if (isset($this->image_meta[$custom_selector]))
{
$custom_thumb_path = '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector];
if (file_exists(ASSETSDIR . $custom_thumb_path))
$custom_filename = $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector];
if (file_exists(ASSETSDIR . '/' . $custom_filename))
{
// 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 . $custom_thumb_path, THUMBSDIR . $custom_thumb_path);
copy(ASSETSDIR . '/' . $custom_filename, THUMBSDIR . '/' . $custom_filename);
// Let's remember this for future reference.
$this->markAsGenerated($this->image_meta[$custom_selector]);
return THUMBSURL . $custom_thumb_path;
return THUMBSURL . '/' . $custom_filename;
}
else
throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!');
}
// Is this the right moment to generate a thumbnail, then?
if ($generate && array_key_exists($thumb_selector, $this->thumbnails))
if ($generate)
{
if (array_key_exists($thumb_selector, $this->thumbnails))
return $this->generate();
else
throw new Exception("Trying to generate a thumbnail not previously queued by the system\n" .
print_r(func_get_args(), true));
}
// If not, queue it for generation at another time, and return a URL to generate it with.
elseif (!$generate)
{
$this->markAsQueued();
return BASEURL . '/thumbnail/' . $this->image->getId() . '/' . $this->width . 'x' . $this->height . $this->filename_suffix . '/';
}
// Still here..? What are you up to? ..Sneaking?
else
{
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));
$this->markAsQueued();
return BASEURL . '/thumbnail/' . $this->image->getId() . '/' . $thumb_selector . '/';
}
}
@ -338,6 +333,8 @@ class Thumbnail
$thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix;
$this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : '';
}
else
throw new UnexpectedValueException('Thumbnail queuing query failed');
return $success;
}