Set image size and additional tests for HTML writer

This commit is contained in:
Ivan Lanin 2014-04-16 14:45:43 +07:00
parent 406534cd42
commit 3d8ae044b7
2 changed files with 38 additions and 19 deletions

View File

@ -27,7 +27,6 @@ use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\TOC;
/** /**
* HTML writer * HTML writer
@ -170,10 +169,10 @@ class HTML extends AbstractWriter implements WriterInterface
$html .= $this->writeImage($element); $html .= $this->writeImage($element);
} elseif ($element instanceof Object) { } elseif ($element instanceof Object) {
$html .= $this->writeObject($element); $html .= $this->writeObject($element);
} elseif ($element instanceof Footnote) {
$html .= $this->writeFootnote($element);
} elseif ($element instanceof Endnote) { } elseif ($element instanceof Endnote) {
$html .= $this->writeEndnote($element); $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); $html .= $this->writeTextBreak($element, true);
} elseif ($element instanceof Image) { } elseif ($element instanceof Image) {
$html .= $this->writeImage($element, true); $html .= $this->writeImage($element, true);
} elseif ($element instanceof Footnote) {
$html .= $this->writeFootnote($element);
} elseif ($element instanceof Endnote) { } elseif ($element instanceof Endnote) {
$html .= $this->writeEndnote($element); $html .= $this->writeEndnote($element);
} elseif ($element instanceof Footnote) {
$html .= $this->writeFootnote($element);
} }
} }
$html .= '</p>' . PHP_EOL; $html .= '</p>' . PHP_EOL;
@ -402,10 +401,10 @@ class HTML extends AbstractWriter implements WriterInterface
$html .= $this->writeImage($content); $html .= $this->writeImage($content);
} elseif ($content instanceof Object) { } elseif ($content instanceof Object) {
$html .= $this->writeObject($content); $html .= $this->writeObject($content);
} elseif ($element instanceof Footnote) {
$html .= $this->writeFootnote($element);
} elseif ($element instanceof Endnote) { } elseif ($element instanceof Endnote) {
$html .= $this->writeEndnote($element); $html .= $this->writeEndnote($element);
} elseif ($element instanceof Footnote) {
$html .= $this->writeFootnote($element);
} }
} }
} else { } else {
@ -434,9 +433,13 @@ class HTML extends AbstractWriter implements WriterInterface
if (!$this->isPdf) { if (!$this->isPdf) {
$imageData = $this->getBase64ImageData($element); $imageData = $this->getBase64ImageData($element);
if (!is_null($imageData)) { if (!is_null($imageData)) {
$html = '<img border="0" src="' . $imageData . '"/>'; $style = $this->assembleCss(array(
'width' => $element->getStyle()->getWidth() . 'px',
'height' => $element->getStyle()->getHeight() . 'px',
));
$html = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
if (!$withoutP) { if (!$withoutP) {
$html = '<p>' . $html . '</p>' . PHP_EOL; $html = "<p>{$html}</p>" . PHP_EOL;
} }
} }
} }
@ -626,11 +629,12 @@ class HTML extends AbstractWriter implements WriterInterface
private function getBase64ImageData(Image $element) private function getBase64ImageData(Image $element)
{ {
$imageData = null; $imageData = null;
$imageBinary = null;
$source = $element->getSource(); $source = $element->getSource();
$imageType = $element->getImageType(); $imageType = $element->getImageType();
// Get actual source // Get actual source from archive image
if ($element->getSourceType() == 'archive') { if ($element->getSourceType() == Image::SOURCE_ARCHIVE) {
$source = substr($source, 6); $source = substr($source, 6);
list($zipFilename, $imageFilename) = explode('#', $source); list($zipFilename, $imageFilename) = explode('#', $source);
$zip = new \ZipArchive(); $zip = new \ZipArchive();
@ -646,10 +650,20 @@ class HTML extends AbstractWriter implements WriterInterface
} }
// Read image binary data and convert into Base64 // Read image binary data and convert into Base64
if ($fp = fopen($actualSource, "rb", 0)) { if ($element->getSourceType() == Image::SOURCE_GD) {
$image = fread($fp, filesize($actualSource)); $imageResource = call_user_func($element->getImageCreateFunction(), $actualSource);
fclose($fp); ob_start();
$base64 = chunk_split(base64_encode($image)); 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; $imageData = 'data:' . $imageType . ';base64,' . $base64;
} }

View File

@ -45,7 +45,9 @@ class HTMLTest extends \PHPUnit_Framework_TestCase
*/ */
public function testSave() 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"; $objectSrc = __DIR__ . "/../_files/documents/sheet.xls";
$file = __DIR__ . "/../_files/temp.html"; $file = __DIR__ . "/../_files/temp.html";
@ -64,7 +66,9 @@ class HTMLTest extends \PHPUnit_Framework_TestCase
$section->addTitle('Test', 1); $section->addTitle('Test', 1);
$section->addPageBreak(); $section->addPageBreak();
$section->addListItem('Test'); $section->addListItem('Test');
$section->addImage($imageSrc); $section->addImage($localImage);
$section->addImage($archiveImage);
$section->addImage($gdImage);
$section->addObject($objectSrc); $section->addObject($objectSrc);
$section->addFootnote(); $section->addFootnote();
$section->addEndnote(); $section->addEndnote();
@ -77,7 +81,7 @@ class HTMLTest extends \PHPUnit_Framework_TestCase
$textrun = $section->addTextRun('Paragraph'); $textrun = $section->addTextRun('Paragraph');
$textrun->addLink('http://test.com'); $textrun->addLink('http://test.com');
$textrun->addImage($imageSrc); $textrun->addImage($localImage);
$textrun->addFootnote(); $textrun->addFootnote();
$textrun->addEndnote(); $textrun->addEndnote();
@ -90,10 +94,11 @@ class HTMLTest extends \PHPUnit_Framework_TestCase
$cell->addLink('http://test.com'); $cell->addLink('http://test.com');
$cell->addTextBreak(); $cell->addTextBreak();
$cell->addListItem('Test'); $cell->addListItem('Test');
$cell->addImage($imageSrc); $cell->addImage($localImage);
$cell->addObject($objectSrc); $cell->addObject($objectSrc);
$cell->addFootnote(); $cell->addFootnote();
$cell->addEndnote(); $cell->addEndnote();
$cell = $table->addRow()->addCell();
$writer = new HTML($phpWord); $writer = new HTML($phpWord);
$writer->save($file); $writer->save($file);