Forum


Replies: 5   Views: 583
Replaceplaceholderimage does not add the image
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 rabol  · 16-04-2023 - 11:01

Hi

in a Word template I have ${replace_with_image}.

I then try to replace the image like this:

$image = "data:image/png;base64,...."; // all data is not included here.

$docx->replacePlaceholderImage('replace_with_image', $image);

then I convert the docx to html but the image is not shown.

other text variables is replaced in the template correctly 

Any hints ?

 

 

Posted by admin  · 16-04-2023 - 11:08

Hello,

The replacePlaceholderImage method replaces an existing image that includes a template placeholder in the alt text with a new image. On the API doc page (https://www.phpdocx.com/api-documentation/templates/replace-placeholder-image-Word-document) you can read this same information.

If you want to replace a text placeholder by an image (or a chart, a table, a list, complex contents or others) you need to use replaceVariableByWordFragment. We recommend you check the included samples of this method and also the following documentation pages:

Regards.

Posted by rabol  · 19-04-2023 - 11:02

Thanks for the feedback.

I still need some advice:

The variables in the docment is like this:
${IMAGE_1} ${IMAGE_2}

One is on the left side and one is on the right side - in this example,

What options do I need to set to make sure the images is positioned where the variables are ?

 

 

Posted by admin  · 19-04-2023 - 11:30

Hello,

If you don't want to replace the paragraph styles that wrap the placeholder and other contents in the same paragraph when using replaceVariableByWordFragment, you need to set the type option to 'inline':

$docx->replaceVariableByWordFragment(array('WORDFRAGMENT' => $wf), array('type' => 'inline'));

Some styles may be need to applied when generating the WordFragment with the image using addImage. On addImage you can read all available options when generating an image.

Also please check the documentation available on:

https://www.phpdocx.com/documentation/snippets/adding-images-side-by-side

that details a similar case of use, although using a single placeholder.

Regards.

Posted by rabol  · 19-04-2023 - 11:57

HI

None of the options replaces the image and put the image at the position of the variable

block removed the second image
'inline' and 'inline-block' put the images on top of each other

Posted by admin  · 19-04-2023 - 12:25

Hello,

A block type replacement always removes the whole paragraph when doing the replacement, as the documentation explains:

block (default): replaces the variable and removes its containing paragraph.

An inline replacement keeps paragraph styles and other contents in the paragraph.

Maybe you need to set a custom textWrap style to the image WordFragment or apply another extra style? Please note that an image has styles that are not available in a text content.

We have done some tests and everything is working correctly. If you send to contact[at]phpdocx.com a sample DOCX template we'll check it.

Regards.