Skip to content

Commit 5a68ef6

Browse files
committed
Allow a closure to be passed with image replacement tags
1 parent 8fbd060 commit 5a68ef6

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs)
447447
$width = null;
448448
$height = null;
449449
$ratio = null;
450+
451+
// a closure can be passed as replacement value which after resolving, can contain the replacement info for the image
452+
// use case: only when a image if found, the replacement tags can be generated
453+
if (is_callable($replaceImage)) {
454+
$replaceImage = $replaceImage();
455+
}
456+
450457
if (is_array($replaceImage) && isset($replaceImage['path'])) {
451458
$imgPath = $replaceImage['path'];
452459
if (isset($replaceImage['width'])) {

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,11 @@ public function testSetImageValue()
392392
$imagePath = __DIR__ . '/_files/images/earth.jpg';
393393

394394
$variablesReplace = array(
395-
'headerValue' => $imagePath,
396-
'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500),
397-
'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false),
395+
'headerValue' => function () use ($imagePath) {
396+
return $imagePath;
397+
},
398+
'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500),
399+
'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false),
398400
);
399401
$templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace);
400402

0 commit comments

Comments
 (0)