From e7490e40dd6fa803e464bf74d1f06669b4bdfe24 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 29 Nov 2020 17:12:27 +0100 Subject: [PATCH] Clean up and force a rebuild of thumbnails when customising a crop region. --- controllers/EditAsset.php | 15 +++++----- models/Image.php | 28 ++++++++++++++++-- templates/EditAssetForm.php | 58 ++++++++++++++++--------------------- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/controllers/EditAsset.php b/controllers/EditAsset.php index 2f79390..a794fd4 100644 --- a/controllers/EditAsset.php +++ b/controllers/EditAsset.php @@ -149,18 +149,19 @@ class EditAsset extends HTMLController $crop_value = $data->crop_width . ',' . $data->crop_height . ',' . $data->source_x . ',' . $data->source_y; $meta[$crop_key] = $crop_value; - // If we uploaded a custom thumbnail, stop considering it such. + // If we previously uploaded a custom thumbnail, stop considering it such. $custom_key = 'custom_' . $data->thumb_width . 'x' . $data->thumb_height; if (isset($meta[$custom_key])) + { + // TODO: delete from disk unset($meta[$custom_key]); + } + + // Save meta changes so far. + $image->setMetaData($meta); // Force a rebuild of related thumbnails. - $thumb_key = 'thumb_' . $data->thumb_width . 'x' . $data->thumb_height; - foreach ($meta as $meta_key => $meta_value) - if ($meta_key === $thumb_key || strpos($meta_key, $thumb_key . '_') !== false) - unset($meta[$meta_key]); - - $image->setMetaData($meta); + $image->removeThumbnailsOfSize($data->thumb_width, $data->thumb_height); $payload = [ 'key' => $crop_key, diff --git a/models/Image.php b/models/Image.php index dae6422..c8205f8 100644 --- a/models/Image.php +++ b/models/Image.php @@ -133,9 +133,9 @@ class Image extends Asset public function removeAllThumbnails() { - foreach ($this->thumbnails as $key => $value) + foreach ($this->thumbnails as $key => $filename) { - $thumb_path = THUMBSDIR . '/' . $this->subdir . '/' . $value; + $thumb_path = THUMBSDIR . '/' . $this->subdir . '/' . $filename; if (is_file($thumb_path)) unlink($thumb_path); } @@ -146,6 +146,30 @@ class Image extends Asset ['id_asset' => $this->id_asset]); } + public function removeThumbnailsOfSize($width, $height) + { + foreach ($this->thumbnails as $key => $filename) + { + if (strpos($key, $width . 'x' . $height) !== 0) + continue; + + $thumb_path = THUMBSDIR . '/' . $this->subdir . '/' . $filename; + if (is_file($thumb_path)) + unlink($thumb_path); + } + + return Registry::get('db')->query(' + DELETE FROM assets_thumbs + WHERE id_asset = {int:id_asset} AND + width = {int:width} AND + height = {int:height}', + [ + 'height' => $height, + 'id_asset' => $this->id_asset, + 'width' => $width, + ]); + } + public function replaceThumbnail($descriptor, $tmp_file) { if (!is_file($tmp_file)) diff --git a/templates/EditAssetForm.php b/templates/EditAssetForm.php index dc7682c..490b0d2 100644 --- a/templates/EditAssetForm.php +++ b/templates/EditAssetForm.php @@ -141,9 +141,6 @@ class EditAssetForm extends SubTemplate $first = INF; foreach ($this->thumbs as $i => $thumb) { - if (!$thumb['status']) - continue; - $first = min($i, $first); echo ' @@ -178,14 +175,12 @@ class EditAssetForm extends SubTemplate Thumbnail - '; } @@ -196,27 +191,27 @@ class EditAssetForm extends SubTemplate echo ' - '; } @@ -259,9 +254,6 @@ class EditAssetForm extends SubTemplate foreach ($this->thumbs as $thumb) { - if (!$thumb['status']) - continue; - echo '