forked from Public/pics
Thumbnail class: refactor getUrl method
This commit is contained in:
parent
2bcdc5fe6e
commit
e3b67c4022
@ -9,6 +9,7 @@
|
|||||||
class Thumbnail
|
class Thumbnail
|
||||||
{
|
{
|
||||||
private $image;
|
private $image;
|
||||||
|
private $image_meta;
|
||||||
private $thumbnails;
|
private $thumbnails;
|
||||||
|
|
||||||
private $properly_initialised;
|
private $properly_initialised;
|
||||||
@ -23,7 +24,7 @@ class Thumbnail
|
|||||||
const CROP_MODE_SLICE_CENTRE = 4;
|
const CROP_MODE_SLICE_CENTRE = 4;
|
||||||
const CROP_MODE_SLICE_BOTTOM = 5;
|
const CROP_MODE_SLICE_BOTTOM = 5;
|
||||||
|
|
||||||
public function __construct($image)
|
public function __construct(Image $image)
|
||||||
{
|
{
|
||||||
$this->image = $image;
|
$this->image = $image;
|
||||||
$this->image_meta = $image->getMeta();
|
$this->image_meta = $image->getMeta();
|
||||||
@ -45,51 +46,45 @@ 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]))
|
||||||
{
|
{
|
||||||
$thumb_path = '/' . $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
|
$thumb_filename = $this->image->getSubdir() . '/' . $this->thumbnails[$thumb_selector];
|
||||||
if (file_exists(THUMBSDIR . $thumb_path))
|
if (file_exists(THUMBSDIR . '/' . $thumb_filename))
|
||||||
return THUMBSURL . $thumb_path;
|
return THUMBSURL . '/' . $thumb_filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]))
|
||||||
{
|
{
|
||||||
$custom_thumb_path = '/' . $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector];
|
$custom_filename = $this->image->getSubdir() . '/' . $this->image_meta[$custom_selector];
|
||||||
if (file_exists(ASSETSDIR . $custom_thumb_path))
|
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 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.
|
// Let's remember this for future reference.
|
||||||
$this->markAsGenerated($this->image_meta[$custom_selector]);
|
$this->markAsGenerated($this->image_meta[$custom_selector]);
|
||||||
|
|
||||||
return THUMBSURL . $custom_thumb_path;
|
return THUMBSURL . '/' . $custom_filename;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!');
|
throw new UnexpectedValueException('Custom thumbnail expected, but missing in file system!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is this the right moment to generate a thumbnail, then?
|
// Is this the right moment to generate a thumbnail, then?
|
||||||
if ($generate && array_key_exists($thumb_selector, $this->thumbnails))
|
if ($generate)
|
||||||
{
|
{
|
||||||
return $this->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.
|
// 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
|
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;
|
$thumb_selector = $this->width . 'x' . $this->height . $this->filename_suffix;
|
||||||
$this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : '';
|
$this->thumbnails[$thumb_selector] = $filename !== 'NULL' ? $filename : '';
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
throw new UnexpectedValueException('Thumbnail queuing query failed');
|
||||||
|
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user