From 5a68ef600be7423ee5f16579ddce19b7d2b89332 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Mon, 9 Sep 2019 13:49:16 +0200 Subject: [PATCH 1/2] Allow a closure to be passed with image replacement tags --- src/PhpWord/TemplateProcessor.php | 7 +++++++ tests/PhpWord/TemplateProcessorTest.php | 8 +++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 7efc0f1ac8..3af8738c77 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -447,6 +447,13 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs) $width = null; $height = null; $ratio = null; + + // a closure can be passed as replacement value which after resolving, can contain the replacement info for the image + // use case: only when a image if found, the replacement tags can be generated + if (is_callable($replaceImage)) { + $replaceImage = $replaceImage(); + } + if (is_array($replaceImage) && isset($replaceImage['path'])) { $imgPath = $replaceImage['path']; if (isset($replaceImage['width'])) { diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 4caca77aeb..212032345e 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -392,9 +392,11 @@ public function testSetImageValue() $imagePath = __DIR__ . '/_files/images/earth.jpg'; $variablesReplace = array( - 'headerValue' => $imagePath, - 'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500), - 'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false), + 'headerValue' => function () use ($imagePath) { + return $imagePath; + }, + 'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500), + 'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false), ); $templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace); From 6ed320311e6accc3874026a3fe1a38fc28d98c1b Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Mon, 9 Sep 2019 13:55:59 +0200 Subject: [PATCH 2/2] Add documentation for image closure support --- docs/templates-processing.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/templates-processing.rst b/docs/templates-processing.rst index 5b32aa18e0..e9c0b96997 100644 --- a/docs/templates-processing.rst +++ b/docs/templates-processing.rst @@ -63,6 +63,11 @@ Example: $templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png'); $templateProcessor->setImageValue('UserLogo', array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false)); + $templateProcessor->setImageValue('FeatureImage', function () { + // Closure will only be executed if the replacement tag is found in the template + + return array('path' => SlowFeatureImageGenerator::make(), 'width' => 100, 'height' => 100, 'ratio' => false); + }); cloneBlock """"""""""