Skip to content

Commit a228811

Browse files
author
Javier Garcia
committed
fixes
1 parent d54cc6e commit a228811

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
use PhpOffice\PhpWord\Element\AbstractContainer;
2121
use PhpOffice\PhpWord\Element\Row;
2222
use PhpOffice\PhpWord\Element\Table;
23+
use PhpOffice\PhpWord\Settings;
2324
use PhpOffice\PhpWord\SimpleType\Jc;
2425
use PhpOffice\PhpWord\SimpleType\NumberFormat;
25-
use PhpOffice\PhpWord\Settings;
2626

2727
/**
2828
* Common Html functions
@@ -50,7 +50,7 @@ class Html
5050
* + IMG_SRC_SEARCH: optional to speed up images loading from remote url when files can be found locally
5151
* + IMG_SRC_REPLACE: optional to speed up images loading from remote url when files can be found locally
5252
*/
53-
public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true, $options = null )
53+
public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true, $options = null)
5454
{
5555
/*
5656
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
@@ -303,9 +303,8 @@ private static function parseSpan($node, &$styles)
303303
*
304304
* @todo As soon as TableItem, RowItem and CellItem support relative width and height
305305
*/
306-
private static function parseTable($node, $element, &$styles )
306+
private static function parseTable($node, $element, &$styles)
307307
{
308-
309308
$elementStyles = self::parseInlineStyle($node, $styles['table']);
310309

311310
$newElement = $element->addTable($elementStyles);
@@ -656,45 +655,46 @@ private static function parseImage($node, $element)
656655
break;
657656
}
658657
}
659-
if( strpos( $src, "data:image" ) !== false ){
660-
if( ! is_dir( self::$imgdir ) )
661-
mkdir( self::$imgdir ) ;
662-
663-
$match = array();
664-
preg_match( '/data:image\/(\w+);base64,(.+)/', $src, $match );
658+
if (strpos($src, 'data:image') !== false) {
659+
if (!is_dir(self::$imgdir)) {
660+
mkdir(self::$imgdir);
661+
}
665662

666-
$src = $imgFile = self::$imgdir . uniqid() . "." . $match[1];
663+
$match = array();
664+
preg_match('/data:image\/(\w+);base64,(.+)/', $src, $match);
667665

668-
$ifp = fopen( $imgFile, "wb");
666+
$src = $imgFile = self::$imgdir . uniqid() . '.' . $match[1];
669667

670-
fwrite($ifp, base64_decode( $match[2] ) );
671-
fclose($ifp);
668+
$ifp = fopen($imgFile, 'wb');
672669

673-
}
674-
$src= urldecode($src);
670+
fwrite($ifp, base64_decode($match[2]));
671+
fclose($ifp);
672+
}
673+
$src = urldecode($src);
675674

676-
if( ! is_file( $src )
675+
if (!is_file($src)
677676
&& !is_null(self::$options)
678677
&& isset(self::$options['IMG_SRC_SEARCH'])
679-
&& isset(self::$options['IMG_SRC_REPLACE'])){
680-
$src = str_replace( self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src );
681-
}
682-
683-
if(! is_file($src)){
684-
if($imgBlob=file_get_contents($src)){
685-
$tmpDir= Settings::getTempDir().'/';
686-
if( ! is_dir( $tmpDir ) )
687-
mkdir( $tmpDir ) ;
688-
$match = array();
689-
preg_match( '/.+\.(\w+)$/', $src, $match );
690-
$src = $tmpDir . uniqid() . "." . $match[1];
691-
692-
$ifp = fopen( $src, "wb");
693-
694-
fwrite($ifp, $imgBlob );
695-
fclose($ifp);
696-
}
697-
}
678+
&& isset(self::$options['IMG_SRC_REPLACE'])) {
679+
$src = str_replace(self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src);
680+
}
681+
682+
if (!is_file($src)) {
683+
if ($imgBlob = file_get_contents($src)) {
684+
$tmpDir = Settings::getTempDir() . '/';
685+
if (!is_dir($tmpDir)) {
686+
mkdir($tmpDir);
687+
}
688+
$match = array();
689+
preg_match('/.+\.(\w+)$/', $src, $match);
690+
$src = $tmpDir . uniqid() . '.' . $match[1];
691+
692+
$ifp = fopen($src, 'wb');
693+
694+
fwrite($ifp, $imgBlob);
695+
fclose($ifp);
696+
}
697+
}
698698
$newElement = $element->addImage($src, $style);
699699

700700
return $newElement;

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function testParseTextDecoration()
115115
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u'));
116116
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
117117
}
118+
118119
/**
119120
* Test font
120121
*/
@@ -478,16 +479,17 @@ public function testParseRemoteImage()
478479
$baseXpath = '/w:document/w:body/w:p/w:r';
479480
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
480481
}
482+
481483
/**
482484
* Test parsing of remote img that can be found locally
483485
*/
484486
public function testParseRemoteLocalImage()
485487
{
486488
$src = 'https://fakedomain.io/images/firefox.png';
487489
$localPath = __DIR__ . '/../_files/images/';
488-
$options= array(
489-
'IMG_SRC_SEARCH'=> 'https://fakedomain.io/images/',
490-
'IMG_SRC_REPLACE'=> $localPath
490+
$options = array(
491+
'IMG_SRC_SEARCH' => 'https://fakedomain.io/images/',
492+
'IMG_SRC_REPLACE' => $localPath,
491493
);
492494

493495
$phpWord = new \PhpOffice\PhpWord\PhpWord();

0 commit comments

Comments
 (0)