Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Typo : Fix hardcoded macro chars in TemplateProcessor method [@glafarge](https://github.com/glafarge) in [#2618](https://github.com/PHPOffice/PHPWord/pull/2618)
- XML Reader : Prevent fatal errors when opening corrupt files or "doc" files [@mmcev106](https://github.com/mmcev106) in [#2626](https://github.com/PHPOffice/PHPWord/pull/2626)
- Documentation : Updated Comment element by [@laminga](https://github.com/laminga) in [#2650](https://github.com/PHPOffice/PHPWord/pull/2650)
- HTML Reader : Read width & height attributes in points fixing [#2589](https://github.com/PHPOffice/PHPWord/issues/2589) by [@Progi1984](https://github.com/Progi1984) in [#2654](https://github.com/PHPOffice/PHPWord/pull/2654)

### Miscellaneous

Expand Down
22 changes: 22 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,12 +902,34 @@ protected static function parseImage($node, $element)
break;
case 'width':
$width = $attribute->value;

// pt
if (false !== strpos($width, 'pt')) {
$width = Converter::pointToPixel((float) str_replace('pt', '', $width));
}

// px
if (false !== strpos($width, 'px')) {
$width = str_replace('px', '', $width);
}

$style['width'] = $width;
$style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;

break;
case 'height':
$height = $attribute->value;

// pt
if (false !== strpos($height, 'pt')) {
$height = Converter::pointToPixel((float) str_replace('pt', '', $height));
}

// px
if (false !== strpos($height, 'px')) {
$height = str_replace('px', '', $height);
}

$style['height'] = $height;
$style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;

Expand Down
60 changes: 60 additions & 0 deletions tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,66 @@ public function testParseImage(): void
self::assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
}

/**
* Test parsing of img.
*/
public function testParseImageSizeInPixels(): void
{
$src = __DIR__ . '/../_files/images/firefox.png';

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150px" height="200px" /></p>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$baseXpath = '/w:document/w:body/w:p/w:r';
self::assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
self::assertStringMatchesFormat('%Swidth:150px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
self::assertStringMatchesFormat('%Sheight:200px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
}

/**
* Test parsing of img.
*/
public function testParseImageSizeInPoints(): void
{
$src = __DIR__ . '/../_files/images/firefox.png';

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150pt" height="200pt" /></p>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$baseXpath = '/w:document/w:body/w:p/w:r';
self::assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
self::assertStringMatchesFormat('%Swidth:200px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
self::assertStringMatchesFormat('%Sheight:266.66666666667%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
}

/**
* Test parsing of img.
*/
public function testParseImageSizeWithoutUnits(): void
{
$src = __DIR__ . '/../_files/images/firefox.png';

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150" height="200" /></p>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$baseXpath = '/w:document/w:body/w:p/w:r';
self::assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
self::assertStringMatchesFormat('%Swidth:150px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
self::assertStringMatchesFormat('%Sheight:200px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
}

/**
* Test parsing of remote img.
*/
Expand Down
Loading