Forum


Replies: 3   Views: 707
Image url also getting printed in .docx file
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 RealTechs  · 03-11-2022 - 18:00

Hi,

I need a help when i am trying to add images in .docx file.

It got inserted in .docx file correctly bu having 2 issues as described below:

  • Whole image url gets printed where i set the placeholders in .docx file
  • Images are adding at the end of .docx file.and not where i placed placeholders

Here is the code i am using:

$options = array(
        'src' => $imageUrl,
        'imageAlign' => 'center',
        'scaling' => 50,
        'spacingTop' => 10,
        'spacingBottom' => 0,
        'spacingLeft' => 0,
        'spacingRight' => 20,
        'textWrap' => 0,
        'borderStyle' => 'lgDash',
        'borderWidth' => 6,
        'borderColor' => 'FF0000',
);

$docX->addImage($options);

Am i doing something wrong or missing something?

Thanks in advance for your help.

Harry

Posted by admin  · 03-11-2022 - 18:32

Hello,

The addImage method adds a new image, it doesn't replace a text placeholder or an existing image in a DOCX template.

You need to use replacePlaceholderImage to replace an existing image that includes a placeholder added as alternative text. Or replaceVariableByWordFragment to replace a text placeholder by a new image.

On the following practical guide: Pictures and images , and the API documentation pages of the methods you can find further information and samples. You can also find more samples in the package.

Also note the following:

  • Adding a stream with addImage may require enabling the streamMode available in the method. As detailed on the API documention pages:

src

The path to the image or base64 string or stream to be inserted into the Word document. If a stream is added, use with the streamMode option.

Regards.

Posted by RealTechs  · 05-11-2022 - 09:09

Hello, 

Its still not working. I have added ALT parameter in image placeholder in .docx file but somehow its not displaying image. Instead it is showing local image path. Please tell me what i am missing ?

I have done exactly the same thing as mentioned in this url: https://stackoverflow.com/questions/30989265/phpdocx-replaceplaceholderimage   exept that i am using latest MS Word where the process of assinging ALT is different.

Could you please help me here ?

Thanks,

Posted by admin  · 05-11-2022 - 09:27

Hello,

All methods and features of phpdocx are fully tested and working with all MS Word versions and other DOCX editors. If the new image is not being displayed when doing the replacement, then maybe PHP can't download the remote image (HTTPs connection issue or missing streamMode that is required to get remote images with replacePlaceholderImage and addImage as detailed on our previous reply or any other external issue).

If you send to contact[at]phpdocx.com the most simple DOCX template and code you are running (using phpdocx standalone without using databases, web services, frameworks, CMS...), this is the code you are using to load the template and replace the image with the new image URL you want to add, we'll check it.

Also please note that MS Word 2021 support was added in phpdocx 12 (https://www.phpdocx.com/news/post/phpdocx-v12-release-notes/223). If you are using a previous version of phpdocx, we recommend you upgrade your license to the latest version to get full support with all MS Word versions.

Regards.