This commit is contained in:
Javier Garcia 2018-05-23 18:48:28 +02:00
parent d54cc6efee
commit a228811a61
2 changed files with 38 additions and 36 deletions

View File

@ -20,9 +20,9 @@ namespace PhpOffice\PhpWord\Shared;
use PhpOffice\PhpWord\Element\AbstractContainer; use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\Element\Row; use PhpOffice\PhpWord\Element\Row;
use PhpOffice\PhpWord\Element\Table; use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\NumberFormat; use PhpOffice\PhpWord\SimpleType\NumberFormat;
use PhpOffice\PhpWord\Settings;
/** /**
* Common Html functions * Common Html functions
@ -50,7 +50,7 @@ class Html
* + IMG_SRC_SEARCH: optional to speed up images loading from remote url when files can be found locally * + IMG_SRC_SEARCH: optional to speed up images loading from remote url when files can be found locally
* + IMG_SRC_REPLACE: optional to speed up images loading from remote url when files can be found locally * + IMG_SRC_REPLACE: optional to speed up images loading from remote url when files can be found locally
*/ */
public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true, $options = null ) public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true, $options = null)
{ {
/* /*
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element, * @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
@ -303,9 +303,8 @@ class Html
* *
* @todo As soon as TableItem, RowItem and CellItem support relative width and height * @todo As soon as TableItem, RowItem and CellItem support relative width and height
*/ */
private static function parseTable($node, $element, &$styles ) private static function parseTable($node, $element, &$styles)
{ {
$elementStyles = self::parseInlineStyle($node, $styles['table']); $elementStyles = self::parseInlineStyle($node, $styles['table']);
$newElement = $element->addTable($elementStyles); $newElement = $element->addTable($elementStyles);
@ -656,45 +655,46 @@ class Html
break; break;
} }
} }
if( strpos( $src, "data:image" ) !== false ){ if (strpos($src, 'data:image') !== false) {
if( ! is_dir( self::$imgdir ) ) if (!is_dir(self::$imgdir)) {
mkdir( self::$imgdir ) ; mkdir(self::$imgdir);
}
$match = array(); $match = array();
preg_match( '/data:image\/(\w+);base64,(.+)/', $src, $match ); preg_match('/data:image\/(\w+);base64,(.+)/', $src, $match);
$src = $imgFile = self::$imgdir . uniqid() . "." . $match[1]; $src = $imgFile = self::$imgdir . uniqid() . '.' . $match[1];
$ifp = fopen( $imgFile, "wb"); $ifp = fopen($imgFile, 'wb');
fwrite($ifp, base64_decode( $match[2] ) ); fwrite($ifp, base64_decode($match[2]));
fclose($ifp); fclose($ifp);
}
$src = urldecode($src);
} if (!is_file($src)
$src= urldecode($src);
if( ! is_file( $src )
&& !is_null(self::$options) && !is_null(self::$options)
&& isset(self::$options['IMG_SRC_SEARCH']) && isset(self::$options['IMG_SRC_SEARCH'])
&& isset(self::$options['IMG_SRC_REPLACE'])){ && isset(self::$options['IMG_SRC_REPLACE'])) {
$src = str_replace( self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src ); $src = str_replace(self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src);
} }
if(! is_file($src)){ if (!is_file($src)) {
if($imgBlob=file_get_contents($src)){ if ($imgBlob = file_get_contents($src)) {
$tmpDir= Settings::getTempDir().'/'; $tmpDir = Settings::getTempDir() . '/';
if( ! is_dir( $tmpDir ) ) if (!is_dir($tmpDir)) {
mkdir( $tmpDir ) ; mkdir($tmpDir);
$match = array(); }
preg_match( '/.+\.(\w+)$/', $src, $match ); $match = array();
$src = $tmpDir . uniqid() . "." . $match[1]; preg_match('/.+\.(\w+)$/', $src, $match);
$src = $tmpDir . uniqid() . '.' . $match[1];
$ifp = fopen( $src, "wb"); $ifp = fopen($src, 'wb');
fwrite($ifp, $imgBlob ); fwrite($ifp, $imgBlob);
fclose($ifp); fclose($ifp);
} }
} }
$newElement = $element->addImage($src, $style); $newElement = $element->addImage($src, $style);
return $newElement; return $newElement;

View File

@ -115,6 +115,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u'));
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
} }
/** /**
* Test font * Test font
*/ */
@ -478,6 +479,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$baseXpath = '/w:document/w:body/w:p/w:r'; $baseXpath = '/w:document/w:body/w:p/w:r';
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape')); $this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
} }
/** /**
* Test parsing of remote img that can be found locally * Test parsing of remote img that can be found locally
*/ */
@ -485,9 +487,9 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
{ {
$src = 'https://fakedomain.io/images/firefox.png'; $src = 'https://fakedomain.io/images/firefox.png';
$localPath = __DIR__ . '/../_files/images/'; $localPath = __DIR__ . '/../_files/images/';
$options= array( $options = array(
'IMG_SRC_SEARCH'=> 'https://fakedomain.io/images/', 'IMG_SRC_SEARCH' => 'https://fakedomain.io/images/',
'IMG_SRC_REPLACE'=> $localPath 'IMG_SRC_REPLACE' => $localPath,
); );
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();