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

Fix Html::parseImage when image has no extension #2459

Merged
merged 1 commit into from
Sep 3, 2023
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
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:https://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:https://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: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.
Loading