phpdocx add image makes docx corrupt

724 Views Asked by At

It has been asked before, years ago, but with no answer corrupt docx when adding image

Hey

I have a template where i replace some variables and generate a docx document... That works fine..

As soon as i add an image, word tells me that the files is corrupt. But letting Word correct the error gives me the document as i expect it to be with the image.

Im adding image like this...

$docx->addBreak(array('type' => 'page'));
            $options = array(
            'src' => $value['110333870356bc784cf1b6b0.56722572.png'],
            'imageAlign' => 'center',
            'scaling' => 100,
            'spacingTop' => 10,
            'spacingBottom' => 0,
            'spacingLeft' => 0,
            'spacingRight' => 20,
            'textWrap' => 0,

            );
            $docx->addImage($options); 

Im lost here .. ? ..

2

There are 2 best solutions below

0
On BEST ANSWER

After being in contact with 2mdc (Creator of phpDocX) they told me, that PHP is not able to get the right extension because of the filename of the image i am inserting. As a result phpDocX adds some wrong values (such as dpi and image extension name).

As mentioned in my question, Word is able to detect those errors and correct them.

Changed my routine which creates my "unique" image names, to make sure that dot's are not present and it fixed the problem ..

As a result, filename: 110333870356bc784cf1b6b0.56722572.png will instead become 110333870356bc784cf1b6b0656722572.png (or other generated filename).

0
On

Just to share my experience. I had exactly the same problem but not for the same reason. It was because the image file extension I wanted to insert (addImage) was in capital letter. Example :

  • image.JPG ==> result a "corrupt" file.
  • image.jpg ==> result OK !

I hope it will help someone...