Skip to content

Commit a89e4c9

Browse files
author
Javier Garcia
committed
added exception control to file_get_contents error
1 parent 46b7bea commit a89e4c9

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

samples/resources/Sample_30_ReadHTML.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ <h1>Adding element via HTML</h1>
1212
<p>Ordered (numbered) list:</p>
1313
<ol><li>Item 1</li><li>Item 2</li></ol>
1414

15-
1615
<p style="line-height:2">Double height</p>
1716

1817
<h2>Includes images</h2>
1918
<img src="https://phpword.readthedocs.io/en/latest/_images/phpword.png" alt=""/>
19+
20+
<img src="https://localhost/gev/desarrollo/actividades/pruebas_14/5b064503587f7.jpeg" name="Imagen 12" align="bottom" width="208" height="183" border="0"/>
21+
<img src="http://localhost/gev/desarrollo/actividades/pruebas_14/5b064503589db.png" name="Imagen 13" align="bottom" width="143" height="202" border="0"/>
22+
<img src="http://localhost/gev/desarrollo/actividades/pruebas_14/5b0645035aac8.jpeg" name="Imagen 14" align="bottom" width="194" height="188" border="0"/>
23+
2024
</body>
2125
</html>

samples/results/.gitignore

100644100755
File mode changed.

src/PhpWord/Shared/Html.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ private static function parseImage($node, $element)
655655
break;
656656
}
657657
}
658+
$origin_src= $src;
658659
if (strpos($src, 'data:image') !== false) {
659660
$tmpDir = Settings::getTempDir() . '/';
660661

@@ -678,7 +679,7 @@ private static function parseImage($node, $element)
678679
}
679680

680681
if (!is_file($src)) {
681-
if ($imgBlob = file_get_contents($src)) {
682+
if ($imgBlob = @file_get_contents($src)) {
682683
$tmpDir = Settings::getTempDir() . '/';
683684
$match = array();
684685
preg_match('/.+\.(\w+)$/', $src, $match);
@@ -690,7 +691,12 @@ private static function parseImage($node, $element)
690691
fclose($ifp);
691692
}
692693
}
693-
$newElement = $element->addImage($src, $style);
694+
695+
if (is_file($src)){
696+
$newElement = $element->addImage($src, $style);
697+
}else{
698+
throw new \Exception("Could not load image $origin_src");
699+
}
694700

695701
return $newElement;
696702
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,20 @@ public function testParseRemoteLocalImage()
519519
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
520520
}
521521

522+
/**
523+
* Test parsing of remote img that can be found locally
524+
*/
525+
public function testCouldNotLoadImage()
526+
{
527+
$src = 'https://fakedomain.io/images/firefox.png';
528+
$this->expectException(\Exception::class);
529+
530+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
531+
$section = $phpWord->addSection();
532+
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/></p>';
533+
Html::addHtml($section, $html, false, true);
534+
}
535+
522536
public function testParseLink()
523537
{
524538
$phpWord = new \PhpOffice\PhpWord\PhpWord();

0 commit comments

Comments
 (0)