Skip to content

Commit 4ad5cf0

Browse files
committed
Merge pull request #84 from bskrtich/textrunimage
Ability to use images in a text run
2 parents d1e16a6 + d0b4ed6 commit 4ad5cf0

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

Classes/PHPWord/Section/TextRun.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null)
109109
return $link;
110110
}
111111

112+
/**
113+
* Add a Image Element
114+
*
115+
* @param string $imageSrc
116+
* @param mixed $styleFont
117+
* @return PHPWord_Section_Image
118+
*/
119+
public function addImage($imageSrc, $style = null) {
120+
$image = new PHPWord_Section_Image($imageSrc, $style);
121+
122+
if (!is_null($image->getSource())) {
123+
$rID = PHPWord_Media::addSectionMediaElement($imageSrc, 'image');
124+
$image->setRelationId($rID);
125+
126+
$this->_elementCollection[] = $image;
127+
return $image;
128+
} else {
129+
trigger_error('Source does not exist or unsupported image type.');
130+
}
131+
}
132+
112133
/**
113134
* Get TextRun content
114135
*

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHP
106106
$this->_writeText($objWriter, $element, true);
107107
} elseif ($element instanceof PHPWord_Section_Link) {
108108
$this->_writeLink($objWriter, $element, true);
109+
} elseif ($element instanceof PHPWord_Section_Image) {
110+
$this->_writeImage($objWriter, $element, true);
109111
}
110112
}
111113
}
@@ -627,7 +629,7 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
627629
* @param \PHPWord_Shared_XMLWriter $objWriter
628630
* @param \PHPWord_Section_Image $image
629631
*/
630-
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image)
632+
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
631633
{
632634
$rId = $image->getRelationId();
633635

@@ -639,14 +641,16 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
639641
$marginLeft = $style->getMarginLeft();
640642
$wrappingStyle = $style->getWrappingStyle();
641643

642-
$objWriter->startElement('w:p');
644+
if (!$withoutP) {
645+
$objWriter->startElement('w:p');
643646

644-
if (!is_null($align)) {
645-
$objWriter->startElement('w:pPr');
646-
$objWriter->startElement('w:jc');
647-
$objWriter->writeAttribute('w:val', $align);
648-
$objWriter->endElement();
649-
$objWriter->endElement();
647+
if (!is_null($align)) {
648+
$objWriter->startElement('w:pPr');
649+
$objWriter->startElement('w:jc');
650+
$objWriter->writeAttribute('w:val', $align);
651+
$objWriter->endElement();
652+
$objWriter->endElement();
653+
}
650654
}
651655

652656
$objWriter->startElement('w:r');
@@ -697,7 +701,9 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
697701

698702
$objWriter->endElement();
699703

700-
$objWriter->endElement();
704+
if (!$withoutP) {
705+
$objWriter->endElement(); // w:p
706+
}
701707
}
702708

703709
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)

samples/Sample_04_Textrun.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
error_reporting(E_ALL);
4+
5+
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
6+
define('EOL', PHP_EOL);
7+
}
8+
else {
9+
define('EOL', '<br />');
10+
}
11+
12+
require_once '../Classes/PHPWord.php';
13+
14+
// New Word Document
15+
echo date('H:i:s') , ' Create new PHPWord object' , EOL;
16+
$PHPWord = new PHPWord();
17+
18+
19+
// Ads styles
20+
$PHPWord->addParagraphStyle('pStyle', array('spacing'=>100));
21+
$PHPWord->addFontStyle('BoldText', array('bold'=>true));
22+
$PHPWord->addFontStyle('ColoredText', array('color'=>'FF8080'));
23+
$PHPWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));
24+
25+
// New portrait section
26+
$section = $PHPWord->createSection();
27+
28+
// Add text run
29+
$textrun = $section->createTextRun('pStyle');
30+
31+
$textrun->addText('Each textrun can contain native text, link elements or an image.');
32+
$textrun->addText(' No break is placed after adding an element.', 'BoldText');
33+
$textrun->addText(' All elements are placed inside a paragraph with the optionally given p-Style.', 'ColoredText');
34+
$textrun->addText(' Sample Link: ');
35+
$textrun->addLink('http://www.google.com', null, 'NLink');
36+
$textrun->addText(' Sample Image: ');
37+
$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
38+
$textrun->addText(' Here is some more text. ');
39+
40+
// Save File
41+
echo date('H:i:s') , ' Write to Word2007 format' , EOL;
42+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
43+
$objWriter->save(str_replace('.php', '.docx', __FILE__));
44+
45+
/* Text Run is not currently supported for ODText
46+
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
47+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
48+
$objWriter->save(str_replace('.php', '.odt', __FILE__));
49+
*/
50+
51+
/* Text Run is not currently supported for RTF
52+
echo date('H:i:s') , ' Write to RTF format' , EOL;
53+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
54+
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
55+
*/
56+
57+
// Echo memory peak usage
58+
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
59+
60+
// Echo done
61+
echo date('H:i:s') , ' Done writing file' , EOL;

0 commit comments

Comments
 (0)