From d0b4ed6d415aab3e8730934a04407fbff90e40a2 Mon Sep 17 00:00:00 2001 From: Brandon Skrtich Date: Tue, 4 Mar 2014 10:54:14 -0700 Subject: [PATCH] Ability to use images in a text run --- Classes/PHPWord/Section/TextRun.php | 21 ++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 24 ++++++---- samples/Sample_04_Textrun.php | 61 ++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 samples/Sample_04_Textrun.php diff --git a/Classes/PHPWord/Section/TextRun.php b/Classes/PHPWord/Section/TextRun.php index a9104341..44473e63 100755 --- a/Classes/PHPWord/Section/TextRun.php +++ b/Classes/PHPWord/Section/TextRun.php @@ -109,6 +109,27 @@ class PHPWord_Section_TextRun return $link; } + /** + * Add a Image Element + * + * @param string $imageSrc + * @param mixed $styleFont + * @return PHPWord_Section_Image + */ + public function addImage($imageSrc, $style = null) { + $image = new PHPWord_Section_Image($imageSrc, $style); + + if (!is_null($image->getSource())) { + $rID = PHPWord_Media::addSectionMediaElement($imageSrc, 'image'); + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Source does not exist or unsupported image type.'); + } + } + /** * Get TextRun content * diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 3d464449..a90126d9 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -106,6 +106,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $this->_writeText($objWriter, $element, true); } elseif ($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element, true); + } elseif ($element instanceof PHPWord_Section_Image) { + $this->_writeImage($objWriter, $element, true); } } } @@ -627,7 +629,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart * @param \PHPWord_Shared_XMLWriter $objWriter * @param \PHPWord_Section_Image $image */ - protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image) + protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) { $rId = $image->getRelationId(); @@ -639,14 +641,16 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $marginLeft = $style->getMarginLeft(); $wrappingStyle = $style->getWrappingStyle(); - $objWriter->startElement('w:p'); + if (!$withoutP) { + $objWriter->startElement('w:p'); - if (!is_null($align)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:jc'); - $objWriter->writeAttribute('w:val', $align); - $objWriter->endElement(); - $objWriter->endElement(); + if (!is_null($align)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:jc'); + $objWriter->writeAttribute('w:val', $align); + $objWriter->endElement(); + $objWriter->endElement(); + } } $objWriter->startElement('w:r'); @@ -697,7 +701,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); - $objWriter->endElement(); + if (!$withoutP) { + $objWriter->endElement(); // w:p + } } protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php new file mode 100644 index 00000000..80ad2d3c --- /dev/null +++ b/samples/Sample_04_Textrun.php @@ -0,0 +1,61 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); + + +// Ads styles +$PHPWord->addParagraphStyle('pStyle', array('spacing'=>100)); +$PHPWord->addFontStyle('BoldText', array('bold'=>true)); +$PHPWord->addFontStyle('ColoredText', array('color'=>'FF8080')); +$PHPWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE)); + +// New portrait section +$section = $PHPWord->createSection(); + +// Add text run +$textrun = $section->createTextRun('pStyle'); + +$textrun->addText('Each textrun can contain native text, link elements or an image.'); +$textrun->addText(' No break is placed after adding an element.', 'BoldText'); +$textrun->addText(' All elements are placed inside a paragraph with the optionally given p-Style.', 'ColoredText'); +$textrun->addText(' Sample Link: '); +$textrun->addLink('http://www.google.com', null, 'NLink'); +$textrun->addText(' Sample Image: '); +$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18)); +$textrun->addText(' Here is some more text. '); + +// Save File +echo date('H:i:s') , ' Write to Word2007 format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save(str_replace('.php', '.docx', __FILE__)); + +/* Text Run is not currently supported for ODText +echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save(str_replace('.php', '.odt', __FILE__)); +*/ + +/* Text Run is not currently supported for RTF +echo date('H:i:s') , ' Write to RTF format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save(str_replace('.php', '.rtf', __FILE__)); +*/ + +// Echo memory peak usage +echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; + +// Echo done +echo date('H:i:s') , ' Done writing file' , EOL; \ No newline at end of file