Forum


Replies: 1   Views: 2442
Png image malformed ?
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 k.barendrecht  · 04-01-2018 - 15:48

Jpeg, EMF, WMF, GIF images all are fine after being added to word, but PNG images are malformed in word (the streams I use are all perfectly fine, tested by decoding the streams on the web);

Original image 

Malformed in Word

I can mail the word file upon request.

Code below:

             /** @var FlowchartDefinition $content */

                FileStreamHelper::registerStream($content);

        $image = new WordFragment($docx, 'document');
        $image->addImage([
            'name' => $content->getFileId(),
            'src' => FileStreamHelper::generateStreamUri($content),
            'dpi' => 96,
            'width' => $content->getWidth(),
            'height' => $content->getHeight(),
            'target' => 'document',
                        'mime' => $content->getMimeType()
                ]);

        $docx->replaceVariableByWordFragment([$placeHolder => $image], ['type' => 'inline']);

                FileStreamHelper::unregisterStream($content);

Posted by admin  · 04-01-2018 - 16:48

Hello,

We have done quick tests with your image and it's being added perfectly.

Adding an image in a DOCX created from scratch:

<?php

require_once 'Classes/Phpdocx/Create/CreateDocx.inc';

$docx = new Phpdocx\Create\CreateDocx();

$options = array(
    'src' => 'afbeeldung.png',
    'height' => 300,
    'width' => 600,
    'dpi' => 96,
);

$docx->addImage($options);

$docx->createDocx('output');

Replacing a placeholder in a template:

<?php

require_once 'Classes/Phpdocx/Create/CreateDocx.inc';

$docx = new Phpdocx\Create\CreateDocxFromTemplate('examples/files/TemplateWordFragment_1.docx');

$image = new Phpdocx\Elements\WordFragment($docx, 'document');
$image->addImage(array(
    'src' => 'afbeeldung.png',
    'height' => 300,
    'width' => 600,
    'dpi' => 96,
));

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

$docx->createDocx('output_2');

In both cases, the image appears fine. Please run both scripts and check the DOCX outputs (you can find TemplateWordFragment_1.docx in the examples/files folder of the package).

We think the problem may come from some special content or filter in the template or that it's being added as 96 dpi and the image is 72 dpi. If you send the DOCX generated by your script to contact[at]phpdocx.com the dev team will check it if the problem is when the image is added or when it's opened by MS Word.

Regards.