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\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 .= '</p>' . 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 = '<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) {
$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)
{
$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;
}

View File

@ -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);