diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 07d23e27..3baa15e0 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -27,7 +27,6 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; -use PhpOffice\PhpWord\TOC; /** * HTML writer @@ -170,10 +169,10 @@ class HTML extends AbstractWriter implements WriterInterface $html .= $this->writeImage($element); } elseif ($element instanceof Object) { $html .= $this->writeObject($element); - } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element); } elseif ($element instanceof Endnote) { $html .= $this->writeEndnote($element); + } elseif ($element instanceof Footnote) { + $html .= $this->writeFootnote($element); } } } @@ -257,10 +256,10 @@ class HTML extends AbstractWriter implements WriterInterface $html .= $this->writeTextBreak($element, true); } elseif ($element instanceof Image) { $html .= $this->writeImage($element, true); - } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element); } elseif ($element instanceof Endnote) { $html .= $this->writeEndnote($element); + } elseif ($element instanceof Footnote) { + $html .= $this->writeFootnote($element); } } $html .= '

' . PHP_EOL; @@ -402,10 +401,10 @@ class HTML extends AbstractWriter implements WriterInterface $html .= $this->writeImage($content); } elseif ($content instanceof Object) { $html .= $this->writeObject($content); - } elseif ($element instanceof Footnote) { - $html .= $this->writeFootnote($element); } elseif ($element instanceof Endnote) { $html .= $this->writeEndnote($element); + } elseif ($element instanceof Footnote) { + $html .= $this->writeFootnote($element); } } } else { @@ -434,9 +433,13 @@ class HTML extends AbstractWriter implements WriterInterface if (!$this->isPdf) { $imageData = $this->getBase64ImageData($element); if (!is_null($imageData)) { - $html = ''; + $style = $this->assembleCss(array( + 'width' => $element->getStyle()->getWidth() . 'px', + 'height' => $element->getStyle()->getHeight() . 'px', + )); + $html = ""; if (!$withoutP) { - $html = '

' . $html . '

' . PHP_EOL; + $html = "

{$html}

" . PHP_EOL; } } } @@ -626,11 +629,12 @@ class HTML extends AbstractWriter implements WriterInterface private function getBase64ImageData(Image $element) { $imageData = null; + $imageBinary = null; $source = $element->getSource(); $imageType = $element->getImageType(); - // Get actual source - if ($element->getSourceType() == 'archive') { + // Get actual source from archive image + if ($element->getSourceType() == Image::SOURCE_ARCHIVE) { $source = substr($source, 6); list($zipFilename, $imageFilename) = explode('#', $source); $zip = new \ZipArchive(); @@ -646,10 +650,20 @@ class HTML extends AbstractWriter implements WriterInterface } // Read image binary data and convert into Base64 - if ($fp = fopen($actualSource, "rb", 0)) { - $image = fread($fp, filesize($actualSource)); - fclose($fp); - $base64 = chunk_split(base64_encode($image)); + if ($element->getSourceType() == Image::SOURCE_GD) { + $imageResource = call_user_func($element->getImageCreateFunction(), $actualSource); + ob_start(); + call_user_func($element->getImageFunction(), $imageResource); + $imageBinary = ob_get_contents(); + ob_end_clean(); + } else { + if ($fp = fopen($actualSource, 'rb', false)) { + $imageBinary = fread($fp, filesize($actualSource)); + fclose($fp); + } + } + if (!is_null($imageBinary)) { + $base64 = chunk_split(base64_encode($imageBinary)); $imageData = 'data:' . $imageType . ';base64,' . $base64; } diff --git a/tests/PhpWord/Tests/Writer/HTMLTest.php b/tests/PhpWord/Tests/Writer/HTMLTest.php index 40ea88b0..354da0e6 100644 --- a/tests/PhpWord/Tests/Writer/HTMLTest.php +++ b/tests/PhpWord/Tests/Writer/HTMLTest.php @@ -45,7 +45,9 @@ class HTMLTest extends \PHPUnit_Framework_TestCase */ public function testSave() { - $imageSrc = __DIR__ . "/../_files/images/PhpWord.png"; + $localImage = __DIR__ . "/../_files/images/PhpWord.png"; + $archiveImage = 'zip://' . __DIR__ . '/../_files/documents/reader.docx#word/media/image1.jpeg'; + $gdImage = 'http://php.net/images/logos/php-med-trans-light.gif'; $objectSrc = __DIR__ . "/../_files/documents/sheet.xls"; $file = __DIR__ . "/../_files/temp.html"; @@ -64,7 +66,9 @@ class HTMLTest extends \PHPUnit_Framework_TestCase $section->addTitle('Test', 1); $section->addPageBreak(); $section->addListItem('Test'); - $section->addImage($imageSrc); + $section->addImage($localImage); + $section->addImage($archiveImage); + $section->addImage($gdImage); $section->addObject($objectSrc); $section->addFootnote(); $section->addEndnote(); @@ -77,7 +81,7 @@ class HTMLTest extends \PHPUnit_Framework_TestCase $textrun = $section->addTextRun('Paragraph'); $textrun->addLink('http://test.com'); - $textrun->addImage($imageSrc); + $textrun->addImage($localImage); $textrun->addFootnote(); $textrun->addEndnote(); @@ -90,10 +94,11 @@ class HTMLTest extends \PHPUnit_Framework_TestCase $cell->addLink('http://test.com'); $cell->addTextBreak(); $cell->addListItem('Test'); - $cell->addImage($imageSrc); + $cell->addImage($localImage); $cell->addObject($objectSrc); $cell->addFootnote(); $cell->addEndnote(); + $cell = $table->addRow()->addCell(); $writer = new HTML($phpWord); $writer->save($file);