Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image src as base64 is not working #1246

Closed
1 of 3 tasks
rebbieboi opened this issue Jan 13, 2018 · 9 comments
Closed
1 of 3 tasks

Image src as base64 is not working #1246

rebbieboi opened this issue Jan 13, 2018 · 9 comments

Comments

@rebbieboi
Copy link

rebbieboi commented Jan 13, 2018

This is:

Expected Behavior

Should display the image

Current Behavior

Image is not displayed

Failure Information

I get this errors instead:
Uncaught PhpOffice\PhpWord\Exception\InvalidImageException: Invalid image: data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEAYABgAAD/7gAOQWRv.....

Please help provide information about the failure.

How to Reproduce

I am using a base64 image

Context

  • PHP version: 7
  • PHPWord version: 0.14
@troosan
Copy link
Contributor

troosan commented Jan 14, 2018

can you give a code sample that reproduces the issue?
I think you should strip out the first part of the string (data:image/jpeg;base64,) which is not part of the base64 encoded data.

@rebbieboi
Copy link
Author

I had to change my approach since using base64 as an image source was not resource friendly. Though there seems to be a problem when img tag is not closed. Is there a fix for this instead or do I have to manually close the img tag which would be tricky for my part since the content is PHP generated.

@troosan
Copy link
Contributor

troosan commented Jan 14, 2018

@rebbieboi right now, as we are loading XML and not HTML, the tag needs to be closed as otherwise it is not valid XML.

@rebbieboi
Copy link
Author

@troosan Thank you for that information. :) Last question if you don't mind. How can I set default dimensions for the content generated from an HTML source? Images tend to take a full 100% width meaning it won't sometimes fit.

@troosan
Copy link
Contributor

troosan commented Jan 16, 2018

when you addHTML, you add it to a container. I guess if you set a fixed with to that container the content should not be wider than that.

@troosan troosan closed this as completed Feb 10, 2018
@padre
Copy link

padre commented Apr 10, 2018

@rebbieboi Have you been able to fix it?

I have the same problem with img base64:

"Invalid image: data:image/png;base64,iVBORw0..."

@freretuc
Copy link

Hi, I have the same issue.

If I try to load HTML directly to phpWord, img is not correctly used and I have nothing in my document.
If I close the tag by putting a <img .... /> or <img ....>, the error is displayed.

word-image.php.zip

I solve this in 3 places :
\Shared\Html.php
// Load DOM
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = $preserveWhiteSpace;
$dom->loadXML($html);
$node = $dom->getElementsByTagName('body');
changes to :
// Load DOM
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = $preserveWhiteSpace;
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
$node = $dom->getElementsByTagName('body');
(the xml encoding is to continue the UTF-8 encoding from loadXML).

\Element\Image.php
in the function getStringImageSize
the result of $result = @getimagesizefromstring($source); is bad with the string, so I remove the base64 tag and decode before sending the data to the function
private function getStringImageSize($source)
{
if(preg_match('/^(.*base64,)/m', $source)) {
$source = base64_decode(preg_replace('/^(.*base64,)/m', '', $source));
}
$result = false;

\Writer\AbstractWriter.php
in the function addFilesToPackage, same as in getStringImageSize
if (isset($element['isMemImage']) && $element['isMemImage']) {
if(preg_match('/^(.*base64,)/m', $element['source'])) {
$element['source'] = base64_decode(preg_replace('/^(.*base64,)/m', '', $element['source']));
}

The must would be to remove the base64 part when it's decoded from HTML. Can you help me to put it in the right place ?

@freretuc
Copy link

freretuc commented Jun 24, 2018

I've find better : No need to update Image.php & AbstractWriter.php
In the function parseImage in HTML.php

    private static function parseImage($node, $element)
    {
        $style = array();
        $src = null;
        foreach ($node->attributes as $attribute) {
            switch ($attribute->name) {
                case 'src':
                    $src = $attribute->value;
                    if(preg_match('/^(.*base64,)/m', $src)) {
                      $src = base64_decode(preg_replace('/^(.*base64,)/m', '', $src));
                    }
                    break;

@troosan
Copy link
Contributor

troosan commented Jul 9, 2018

@freretuc was this not fixed with #1382

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants