Skip to content

Commit a31fa1b

Browse files
committed
Added lazy resizing support for thumbnail view in media manager
1 parent c0119a0 commit a31fa1b

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

modules/backend/widgets/MediaManager.php

+24-8
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ public function onResizeImage()
802802
$path = Input::get('path');
803803
$path = MediaLibrary::validatePath($path);
804804

805-
$croppedPath = $this->resizeImage(MediaLibrary::url($path), [
805+
$croppedPath = $this->resizeImage($path, [
806806
'mode' => 'exact',
807807
'width' => $width,
808808
'height' => $height
@@ -1325,14 +1325,14 @@ protected function generateThumbnail($thumbnailInfo, $thumbnailParams = null): a
13251325
/*
13261326
* Resize the thumbnail and save to the thumbnails directory
13271327
*/
1328-
$fullThumbnailPath = $this->resizeImage(MediaLibrary::url($path), $thumbnailParams);
1328+
$fullThumbnailPath = $this->lazyResizeImage($path, $thumbnailParams);
13291329

13301330
/*
13311331
* Delete the temporary file
13321332
*/
13331333
$markup = $this->makePartial('thumbnail-image', [
13341334
'isError' => false,
1335-
'imageUrl' => $this->getThumbnailImageUrl($fullThumbnailPath)
1335+
'imageUrl' => $fullThumbnailPath
13361336
]);
13371337
} catch (\Throwable $ex) {
13381338
$markup = $this->makePartial('thumbnail-image', ['isError' => true]);
@@ -1353,10 +1353,26 @@ protected function generateThumbnail($thumbnailInfo, $thumbnailParams = null): a
13531353
/**
13541354
* Resize an image
13551355
*/
1356-
protected function resizeImage(string $image, array $params): string
1356+
protected function lazyResizeImage(string $path, array $params): string
1357+
{
1358+
return ImageResizer::filterGetUrl(
1359+
MediaLibrary::url($path),
1360+
$params['width'],
1361+
$params['height'],
1362+
array_merge(
1363+
['mode' => 'exact'],
1364+
$params
1365+
)
1366+
);
1367+
}
1368+
1369+
/**
1370+
* Resize an image
1371+
*/
1372+
protected function resizeImage(string $path, array $params): string
13571373
{
13581374
return ImageResizer::processImage(
1359-
$image,
1375+
MediaLibrary::url($path),
13601376
$params['width'],
13611377
$params['height'],
13621378
array_merge(
@@ -1370,10 +1386,10 @@ protected function resizeImage(string $image, array $params): string
13701386
/**
13711387
* Crop an image
13721388
*/
1373-
protected function cropImage(string $image, array $params): string
1389+
protected function cropImage(string $path, array $params): string
13741390
{
13751391
return ImageResizer::processImage(
1376-
$image,
1392+
MediaLibrary::url($path),
13771393
$params['width'],
13781394
$params['height'],
13791395
array_merge(
@@ -1423,7 +1439,7 @@ protected function getCropEditImageUrlAndSize($path, $params = null)
14231439
$url = MediaLibrary::url($path);
14241440

14251441
if ($params) {
1426-
$url = $this->resizeImage($url, [
1442+
$url = $this->resizeImage($path, [
14271443
'mode' => 'exact',
14281444
'width' => $params['width'],
14291445
'height' => $params['height'],

modules/backend/widgets/mediamanager/partials/_item-icon.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
<div class="icon-container <?= $itemType ?>">
22
<div class="icon-wrapper"><i class="<?= $this->itemTypeToIconClass($item, $itemType) ?>"></i></div>
3-
4-
<?php
5-
if ($itemType == System\Classes\MediaLibraryItem::FILE_TYPE_IMAGE):
6-
$thumbnailPath = $this->thumbnailExists($thumbnailParams, $item->path, $item->lastModified);
7-
?>
8-
3+
<?php if (
4+
$itemType == System\Classes\MediaLibraryItem::FILE_TYPE_IMAGE
5+
&& $thumbnailPath = $this->lazyResizeImage($item->path, $thumbnailParams)
6+
): ?>
97
<div>
108
<?php if (!$thumbnailPath): ?>
119
<div class="image-placeholder"
12-
data-width="<?= $thumbnailParams['width'] ?>"
13-
data-height="<?= $thumbnailParams['height'] ?>"
14-
data-path="<?= e($item->path) ?>"
15-
data-last-modified="<?= $item->lastModified ?>"
16-
id="<?= $this->getPlaceholderId($item) ?>"
10+
data-width="<?= $thumbnailParams['width'] ?>"
11+
data-height="<?= $thumbnailParams['height'] ?>"
12+
data-path="<?= e($item->path) ?>"
13+
data-last-modified="<?= $item->lastModified ?>"
14+
id="<?= $this->getPlaceholderId($item) ?>"
1715
>
1816
<div class="icon-wrapper"><i class="<?= $this->itemTypeToIconClass($item, $itemType) ?>"></i></div>
1917
</div>
2018
<?php else: ?>
2119
<?= $this->makePartial('thumbnail-image', [
22-
'isError' => $this->thumbnailIsError($thumbnailPath),
23-
'imageUrl' => $this->getThumbnailImageUrl($thumbnailPath)
20+
'imageUrl' => $thumbnailPath
2421
]) ?>
2522
<?php endif ?>
2623
</div>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?php if (!$isError): ?>
1+
<?php if ($imageUrl): ?>
22
<img src="<?= $imageUrl ?>"/>
33
<?php else: ?>
44
<i class="icon-chain-broken" title="<?= e(trans('backend::lang.media.thumbnail_error')) ?>"></i>
55
<p class="thumbnail-error-message"><?= e(trans('backend::lang.media.thumbnail_error')) ?></p>
6-
<?php endif ?>
6+
<?php endif ?>

0 commit comments

Comments
 (0)