Forum


Replies: 2   Views: 256
Dynamic variable while using replaceplaceholderimage
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 Dale.Cooper  · 29-04-2024 - 09:25

Hi!

I have the following code in use:

$qrcode_id = $_POST['qrcode_id'];

$image_options = array(

    'height' => 2,

    'width' => 2,

    'target' => 'header',

);

$docx->replacePlaceholderImage('QRCODE', 'docs/'.$qrcode_id.".png" , $image_options);

$docx->createDocx('docs/'.$filename);

 

But I have a problem with the dynamic file url. If i use it like above, the Word Doc is broken and can't be opened. When using hard coded file path, it works. Also I tried to create the filepath first and save it to another variable and put only a variable into the replacePlaceholderImage function, but it makes no difference. Am I missing something here what might cause to break the creation of the word file while using dynamic paths?

- I've checked the dynamic handover for $qrcode_id, that's coming in correct
- The needed PNG file is on the server and readable - and it works when using the hard coded path.

 

Thank you for any tips!

 

Marc

Posted by admin  · 29-04-2024 - 09:57

Hello,

Sorry, but we are not sure if we understand your issue correctly.

Do you mean if you use the following code it runs correctly?:

$image_options = array(
    'height' => 2,
    'width' => 2,
    'target' => 'header',
);

$docx->replacePlaceholderImage('QRCODE', 'docs/image1.png' , $image_options);
$docx->createDocx('docs/'.$filename);

But if you use the following code it doesn't work?:

$qrcode_id = 'image1';

$image_options = array(
    'height' => 2,
    'width' => 2,
    'target' => 'header',
);

$docx->replacePlaceholderImage('QRCODE', 'docs/'.$qrcode_id.'.png' , $image_options);
$docx->createDocx('docs/'.$filename);

The only difference is that a variable is used to set the image path.

What is the value of $qrcode_id in your code?:

$qrcode_id = $_POST['qrcode_id'];

a string with the file path? an URL? a stream?

We have done some tests and everything is working correctly. For example, if we change the examples/Templates/replacePlaceholderImage/sample_1.php script included in the package to use a variable to set the image path and we run the following code:

$docx = new CreateDocxFromTemplate('../../files/placeholderImage.docx');

$imagePath = '../../img/';
$image_1 = array(
        'height' => 3,
        'width' => 3,
        'target' => 'header',
);

$docx->replacePlaceholderImage('HEADERIMG', $imagePath . 'logo_header.jpg', $image_1);
$docx->replacePlaceholderImage('LOGO', $imagePath . 'imageP3.png');

$docx->createDocx('example_replacePlaceholderImage_1');

the DOCX is generated correctly.

For further support please send to contact[at]phpdocx.com the most simple script that illustrates your code using phpdocx standalone (without external resources such as frameworks, databases...), the image you are adding (you can also send the DOCX template you are using) and the DOCX output you get. We'll check them.

Regards.

Posted by Dale.Cooper  · 29-04-2024 - 10:59

Thank you for your fast reply. You totally understood it right: dynamic paths not working, hardcoded it works (like in your assumed examples below).


I've sent you all relevant files via mail just a second ago.

 

Thanks again!

Marc