Forum


Replies: 7   Views: 3616
Issue with <img> in html
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 meantimeit  · 19-02-2018 - 10:00

I have two test <img> tags in a HTML fragment - the first image is a direct link via a HTTP request, the second is to the same image but accessed via a PHP script that echoes out the file_get_contents().

I can access both the above images using the appropriate URLs so they are working individually but the generated DOCX only shows the directly linked image, the image via PHP is not shown at all, not even a broken image.

The first URL is in the form http://our-test-site/images/logo.jpg
The second is in the form http://our-test-site/images/test.php

If I change the second URL to http://our-test-site/images/test.php?image.jpg then the generated DOCX shows a broken image placeholder instead of nothing.

Any ideas? Do I need to send specific headers? Does the <img> tag need anything special? Is there a bug in the phpdocx software? Is there anything else I need to provide to help with a diagnosis?

Thanks,
D.

Posted by meantimeit  · 22-02-2018 - 11:13

If I disable the image downloads then both images appear (Word fetches the images on-the-fly), re-enabling image downloads still only shows the directly linked image.

If the URL to the image hosted by PHP doesn't end in *.jpg then the image doesn't appear in the Word document no matter if downloading images is enabled or not.

Enabling image downloads (it was already enabled) embeds the images into the Word document but there's something wrong with the way they are linked internally.

Any ideas? I've read through the information and there's nothing helpful there. What could be causing PHPDocX to be breaking the links with the embedded image?

Posted by meantimeit  · 22-02-2018 - 12:07

In addition - if I rename the .docx file to .zip and explore the contents I can see all the required files are existing in a readable state in the media folder but for some reason the link to them in the document is broken.

PHPDocX appears to be downloading the images correctly but something is going wrong when it tries to link these streamed images. I've got the fully licenced copy so I can attempt to debug your code.

If I find anything I'll let you know but any hints or ideas would be helpful.

Posted by admin  · 22-02-2018 - 12:20

Hello,

When a stream is added as src of an image, the embedHTML method uses exif_imagetype from PHP to get the mime type. Maybe the server is not returning the correct mime type of the image or for any other reason PHP can't get it correctly or the file_get_contents method is unable to download the stream content from the remote URL due to some permission access.

If you want to debug it, the image is downloaded and added in the HTML2WorML.inc file (line around 981).

The dev team has done some quick tests and everything is running fine (using the test servers and samples). If you send the DOCX output generated by your server to contact[at]phpdocx.com, they'll check it.

If you also send the most simple script (a single HTML with two existing remote imgs: a static image and a stream) that illustrate your issue they'll check it; they need to run it, so please send the most simple script that doesn't do external connections such as databases or web services.

Regards.

Posted by meantimeit  · 22-02-2018 - 12:53

External access to our development environment is forbidden so I can't provide a testable example for you.

Opening up the DOCX file word/_rels/document.xml.rels shows the embedded JPG files saved with a .php extension. If I manually change .php to .jpg then the embedded images appear. (Rename the .docx as a .zip, navigate to the appropriate component, copy that to the desktop, edit it on the desktop, copy it back by overwriting, rename the file back as a .docx)

The headers we're sending should be correct. What headers do you send with your minimal example for the streamed image? Is there a URL I can use so I can compare your headers against ours?

Posted by admin  · 22-02-2018 - 13:04

Hello,

The mime type on one of our test server is image/jpeg for a JPEG image. Anyway, the important point is the output of exif_imagetype. As we can't do a direct test, we think the best approach we can recommend is to debug the image generation, the source of the issue should be in HTML2WorML.inc, around line 981, when the code gets the extension of the image ($extension variable), maybe you can test changing the extension detection or forcing an extension.

If you are using PHP5.4 or newer you can use the following code to get the extension:

$imageStream = file_get_contents($data['src']);
$attrImage = getimagesizefromstring($imageStream);
$mimeType = $attrImage['mime'];

switch ($mimeType) {
    case 'image/gif':
        $dir['extension'] = 'gif';
        break;
    case 'image/jpg':
        $dir['extension'] = 'jpg';
        break;
    case 'image/jpeg':
        $dir['extension'] = 'jpeg';
        break;
    case 'image/png':
        $dir['extension'] = 'png';
        break;
    default:
    break;
}

The embedHTML method uses other code due to compatibility reasons with old version of PHP.

Regards.

Posted by meantimeit  · 22-02-2018 - 15:23

* RESOLVED *

I changed the code to something very similar to your example (only on 5.3 so just used getimagesize() directly) but the document couldn't open due to an unknown error.

The <img> tags that wre now working still contained deprecated in-line styling parameters such as align, width & height. Once these were removed the generated document could be opened and contained all the required images.

Many thanks.