Skip to content

Commit 3d8ae04

Browse files
committed
Set image size and additional tests for HTML writer
1 parent 406534c commit 3d8ae04

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src/PhpWord/Writer/HTML.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use PhpOffice\PhpWord\Style;
2828
use PhpOffice\PhpWord\Style\Font;
2929
use PhpOffice\PhpWord\Style\Paragraph;
30-
use PhpOffice\PhpWord\TOC;
3130

3231
/**
3332
* HTML writer
@@ -170,10 +169,10 @@ private function writeHTMLBody()
170169
$html .= $this->writeImage($element);
171170
} elseif ($element instanceof Object) {
172171
$html .= $this->writeObject($element);
173-
} elseif ($element instanceof Footnote) {
174-
$html .= $this->writeFootnote($element);
175172
} elseif ($element instanceof Endnote) {
176173
$html .= $this->writeEndnote($element);
174+
} elseif ($element instanceof Footnote) {
175+
$html .= $this->writeFootnote($element);
177176
}
178177
}
179178
}
@@ -257,10 +256,10 @@ private function writeTextRun($textrun)
257256
$html .= $this->writeTextBreak($element, true);
258257
} elseif ($element instanceof Image) {
259258
$html .= $this->writeImage($element, true);
260-
} elseif ($element instanceof Footnote) {
261-
$html .= $this->writeFootnote($element);
262259
} elseif ($element instanceof Endnote) {
263260
$html .= $this->writeEndnote($element);
261+
} elseif ($element instanceof Footnote) {
262+
$html .= $this->writeFootnote($element);
264263
}
265264
}
266265
$html .= '</p>' . PHP_EOL;
@@ -402,10 +401,10 @@ private function writeTable($element)
402401
$html .= $this->writeImage($content);
403402
} elseif ($content instanceof Object) {
404403
$html .= $this->writeObject($content);
405-
} elseif ($element instanceof Footnote) {
406-
$html .= $this->writeFootnote($element);
407404
} elseif ($element instanceof Endnote) {
408405
$html .= $this->writeEndnote($element);
406+
} elseif ($element instanceof Footnote) {
407+
$html .= $this->writeFootnote($element);
409408
}
410409
}
411410
} else {
@@ -434,9 +433,13 @@ private function writeImage($element, $withoutP = false)
434433
if (!$this->isPdf) {
435434
$imageData = $this->getBase64ImageData($element);
436435
if (!is_null($imageData)) {
437-
$html = '<img border="0" src="' . $imageData . '"/>';
436+
$style = $this->assembleCss(array(
437+
'width' => $element->getStyle()->getWidth() . 'px',
438+
'height' => $element->getStyle()->getHeight() . 'px',
439+
));
440+
$html = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
438441
if (!$withoutP) {
439-
$html = '<p>' . $html . '</p>' . PHP_EOL;
442+
$html = "<p>{$html}</p>" . PHP_EOL;
440443
}
441444
}
442445
}
@@ -626,11 +629,12 @@ private function assembleCss($css, $curlyBracket = false)
626629
private function getBase64ImageData(Image $element)
627630
{
628631
$imageData = null;
632+
$imageBinary = null;
629633
$source = $element->getSource();
630634
$imageType = $element->getImageType();
631635

632-
// Get actual source
633-
if ($element->getSourceType() == 'archive') {
636+
// Get actual source from archive image
637+
if ($element->getSourceType() == Image::SOURCE_ARCHIVE) {
634638
$source = substr($source, 6);
635639
list($zipFilename, $imageFilename) = explode('#', $source);
636640
$zip = new \ZipArchive();
@@ -646,10 +650,20 @@ private function getBase64ImageData(Image $element)
646650
}
647651

648652
// Read image binary data and convert into Base64
649-
if ($fp = fopen($actualSource, "rb", 0)) {
650-
$image = fread($fp, filesize($actualSource));
651-
fclose($fp);
652-
$base64 = chunk_split(base64_encode($image));
653+
if ($element->getSourceType() == Image::SOURCE_GD) {
654+
$imageResource = call_user_func($element->getImageCreateFunction(), $actualSource);
655+
ob_start();
656+
call_user_func($element->getImageFunction(), $imageResource);
657+
$imageBinary = ob_get_contents();
658+
ob_end_clean();
659+
} else {
660+
if ($fp = fopen($actualSource, 'rb', false)) {
661+
$imageBinary = fread($fp, filesize($actualSource));
662+
fclose($fp);
663+
}
664+
}
665+
if (!is_null($imageBinary)) {
666+
$base64 = chunk_split(base64_encode($imageBinary));
653667
$imageData = 'data:' . $imageType . ';base64,' . $base64;
654668
}
655669

tests/PhpWord/Tests/Writer/HTMLTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public function testConstructWithNull()
4545
*/
4646
public function testSave()
4747
{
48-
$imageSrc = __DIR__ . "/../_files/images/PhpWord.png";
48+
$localImage = __DIR__ . "/../_files/images/PhpWord.png";
49+
$archiveImage = 'zip://' . __DIR__ . '/../_files/documents/reader.docx#word/media/image1.jpeg';
50+
$gdImage = 'http://php.net/images/logos/php-med-trans-light.gif';
4951
$objectSrc = __DIR__ . "/../_files/documents/sheet.xls";
5052
$file = __DIR__ . "/../_files/temp.html";
5153

@@ -64,7 +66,9 @@ public function testSave()
6466
$section->addTitle('Test', 1);
6567
$section->addPageBreak();
6668
$section->addListItem('Test');
67-
$section->addImage($imageSrc);
69+
$section->addImage($localImage);
70+
$section->addImage($archiveImage);
71+
$section->addImage($gdImage);
6872
$section->addObject($objectSrc);
6973
$section->addFootnote();
7074
$section->addEndnote();
@@ -77,7 +81,7 @@ public function testSave()
7781

7882
$textrun = $section->addTextRun('Paragraph');
7983
$textrun->addLink('http://test.com');
80-
$textrun->addImage($imageSrc);
84+
$textrun->addImage($localImage);
8185
$textrun->addFootnote();
8286
$textrun->addEndnote();
8387

@@ -90,10 +94,11 @@ public function testSave()
9094
$cell->addLink('http://test.com');
9195
$cell->addTextBreak();
9296
$cell->addListItem('Test');
93-
$cell->addImage($imageSrc);
97+
$cell->addImage($localImage);
9498
$cell->addObject($objectSrc);
9599
$cell->addFootnote();
96100
$cell->addEndnote();
101+
$cell = $table->addRow()->addCell();
97102

98103
$writer = new HTML($phpWord);
99104
$writer->save($file);

0 commit comments

Comments
 (0)