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 produced by templateProcessor->setImageValue look different in Windows versus Mac #2576

Closed
sookoll opened this issue Feb 23, 2024 · 1 comment

Comments

@sookoll
Copy link

sookoll commented Feb 23, 2024

Describe the Bug

It seems to have same/related issue as these old (fixed) ones:
#224
#227

Images created with templateProcessor->setImageValue and open in windows vs mac are different in size. I tried to add images into table cell and after tweaking dimensions (images in pt, cell width in some weird Twentieths of a Point) I got image fit into cell correctly in LibreOffice in Mac. However, when open MS Word in Windows, images are rendered in different size.

Steps to Reproduce

Please provide a code sample that reproduces the issue.

In template docx I have template variable for table ${table}

<?php
require __DIR__ . '/vendor/autoload.php';

$templateProcessor = new TemplateProcessor('path/to/save/template.docx');

$cellRowSpan = ['vMerge' => 'restart'];
$cellRowContinue = ['vMerge' => 'continue'];
$cellColSpan = ['gridSpan' => 2];

$table = new Table([
    'borderSize' => 6,
    'borderColor' => '000000',
    'cellMargin' => 75,
    'layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO,
     'unit' => \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP
 ]);

$table->addRow();
$table->addCell(1400, $cellRowSpan)->addText('Some title');
$table->addCell(8200, $cellColSpan)->addText('Some description');

$table->addRow();
$table->addCell(1400, $cellRowContinue);
$table->addCell(6000)->addText('${map}');
$table->addCell(2200)->addText('${legend}');

$templateProcessor->setComplexBlock('table', $table);

$templateProcessor->setImageValue('map', [
    'path' => 'base64string',
    'width' => 600,
    'height' => '',
    'ratio' => true
]);
$templateProcessor->setImageValue('legend', [
    'path' => 'image_url',
    'width' => '',
    'height' => '',
    'ratio' => true
]);

$templateProcessor->saveAs('path/to/save/file.docx');

Expected Behavior

Images rendered in document are same size regardless operating system.

Current Behavior

Images are rendered in different size. And it seems, that it also depends on size. If I set image size to 300, they seems to have almost identical. 400, already big difference. Below are screenshots how 300, 400 and 600 looks in mac vs win.

300 mac
scrn-mac-300

300 win
scrn-win-300

400 mac
scrn-mac-400

400 win
scrn-win-400

600 mac
scrn-mac-600

600 win
scrn-win-600

In windows, all images above 300 are clearly larger (indicated by activated image corners), and do not fit into cell in case of 600. Width doesn't seems to be linear unit at all in windows. 400 seems to be 2x larger than 300. Same thing happens in LibreOffice in Windows.

Also, I wasn't able to use $cell->addImage, no image was rendered, but that's another problem.

Context

Please fill in your environment information:

  • PHP Version: 8.1
  • PHPWord Version: 1.2.0
@sookoll sookoll changed the title Images produced by templateProcessor->setImageValue look different in Word for Windows and Word for Mac Images produced by templateProcessor->setImageValue look different in Windows versus Mac Feb 23, 2024
@sookoll
Copy link
Author

sookoll commented Apr 25, 2024

Update: I found a solution for me. Just had to use pt instead px, which is default, if You provide number without unit.

$templateProcessor->setImageValue('map', [
    'path' => 'base64string',
    'width' => '290pt',
    'height' => '',
    'ratio' => true
]);

@sookoll sookoll closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant