Forum


Replies: 2   Views: 1256
Watermarkpdf image sizes
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 Lochspring  · 17-03-2022 - 20:25

HI there.  We're testing out v12.5, and working through some snags with PDF conversion.  ONe of the items we noticed was that, when passing in images with height and width specified, we're getting images that are consistently the WRONG size.  For example, I added a simple 600x200 jpg, using the code ffrom the examples provided, like so.  

 

<?php
// add an image watermark to an existing PDF

require_once '../../../Classes/Phpdocx/Create/CreateDocx.php';

$docx = new Phpdocx\Utilities\PdfUtilities();

$source = '../../files/Test.pdf';
$target = 'example_watermarkImage3.pdf';

$docx->watermarkPdf($source, $target, 'image', array('image' => 'test2.jpg'));

This works fine, creating my pdf as expected.  However, when I added a height and width param, like so:

 

<?php
// add an image watermark to an existing PDF

require_once '../../../Classes/Phpdocx/Create/CreateDocx.php';

$docx = new Phpdocx\Utilities\PdfUtilities();

$source = '../../files/Test.pdf';
$target = 'example_watermarkImage3.pdf';

$docx->watermarkPdf($source, $target, 'image', array('image' => 'test2.jpg', 'width' => '300', 'height' => '100'));

My image was actually generated at a LARGER size.  I wanted to verify this behavior, so I tried to generate again, only this time, I used the original proportions for my image, 600x200.

<?php
// add an image watermark to an existing PDF

require_once '../../../Classes/Phpdocx/Create/CreateDocx.php';

$docx = new Phpdocx\Utilities\PdfUtilities();

$source = '../../files/Test.pdf';
$target = 'example_watermarkImage3.pdf';

$docx->watermarkPdf($source, $target, 'image', array('image' => 'test2.jpg', 'width'=>'600', 'height'=>'200'));

The image was massive, nearly double in size, and forced itself onto the next page.

I suspect that there may be a pixel conversion problem here, but the DOCX transform doesn't seem to have the problem, only the PDF one.  Any ideas?

Posted by admin  · 17-03-2022 - 21:17

Hello,

The watermarkDocx method uses px to set height and width values. From the API doc page (https://www.phpdocx.com/api-documentation/docxutilities/insert-watermark-Word-document-with-PHP):

height int Watermark image height in pixels (optional).

width int Watermark image width in pixels (optional).

But the watermarkPdf method doesn't use pixels to set height and width values. From the API doc page (https://www.phpdocx.com/api-documentation/docxutilities/insert-watermark-pdf-document-with-PHP):

height int Height value (proportional).

width int Width value (proportional).

If you don't want to autodetect it (not setting width and height values), the exact height and width values to be used depends on the dpi and the image size. In your code, the values you are setting exceed the page size so the image is moved to the next page. For example, using a sample image included in the package (example PdfUtilities/watermarkPdf):

$docx->watermarkPdf($source, $target, 'image', array('image' => '../../files/image.png', 'width' => 78, 'height' => 64));

Regards.

Posted by Lochspring  · 18-03-2022 - 13:09

This appears to have resolved my issue. I've elected to resize the image prior to insertion into the document, and then do a secondary sizing calculation if needed after the fact.  

As a note, it might be worth mentioning the size parameter differences more explicitly, perhaps with an example calculation on dpi and size as you did in the example above.   It's not truly clear that you would need to do some math to make the PDF sizes make sense.