Skip to content

Commit

Permalink
Fix Html::parseImage when image has no extension
Browse files Browse the repository at this point in the history
  • Loading branch information
JokubasR committed Sep 3, 2023
1 parent c17c4c7 commit b9a0dff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,10 @@ protected static function parseImage($node, $element)
$tmpDir = Settings::getTempDir() . '/';
$match = [];
preg_match('/.+\.(\w+)$/', $src, $match);
$src = $tmpDir . uniqid() . '.' . $match[1];
$src = $tmpDir . uniqid();
if (isset($match[1])) {
$src .= '.' . $match[1];
}

$ifp = fopen($src, 'wb');

Expand Down
9 changes: 9 additions & 0 deletions tests/PhpWordTests/AbstractWebServerEmbeddedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ protected static function getRemoteImageUrl()
return 'http:https://php.net/images/logos/new-php-logo.png';
}

protected static function getRemoteImageUrlWithoutExtension()

Check failure on line 56 in tests/PhpWordTests/AbstractWebServerEmbeddedTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWordTests\AbstractWebServerEmbeddedTest::getRemoteImageUrlWithoutExtension() has no return type specified.
{
if (self::$httpServer) {
return self::getBaseUrl() . '/images/new-php-logo';
}

return 'http:https://placekitten.com/200/300';
}

protected static function getRemoteGifImageUrl()
{
if (self::$httpServer) {
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,24 @@ public function testParseRemoteImage(): void
self::assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
}

/**
* Test parsing of remote img without extension.
*/
public function testParseRemoteImageWithoutExtension(): void
{
$src = self::getRemoteImageUrlWithoutExtension();

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/><img src="' . $src . '" style="float: left;"/></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'));
}

/**
* Test parsing embedded image.
*/
Expand Down
Binary file added tests/PhpWordTests/_files/images/new-php-logo
Binary file not shown.

0 comments on commit b9a0dff

Please sign in to comment.