Skip to content

Commit 08f3fa5

Browse files
authored
Merge pull request #2459 from JokubasR/fix/html-image-parser
Fix Html::parseImage when image has no extension
2 parents c17c4c7 + 03246d7 commit 08f3fa5

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2121
- PclZip : strtr using empty string by @spl1nes in #2432
2222
- Fixed PHP 8.2 deprecated about Allow access to an undefined property by @DAdq26 in #2440
2323
- Template Processor : Fixed choose dimention for Float Value by @gdevilbat in #2449
24+
- HTML Parser : Fix image parsing from url without extension by @JokubasR in #2459
2425

2526
### Miscellaneous
2627

src/PhpWord/Shared/Html.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,10 @@ protected static function parseImage($node, $element)
965965
$tmpDir = Settings::getTempDir() . '/';
966966
$match = [];
967967
preg_match('/.+\.(\w+)$/', $src, $match);
968-
$src = $tmpDir . uniqid() . '.' . $match[1];
968+
$src = $tmpDir . uniqid();
969+
if (isset($match[1])) {
970+
$src .= '.' . $match[1];
971+
}
969972

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

tests/PhpWordTests/AbstractWebServerEmbeddedTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ protected static function getRemoteImageUrl()
5353
return 'http://php.net/images/logos/new-php-logo.png';
5454
}
5555

56+
protected static function getRemoteImageUrlWithoutExtension(): string
57+
{
58+
if (self::$httpServer) {
59+
return self::getBaseUrl() . '/images/new-php-logo';
60+
}
61+
62+
return 'http://placekitten.com/200/300';
63+
}
64+
5665
protected static function getRemoteGifImageUrl()
5766
{
5867
if (self::$httpServer) {

tests/PhpWordTests/Shared/HtmlTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,24 @@ public function testParseRemoteImage(): void
816816
self::assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
817817
}
818818

819+
/**
820+
* Test parsing of remote img without extension.
821+
*/
822+
public function testParseRemoteImageWithoutExtension(): void
823+
{
824+
$src = self::getRemoteImageUrlWithoutExtension();
825+
826+
$phpWord = new PhpWord();
827+
$section = $phpWord->addSection();
828+
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/><img src="' . $src . '" style="float: left;"/></p>';
829+
Html::addHtml($section, $html);
830+
831+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
832+
833+
$baseXpath = '/w:document/w:body/w:p/w:r';
834+
self::assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
835+
}
836+
819837
/**
820838
* Test parsing embedded image.
821839
*/
10.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)