Forum


Replies: 6   Views: 278
Replacing multiple images
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 25-04-2024 - 12:54

Hello,

Yes, as explained in our previous reply, you can add as many contents as needed in the same WordFragment. For example, three images added as block contents (each image in its own pararagraph):

$contentFragment = new WordFragment($docx, 'document');
$contentFragment->addImage(['src' => 'image1.png']);
$contentFragment->addImage(['src' => 'image2.png']);
$contentFragment->addImage(['src' => 'image3.png']);

$docx->replaceVariableByWordFragment(['VAR_IMAGE' => $contentFragment], ['type' => 'block']);

Or three images added as inline images (in the same paragraph):

$contentFragment1 = new WordFragment($docx, 'document');
$contentFragment1->addImage(['src' => 'image1.png']);
$contentFragment2 = new WordFragment($docx, 'document');
$contentFragment2->addImage(['src' => 'image2.png']);
$contentFragment3 = new WordFragment($docx, 'document');
$contentFragment3->addImage(['src' => 'image3.png']);

$content = array();
$content[] = $contentFragment1;
$content[] = $contentFragment2;
$content[] = $contentFragment3;

$contentFragment = new WordFragment($docx);
$contentFragment->addText($content);

$docx->replaceVariableByWordFragment(['VAR_IMAGE' => $contentFragment], ['type' => 'block']);

replaceVariableByWordFragment supports doing block or inline type replacements.

We recommend you read the following documentation pages, that explains how to work with WordFragments (including samples):

Regards.