Skip to content

Ability to use images in a text run #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Classes/PHPWord/Section/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
24 changes: 15 additions & 9 deletions Classes/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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();

Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions samples/Sample_03_Textrun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

error_reporting(E_ALL);

if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}

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;