Skip to content

Commit 41ae435

Browse files
committed
Optimize test cases and handle image loading failures
1 parent 6c0c8f8 commit 41ae435

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/PhpWord/TemplateProcessor.php

+14
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
327327
{
328328
$phpWord = new PhpWord();
329329
$section = $phpWord->addSection();
330+
//deal remote load Image
331+
$pattern = '/<img[^>]+src\s*=\s*["\']([^"\']+)["\'][^>]*>/i';
332+
preg_match_all($pattern, $htmlContent, $matches);
333+
$imageSrcList = $matches[1];
334+
if (!empty($imageSrcList)) {
335+
foreach ($imageSrcList as $imageSrc) {
336+
try {
337+
file_get_contents($imageSrc);
338+
}catch (\Exception $e) {
339+
$localImg = __DIR__.'/resources/doc.png';
340+
$htmlContent = str_replace($imageSrc, $localImg, $htmlContent);
341+
}
342+
}
343+
}
330344
Html::addHtml($section, $htmlContent, $fullHtml);
331345
$zip = $this->zip();
332346
$obj = new Word2007($phpWord);

tests/PhpWordTests/TemplateProcessorTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1634,8 +1634,10 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
16341634
public function testSetHtml(): void
16351635
{
16361636
Settings::setOutputEscapingEnabled(true);
1637-
$content = '<p><img src="https://t7.baidu.com/it/u=4198287529,2774471735&fm=193&f=GIF" /></p>
1638-
<p><img src="https://t7.baidu.com/it/u=4069854689,43753836&fm=193&f=GIF" /></p>
1637+
$image1 = __DIR__.'/_files/images/earth.jpg';
1638+
$image2 = __DIR__.'/_files/images/mars.jpg';
1639+
$content = '<p><img src="'.$image1.'" /></p>
1640+
<p><img src="'.$image2.'" /></p>
16391641
<p>HPJ LDAP(Lightweight Directory Access Protocol),轻量级目录访问协议,是一种在线目录访问协议,主要用于目录中资源的搜索和查询。如果在用户可控制的输入中没有对 LDAP 语法进行除去或引用,那么生成的 LDAP 查询可能会导致</p>';
16401642
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/template_to_html.docx');
16411643
$templateProcessor->setHtmlBlock('html_content', $content);

0 commit comments

Comments
 (0)