From 901afca49a20dcfcd136240c0d81cacd7155844b Mon Sep 17 00:00:00 2001 From: Brandon Skrtich Date: Fri, 21 Feb 2014 16:59:47 -0700 Subject: [PATCH 1/2] Ability to use images in a text run Signed-off-by: Brandon Skrtich --- Classes/PHPWord/Section/TextRun.php | 21 ++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 24 ++++++---- samples/Sample_03_Textrun.php | 61 ++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 samples/Sample_03_Textrun.php diff --git a/Classes/PHPWord/Section/TextRun.php b/Classes/PHPWord/Section/TextRun.php index 2c7a216697..68320f1a6e 100755 --- a/Classes/PHPWord/Section/TextRun.php +++ b/Classes/PHPWord/Section/TextRun.php @@ -109,6 +109,27 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null) 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 331625fccb..3ea2246851 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -106,6 +106,8 @@ protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHP $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 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P * @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 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $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 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $objWriter->endElement(); - $objWriter->endElement(); + if (!$withoutP) { + $objWriter->endElement(); // w:p + } } protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) diff --git a/samples/Sample_03_Textrun.php b/samples/Sample_03_Textrun.php new file mode 100644 index 0000000000..80ad2d3c87 --- /dev/null +++ b/samples/Sample_03_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 From dff876eb978ffea13d7c21db7e45643b3896df4f Mon Sep 17 00:00:00 2001 From: Brandon Skrtich Date: Fri, 21 Feb 2014 17:14:21 -0700 Subject: [PATCH 2/2] Updated requirements on the read me Signed-off-by: Brandon Skrtich --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21c15959ac..58f6acbc33 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PHPWord - OpenXML - Read, Write and Create Word documents in PHP -PHPWord is a library written in PHP that create word documents. +PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software. @@ -10,6 +10,8 @@ Fork us! ## Requirements * PHP version 5.3.0 or higher +* PHP extension php_zip enabled +* PHP extension php_xml enabled ## License PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md)