1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 01:34:31 +02:00

Add PR #124 - Add hookable method to PageImage variation creation

Co-authored-by: karltdev <karl@assembly.com.hk>
This commit is contained in:
Ryan Cramer
2021-06-01 09:33:02 -04:00
parent 2895b9c7b8
commit 3fe499899b

View File

@@ -77,6 +77,7 @@
* @method array rebuildVariations($mode = 0, array $suffix = array(), array $options = array()) * @method array rebuildVariations($mode = 0, array $suffix = array(), array $options = array())
* @method install($filename) * @method install($filename)
* @method render($markup = '', $options = array()) * @method render($markup = '', $options = array())
* @method void createdVariation(Pageimage $image, array $data) Called after new image variation created (3.0.180+)
* *
*/ */
@@ -745,6 +746,7 @@ class Pageimage extends Pagefile {
$debug = $config->debug; $debug = $config->debug;
$configOptions = $config->imageSizerOptions; $configOptions = $config->imageSizerOptions;
$webpOptions = $config->webpOptions; $webpOptions = $config->webpOptions;
$createdVariationHookData = null; // populated as array only when new variation created (for createdVariation hook)
if(!empty($webpOptions['quality'])) $defaultOptions['webpQuality'] = $webpOptions['quality']; if(!empty($webpOptions['quality'])) $defaultOptions['webpQuality'] = $webpOptions['quality'];
if(!is_array($configOptions)) $configOptions = array(); if(!is_array($configOptions)) $configOptions = array();
@@ -908,7 +910,7 @@ class Pageimage extends Pagefile {
} else { } else {
$this->error = "ImageSizer::resize($width, $height) failed for $filenameUnvalidated"; $this->error = "ImageSizer::resize($width, $height) failed for $filenameUnvalidated";
} }
if($debug && empty($options['webpOnly'])) $this->wire('log')->save('image-sizer', if($debug && empty($options['webpOnly'])) $this->wire('log')->save('image-sizer',
str_replace('ImageSizerEngine', '', $sizer->getEngine()) . ' ' . str_replace('ImageSizerEngine', '', $sizer->getEngine()) . ' ' .
($this->error ? "FAILED Resize: " : "Resized: ") . "$originalName => " . basename($filenameFinal) . " " . ($this->error ? "FAILED Resize: " : "Resized: ") . "$originalName => " . basename($filenameFinal) . " " .
@@ -916,6 +918,18 @@ class Pageimage extends Pagefile {
"(quality=$options[quality], sharpening=$options[sharpening]) " "(quality=$options[quality], sharpening=$options[sharpening]) "
); );
if(!$this->error) {
$createdVariationHookData = array(
'width' => $width,
'height' => $height,
'options' => $options,
'filenameUnvalidated' => $filenameUnvalidated,
'filenameFinal' => $filenameFinal,
'filenameUnvalidatedWebp' => $filenameUnvalidatedWebp,
'filenameFinalWebp' => $filenameFinalWebp,
);
}
} catch(\Exception $e) { } catch(\Exception $e) {
$this->trackException($e, false); $this->trackException($e, false);
$this->error = $e->getMessage(); $this->error = $e->getMessage();
@@ -944,6 +958,8 @@ class Pageimage extends Pagefile {
$pageimage->setFilename($filenameFinal); $pageimage->setFilename($filenameFinal);
$pageimage->setOriginal($this); $pageimage->setOriginal($this);
if($createdVariationHookData) $this->createdVariation($pageimage, $createdVariationHookData);
return $pageimage; return $pageimage;
} }
@@ -1426,6 +1442,16 @@ class Pageimage extends Pagefile {
return $this->variations()->remove($options); return $this->variations()->remove($options);
} }
/**
* Hook called after successful creation of image variation
*
* @param Pageimage $image The variation image that was created
* @param array $data Verbose associative array of data used to create the variation
* @since 3.0.180
*
*/
protected function ___createdVariation(Pageimage $image, array $data) { }
/** /**
* Identify this Pageimage as a variation, by setting the Pageimage it was resized from. * Identify this Pageimage as a variation, by setting the Pageimage it was resized from.
* *