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

Images not rendered correctly on LibreOffice #667

Open
xprt64 opened this issue Nov 26, 2015 · 11 comments
Open

Images not rendered correctly on LibreOffice #667

xprt64 opened this issue Nov 26, 2015 · 11 comments

Comments

@xprt64
Copy link

xprt64 commented Nov 26, 2015

The images are not rendered correctly (at least on LibreOffice), they get enlarged by a few millimeters and thus get fuzzy. I resolved the issue by modifying the width and height attributes from pixels to points in PHPWord_Writer_Word2007_Base::_writeImage() like this:

$objWriter->writeAttribute('style', 'width:'.(0.75 * $width).'pt;height:'.(0.75*$height).'pt');

Environment: Linux version 3.16.0-4-amd64 ([email protected]) (gcc version 4.8.4 (Debian 4.8.4-1) ) #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09)


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@michaelachrisco
Copy link

@xprt64 Which version of LibreOffice are you using? Can you upload a sample doc? I may have ran into this bug before.

@xprt64
Copy link
Author

xprt64 commented Nov 27, 2015

Version: 4.3.3.2
Build ID: 430m0(Build:2)

The code I posted on is about an old version of PhpWord but the bug is
still there in the last stable version.

2015-11-27 15:28 GMT+02:00 michael chrisco [email protected]:

Which version of LibreOffice are you using? Can you upload a sample doc? I
may have ran into this bug before.


Reply to this email directly or view it on GitHub
#667 (comment).

Best regards,
Constantin Galbenu,
Tel: +40728247366
Skype: xprt64

@michaelachrisco
Copy link

Ill take a look on my side when I get a chance. If memory serves, the issue has something to do with the GD extension. http:https://php.net/manual/en/book.image.php

The offender might have something to do with the image element: https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/Element/Image.php

I know I was testing on MS Word and the stretching was happening.

@xprt64
Copy link
Author

xprt64 commented Nov 27, 2015

It's not about GD, the image is fine in the docx file and the dimensions
are correct on the docx/word/document.xml; it's just about LibreOffice
rendering: if the unit is pixel then it get streched as if LibreOffice
ignores the unit and uses points anyway.
The solution that worked for me was to use points as unit in the resulting
docx file.

2015-11-27 15:48 GMT+02:00 michael chrisco [email protected]:

Ill take a look on my side when I get a chance. If memory serves, the
issue has something to do with the GD extension.
http:https://php.net/manual/en/book.image.php

The offender might have something to do with the image element:
https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/Element/Image.php

I know I was testing on MS Word and the stretching was happening.


Reply to this email directly or view it on GitHub
#667 (comment).

Best regards,
Constantin Galbenu,
Tel: +40728247366
Skype: xprt64

@samsullivan
Copy link
Contributor

Blurry for me too, even in MS Word.

@samsullivan
Copy link
Contributor

I found out this is only on images from remote URLs, rather than a local path. I've changed my code from:

$section->addImage($url);

To:

$logoPath = '/tmp/image.png';
file_put_contents($logoPath, file_get_contents($url));
$section->addImage($logoPath);

// Remove temporary local file after rendering Word2007
unlink($logoPath);

Not a great solution, but it works for now! FYI, rendering of the image looked fine (before this solution) on any file type other than Word2007.

@michaelachrisco
Copy link

Huh that's a strange one....any indication why a remote URL would stretch
an image?
What was the URL if you don't mind posting?

On Wed, Apr 6, 2016 at 2:48 PM, Sam Sullivan [email protected]
wrote:

I found out this is only on images from remote URLs, rather than a local
path. I've changed my code from:

$section->addImage($url);

To:

$logoPath = '/tmp/image.png';
file_put_contents($logoPath, file_get_contents($url));
$section->addImage($logoPath);

// Remove temporary local file after rendering Word2007
unlink($logoPath);

Not a great solution, but it works for now! FYI, rendering of the image
looked fine (before this solution) on any file type other than Word2007.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#667 (comment)

@samsullivan
Copy link
Contributor

It's in quite a big tech stack using private AWS images to create custom reports, so I made an example for you using Google's logo. Something crazier than just bad quality happened...

Here's the sample code:

$document = new \PhpOffice\PhpWord\PhpWord();

$url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
$path = '/tmp/image.png';
file_put_contents($path, file_get_contents($url));

$document->addSection()->addImage($url);
$document->addSection()->addImage($path);

$file = \PhpOffice\PhpWord\IOFactory::createWriter($document, 'Word2007');
$file->save('/tmp/document.docx');

Here's the result:
screenshot from 2016-04-06 17 16 58

Also, I noticed that JPGs were much better quality -- if not perfect. You can see if you use the above example with this image. So, my bug (not sure if it's related to OP, honestly...let me know if I should open a new ticket), is narrowed to Word2007/PNG/remote.

@samsullivan
Copy link
Contributor

I've figured out why this bug exists (and it does take place in Element\Image, but the root has to do with the GD extension). I expanded on my above test:

$document = new \PhpOffice\PhpWord\PhpWord();

$url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
$path = '/tmp/image.png';

$contents = file_get_contents($url);
file_put_contents($path, $contents);

$image = imagecreatefromstring($contents);
imagepng($image, str_replace('image', 'image-2', $path));

$image = imagecreatefromstring($contents);
imagesavealpha($image, true);
imagepng($image, str_replace('image', 'image-3', $path));

// Remotely; PHPWord uses imagepng() to get contents; FAIL
$document->addSection()->addImage($url);

// Local; PHPWord just opens the file; SUCCESS
$document->addSection()->addImage($path);

// Local w/imagepng(); FAIL
$document->addSection()->addImage(str_replace('image', 'image-2', $path));

// Local w/imagesavealpha(); SUCCESS
$document->addSection()->addImage(str_replace('image', 'image-3', $path));

$file = \PhpOffice\PhpWord\IOFactory::createWriter($document, 'Word2007');
$file->save('/tmp/document.docx');

It looks like PHPWord doesn't use imagesavealpha($image, true) when saving the remote PNG files, and there for the data in said PNG files gets all screwy.

We need to find a way to work this into Element\Image...I can do a PR after I'm done getting this big project completed (since for right now downloading the image locally works just fine for me).

@michaelachrisco
Copy link

Sounds good to me.
I had thought that the GD extension might have had something to do with the
error. Thanks for the writeup!

On Thu, Apr 7, 2016 at 4:57 PM, Sam Sullivan [email protected]
wrote:

I've figured out why this bug exists (and it does take place in
Element\Image, but the root has to do with the GD extension). I expanded
on my above test:

$document = new \PhpOffice\PhpWord\PhpWord();

$url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
$path = '/tmp/image.png';

$contents = file_get_contents($url);
file_put_contents($path, $contents);

$image = imagecreatefromstring($contents);
imagepng($image, str_replace('image', 'image-2', $path));

$image = imagecreatefromstring($contents);
imagesavealpha($image, true);
imagepng($image, str_replace('image', 'image-3', $path));

// Remotely; PHPWord uses imagepng() to get contents; FAIL
$document->addSection()->addImage($url);

// Local; PHPWord just opens the file; SUCCESS
$document->addSection()->addImage($path);

// Local w/imagepng(); FAIL
$document->addSection()->addImage(str_replace('image', 'image-2', $path));

// Local w/imagesavealpha(); SUCCESS
$document->addSection()->addImage(str_replace('image', 'image-3', $path));

$file = \PhpOffice\PhpWord\IOFactory::createWriter($document, 'Word2007');
$file->save('/tmp/document.docx');

It looks like PHPWord doesn't use imagesavealpha($image, true)
http:https://php.net/manual/en/function.imagesavealpha.php when saving the
remote PNG files, and there for the data in said PNG files gets all screwy.

We need to find a way to work this into Element\Image...I can do a PR
after I'm done getting this big project completed (since for right now
downloading the image locally works just fine for me).


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#667 (comment)

@samsullivan
Copy link
Contributor

I've opened a PR for this, Michael. Didn't test it with the HTML writer (which seems to use a different method), but everything looks good and it does fix the Word2007 issue.

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

3 participants