From 2b0d8f333accde70251591c7224cdb117f17dd95 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Sun, 30 Jun 2019 11:01:23 -0400 Subject: [PATCH] Some updates preparing for additonal webp options (new options not ready to use quite yet). --- wire/config.php | 20 ++++++++++++++++++-- wire/core/Config.php | 1 + wire/core/PagefileExtra.php | 10 ++++++++-- wire/core/Pageimage.php | 13 ++++++++----- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/wire/config.php b/wire/config.php index 1201d5aa..6b2fac0c 100644 --- a/wire/config.php +++ b/wire/config.php @@ -622,7 +622,6 @@ $config->fileContentTypes = array( * #property int quality Image quality, enter a value between 1 and 100, where 100 is highest quality (and largest files) * #property float defaultGamma Default gamma of 0.5 to 4.0 or -1 to disable gamma correction (default=2.0) * #property bool webpAdd Create a WEBP copy with every new image variation? (default=false) - * #property int webpQuality Quality setting of 1-100 where higher is better but bigger (default=90) * * @var array * @@ -637,7 +636,24 @@ $config->imageSizerOptions = array( 'hidpiQuality' => 60, // Same as above quality setting, but specific to hidpi images 'defaultGamma' => 2.0, // defaultGamma: 0.5 to 4.0 or -1 to disable gamma correction (default=2.0) 'webpAdd' => false, // set this to true, if the imagesizer engines should create a Webp copy with every (new) image variation - 'webpQuality' => 90, // webpQuality: 1-100 where higher is better but bigger + ); + +/** + * Options for webp images + * + * #property int quality Quality setting where 1-100 where higher is better but bigger + * #property bool useSrcExt Use source file extension in webp filename? (file.jpg.webp rather than file.webp) + * #property bool useSrcUrlOnSize Fallback to source file URL when webp file is larger than source? + * #property bool useSrcUrlOnFail Fallback to source file URL when webp file fails for some reason? + * + * @var array + * + */ +$config->webpOptions = array( + 'quality' => 90, // Quality setting of 1-100 where higher is better but bigger + 'useSrcExt' => false, // Use source file extension in webp filename? (file.jpg.webp rather than file.webp) + 'useSrcUrlOnSize' => true, // Fallback to source file URL when webp file is larger than source? + 'useSrcUrlOnFail' => true, // Fallback to source file URL when webp file fails for some reason? ); /** diff --git a/wire/core/Config.php b/wire/core/Config.php index 98269f40..df938c65 100644 --- a/wire/core/Config.php +++ b/wire/core/Config.php @@ -40,6 +40,7 @@ * @property bool $protectCSRF Enables CSRF (cross site request forgery) protection on all PW forms, recommended for security. #pw-group-HTTP-and-input * * @property array $imageSizerOptions Options to set image sizing defaults. Please see the /wire/config.php file for all options and defaults. #pw-group-images + * @property array $webpOptions Options for webp images. Please see /wire/config.php for all options. #pw-group-images * * @property bool $pagefileSecure When used, files in /site/assets/files/ will be protected with the same access as the page. Routines files through a passthrough script. #pw-group-files * @property string $pagefileSecurePathPrefix One or more characters prefixed to the pathname of protected file dirs. This should be some prefix that the .htaccess file knows to block requests for. #pw-group-files diff --git a/wire/core/PagefileExtra.php b/wire/core/PagefileExtra.php index 223a4d59..dabc90a6 100644 --- a/wire/core/PagefileExtra.php +++ b/wire/core/PagefileExtra.php @@ -22,6 +22,7 @@ * ======================================================================= * @property bool $useSrcUrlOnFail Use source Pagefile URL if extra image does not exist and cannot be created? (default=false) * @property bool $useSrcUrlOnSize Use source Pagefile URL if extra file is larger than source file? (default=false) + * @property bool $useSrcExt Use longer filenames that also include the Pagefile’s extension? (default=false) * * Hookable methods * ================ @@ -64,6 +65,7 @@ class PagefileExtra extends WireData { $this->setExtension($extension); $this->useSrcUrlOnFail = true; $this->useSrcUrlOnSize = false; + $this->useSrcExt = false; return parent::__construct(); } @@ -117,7 +119,9 @@ class PagefileExtra extends WireData { */ public function filename() { $pathinfo = pathinfo($this->pagefile->filename()); - $filename = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.' . $this->extension; + $ext = '.' . $this->extension; + if($this->useSrcExt) $ext = '.' . $pathinfo['extension'] . $ext; + $filename = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . $ext; if(empty($this->filenamePrevious)) $this->filenamePrevious = $filename; return $filename; } @@ -151,7 +155,9 @@ class PagefileExtra extends WireData { $url = $this->pagefile->url(); } else { $pathinfo = pathinfo($this->pagefile->url()); - $url = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.' . $this->extension; + $ext = '.' . $this->extension; + if($this->useSrcExt) $ext = '.' . $pathinfo['extension'] . $ext; + $url = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . $ext; } return $url; diff --git a/wire/core/Pageimage.php b/wire/core/Pageimage.php index ecff9392..d6654b6b 100644 --- a/wire/core/Pageimage.php +++ b/wire/core/Pageimage.php @@ -685,6 +685,8 @@ class Pageimage extends Pagefile { $config = $this->wire('config'); $debug = $config->debug; $configOptions = $config->imageSizerOptions; + $webpOptions = $config->webpOptions; + if(!empty($webpOptions['quality'])) $defaultOptions['webpQuality'] = $webpOptions['quality']; if(!is_array($configOptions)) $configOptions = array(); $options = array_merge($defaultOptions, $configOptions, $options); @@ -772,11 +774,13 @@ class Pageimage extends Pagefile { $filenameFinal = $this->pagefiles->path() . $basename; $filenameFinalExists = file_exists($filenameFinal); - + if(!empty($options['webpName'])) { $filenameFinalWebp = $this->pagefiles->path() . basename($options['webpName'], '.webp') . '.webp'; + } else if(!empty($webpOptions['useSrcExt'])) { + $filenameFinalWebp = $this->pagefiles->path() . $basename . '.webp'; // file.jpg.webp } else { - $filenameFinalWebp = $this->pagefiles->path() . $basenameNoExt . '.webp'; + $filenameFinalWebp = $this->pagefiles->path() . $basenameNoExt . '.webp'; // file.webp } // force new creation if requested webp copy doesn't exist, (regardless if regular variation exists or not) @@ -1913,8 +1917,7 @@ class Pageimage extends Pagefile { $webp = $this->extras('webp'); if(!$webp) { $webp = new PagefileExtra($this, 'webp'); - $webp->useSrcUrlOnFail = true; // use this pagefile URL instead if we fail to create a webp - $webp->useSrcUrlOnSize = true; // use webp URL only if it results in a smaller file + $webp->setArray($this->wire('config')->webpOptions); $this->extras('webp', $webp); $webp->addHookAfter('create', $this, 'hookWebpCreate'); } @@ -1945,7 +1948,7 @@ class Pageimage extends Pagefile { $original = $this; $options = array( 'allowOriginal' => false, - 'webpName' => basename($this->basename(), ".$this->ext"), + 'webpName' => $webp->useSrcExt ? $this->basename() : basename($this->basename(), ".$this->ext"), 'webpOnly' => true ); $width = $this->width;