Forum


Replies: 1   Views: 150
Image replacement in word with variable.
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  · 22-08-2024 - 05:51

Hello,

Yes, you can replace template text variables with images using replaceVariableByWordFragment.
On https://www.phpdocx.com/documentation/practical/pictures-and-images you can read more information about this feature. For example:

$image = new WordFragment($docx, 'document');
$image->addImage(array('src' => 'image.png' , 'scaling' => 50, 'float' => 'right', 'textWrap' => 1));
$docx->replaceVariableByWordFragment(array('IMAGE' => $image), array('type' => 'block'));

If you need to fill a table with replaceTableVariable you can add WordFragments as new contents:

$image = new WordFragment($docx);
$imageOptions = array(
    'src' => 'image.png',
    'scaling' => 50,
    );
$image->addImage($imageOptions);

$data = array(
                array(
                    'ITEM' => $link1,
                    'REFERENCE' => $image,
                    'PRICE' => '5.45'
                ),
                array(
                    'ITEM' => $link2,
                    'REFERENCE' => $image,
                    'PRICE' => '30.12'
                ),
                array(
                    'ITEM' => $link3,
                    'REFERENCE' => $image,
                    'PRICE' => '7.00'
                )
        );

$docx->replaceTableVariable($data);

Please check the samples included in the package (Templates/replaceTableVariable folder).

On https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml you can read more information about WordFragments.

Regards.