Merge pull request #1716 from enflow/allowImageClosure
Allow a closure to be passed with image replacement tags
This commit is contained in:
commit
4e87e72110
|
|
@ -63,6 +63,11 @@ Example:
|
||||||
|
|
||||||
$templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png');
|
$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('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
|
cloneBlock
|
||||||
""""""""""
|
""""""""""
|
||||||
|
|
|
||||||
|
|
@ -456,6 +456,13 @@ class TemplateProcessor
|
||||||
$width = null;
|
$width = null;
|
||||||
$height = null;
|
$height = null;
|
||||||
$ratio = 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'])) {
|
if (is_array($replaceImage) && isset($replaceImage['path'])) {
|
||||||
$imgPath = $replaceImage['path'];
|
$imgPath = $replaceImage['path'];
|
||||||
if (isset($replaceImage['width'])) {
|
if (isset($replaceImage['width'])) {
|
||||||
|
|
|
||||||
|
|
@ -397,9 +397,11 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
||||||
$imagePath = __DIR__ . '/_files/images/earth.jpg';
|
$imagePath = __DIR__ . '/_files/images/earth.jpg';
|
||||||
|
|
||||||
$variablesReplace = array(
|
$variablesReplace = array(
|
||||||
'headerValue' => $imagePath,
|
'headerValue' => function () use ($imagePath) {
|
||||||
'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500),
|
return $imagePath;
|
||||||
'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false),
|
},
|
||||||
|
'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500),
|
||||||
|
'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false),
|
||||||
);
|
);
|
||||||
$templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace);
|
$templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue