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\Row;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\NumberFormat;
use PhpOffice\PhpWord\Settings;
/**
* 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_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,
@ -303,9 +303,8 @@ class Html
*
* @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']);
$newElement = $element->addTable($elementStyles);
@ -656,45 +655,46 @@ class Html
break;
}
}
if( strpos( $src, "data:image" ) !== false ){
if( ! is_dir( self::$imgdir ) )
mkdir( self::$imgdir ) ;
if (strpos($src, 'data:image') !== false) {
if (!is_dir(self::$imgdir)) {
mkdir(self::$imgdir);
}
$match = array();
preg_match( '/data:image\/(\w+);base64,(.+)/', $src, $match );
$match = array();
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] ) );
fclose($ifp);
fwrite($ifp, base64_decode($match[2]));
fclose($ifp);
}
$src = urldecode($src);
}
$src= urldecode($src);
if( ! is_file( $src )
if (!is_file($src)
&& !is_null(self::$options)
&& isset(self::$options['IMG_SRC_SEARCH'])
&& isset(self::$options['IMG_SRC_REPLACE'])){
$src = str_replace( self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src );
}
&& isset(self::$options['IMG_SRC_REPLACE'])) {
$src = str_replace(self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src);
}
if(! is_file($src)){
if($imgBlob=file_get_contents($src)){
$tmpDir= Settings::getTempDir().'/';
if( ! is_dir( $tmpDir ) )
mkdir( $tmpDir ) ;
$match = array();
preg_match( '/.+\.(\w+)$/', $src, $match );
$src = $tmpDir . uniqid() . "." . $match[1];
if (!is_file($src)) {
if ($imgBlob = file_get_contents($src)) {
$tmpDir = Settings::getTempDir() . '/';
if (!is_dir($tmpDir)) {
mkdir($tmpDir);
}
$match = array();
preg_match('/.+\.(\w+)$/', $src, $match);
$src = $tmpDir . uniqid() . '.' . $match[1];
$ifp = fopen( $src, "wb");
$ifp = fopen($src, 'wb');
fwrite($ifp, $imgBlob );
fclose($ifp);
}
}
fwrite($ifp, $imgBlob);
fclose($ifp);
}
}
$newElement = $element->addImage($src, $style);
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->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
}
/**
* Test font
*/
@ -478,6 +479,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$baseXpath = '/w:document/w:body/w:p/w:r';
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
}
/**
* 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';
$localPath = __DIR__ . '/../_files/images/';
$options= array(
'IMG_SRC_SEARCH'=> 'https://fakedomain.io/images/',
'IMG_SRC_REPLACE'=> $localPath
$options = array(
'IMG_SRC_SEARCH' => 'https://fakedomain.io/images/',
'IMG_SRC_REPLACE' => $localPath,
);
$phpWord = new \PhpOffice\PhpWord\PhpWord();