Skip to content

Commit

Permalink
Merge pull request #2459 from JokubasR/fix/html-image-parser
Browse files Browse the repository at this point in the history
Fix Html::parseImage when image has no extension
  • Loading branch information
Progi1984 committed Sep 3, 2023
2 parents c17c4c7 + 03246d7 commit 08f3fa5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- PclZip : strtr using empty string by @spl1nes in #2432
- Fixed PHP 8.2 deprecated about Allow access to an undefined property by @DAdq26 in #2440
- Template Processor : Fixed choose dimention for Float Value by @gdevilbat in #2449
- HTML Parser : Fix image parsing from url without extension by @JokubasR in #2459

### Miscellaneous

Expand Down
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://php.net/images/logos/new-php-logo.png';
}

protected static function getRemoteImageUrlWithoutExtension(): string
{
if (self::$httpServer) {
return self::getBaseUrl() . '/images/new-php-logo';
}

return 'http://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 08f3fa5

Please sign in to comment.