From d6c7273c638b09557f1842d1c01a1d03ee71fab3 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 2 Aug 2019 10:32:42 -0400 Subject: [PATCH] Update the PagefileExtra class to support some additional properties related to file size --- wire/core/PagefileExtra.php | 28 +++++++++++++++++++ .../InputfieldImage/InputfieldImage.module | 10 +++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/wire/core/PagefileExtra.php b/wire/core/PagefileExtra.php index dabc90a6..3a64251d 100644 --- a/wire/core/PagefileExtra.php +++ b/wire/core/PagefileExtra.php @@ -16,7 +16,11 @@ * @property string $ext Alias of extension * @property bool $exists Does the file exist? * @property int $filesize Size of file in bytes + * @property string $filesizeStr Human readable size of file * @property Pagefile|Pageimage $pagefile Source Pageimage object + * @property int $savings Bytes saved by this extra + * @property string $savingsStr Human readable savings by this extra + * @property string $savingsPct Percent savings by this extra * * The following properties affect the behavior of the URL-related methods * ======================================================================= @@ -111,6 +115,16 @@ class PagefileExtra extends WireData { return (int) @filesize($this->filename()); } + /** + * Return human readable file size string + * + * @return string + * + */ + public function filesizeStr() { + return wireBytesStr($this->filesize()); + } + /** * Return the full server disk path to the extra file, whether it exists or not * @@ -223,6 +237,20 @@ class PagefileExtra extends WireData { case 'filesize': $value = $this->filesize(); break; + case 'filesizeStr': + $value = $this->filesizeStr(); + break; + case 'savings': + $value = $this->pagefile->filesize() - $this->filesize(); + break; + case 'savingsStr': + $value = wireBytesStr($this->pagefile->filesize() - $this->filesize()); + break; + case 'savingsPct': + $imageSize = $this->pagefile->filesize(); + $extraSize = $this->filesize(); + $value = round((($imageSize - $extraSize) / $imageSize) * 100) . '%'; + break; case 'url': $value = $this->url(); break; diff --git a/wire/modules/Inputfield/InputfieldImage/InputfieldImage.module b/wire/modules/Inputfield/InputfieldImage/InputfieldImage.module index 8b5bca1e..38c9e6f0 100755 --- a/wire/modules/Inputfield/InputfieldImage/InputfieldImage.module +++ b/wire/modules/Inputfield/InputfieldImage/InputfieldImage.module @@ -27,6 +27,7 @@ * * @property int $gridSize squared size of the admin thumbnails (default=130) * @property string $gridMode Default grid mode in admin, one of "grid", "left" or "list" (default="grid") + * @property string $focusMode May be 'on', 'off', or 'zoom' * @property array $imageSizerOptions Options to pass along to the ImageSizer class. See /wire/config.php $imageSizerOptions for details. * * @@ -624,6 +625,11 @@ class InputfieldImage extends InputfieldFile implements InputfieldItemList, Inpu $sanitizer = $this->wire('sanitizer'); $thumb = $this->getAdminThumb($pagefile, false); $fileStats = str_replace(' ', ' ', $pagefile->filesizeStr) . ", {$pagefile->width}×{$pagefile->height} "; + + foreach($pagefile->extras() as $name => $extra) { + if($extra->exists()) $fileStats .= " • $extra->filesizeStr $name ($extra->savingsPct)"; + } + // $gridSize = $this->gridSize; //
@@ -773,8 +779,8 @@ class InputfieldImage extends InputfieldFile implements InputfieldItemList, Inpu if($n) {} // ignore, $n is for hooks $pageID = $pagefile->pagefiles->page->id; - $variations = $this->getPagefileVariations($pagefile); - $variationCount = count($variations); + $variationCount = $pagefile->variations()->count(); + if($pagefile->webp()->exists()) $variationCount++; $editUrl = $this->getEditUrl($pagefile, $pageID); $variationUrl = $this->getVariationUrl($pagefile, $id); $buttonClass = $this->themeSettings['buttonClass'];