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 */


        $image = new WordFragment($docx, 'document');
            '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']);


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


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

Adding an image in a DOCX created from scratch:


require_once 'Classes/Phpdocx/Create/';

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

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



Replacing a placeholder in a template:


require_once 'Classes/Phpdocx/Create/';

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

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

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


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] the dev team will check it if the problem is when the image is added or when it's opened by MS Word.