RTF Writer: Ability to write image

This commit is contained in:
Ivan Lanin 2014-05-23 18:32:56 +07:00
parent e00b551aa2
commit a65c3c3cf1
9 changed files with 124 additions and 64 deletions

View File

@ -29,6 +29,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin - Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin
- Image: Enable "image float left" - @ivanlanin GH-244 - Image: Enable "image float left" - @ivanlanin GH-244
- RTF Writer: Ability to write document properties - @ivanlanin - RTF Writer: Ability to write document properties - @ivanlanin
- RTF Writer: Ability to write image - @ivanlanin
### Bugfixes ### Bugfixes

View File

@ -83,7 +83,7 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Table | ✓ | ✓ | | ✓ | ✓ | | | Table | ✓ | ✓ | | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Image | ✓ | ✓ | | ✓ | | | | Image | ✓ | ✓ | | ✓ | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Object | ✓ | | | | | | | Object | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+ +---------------------------+----------------------+--------+-------+-------+--------+-------+

View File

@ -89,7 +89,7 @@ Below are the supported features for each file formats.
| | Page Break | ✓ | | ✓ | | | | | Page Break | ✓ | | ✓ | | |
| | List | ✓ | | | | | | | List | ✓ | | | | |
| | Table | ✓ | ✓ | | ✓ | ✓ | | | Table | ✓ | ✓ | | ✓ | ✓ |
| | Image | ✓ | ✓ | | ✓ | | | | Image | ✓ | ✓ | | ✓ | |
| | Object | ✓ | | | | | | | Object | ✓ | | | | |
| | Watermark | ✓ | | | | | | | Watermark | ✓ | | | | |
| | Table of Contents | ✓ | | | | | | | Table of Contents | ✓ | | | | |

View File

@ -83,6 +83,7 @@ abstract class AbstractContainer extends AbstractElement
$mediaContainer = $this->getMediaContainer(); $mediaContainer = $this->getMediaContainer();
if (in_array($elementName, array('Link', 'Image', 'Object'))) { if (in_array($elementName, array('Link', 'Image', 'Object'))) {
if ($elementName == 'Image') { if ($elementName == 'Image') {
/** @var \PhpOffice\PhpWord\Element\Image $element Type hint */
$rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1], $element); $rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1], $element);
} else { } else {
$rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1]); $rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1]);

View File

@ -279,6 +279,65 @@ class Image extends AbstractElement
$this->mediaIndex = $value; $this->mediaIndex = $value;
} }
/**
* Get image string data
*
* @param bool $base64
* @return string|null
*/
public function getImageStringData($base64 = false)
{
$source = $this->source;
$actualSource = null;
$imageBinary = null;
$imageData = null;
// Get actual source from archive image or other source
// Return null if not found
if ($this->sourceType == self::SOURCE_ARCHIVE) {
$source = substr($source, 6);
list($zipFilename, $imageFilename) = explode('#', $source);
$zip = new ZipArchive();
if ($zip->open($zipFilename) !== false) {
if ($zip->locateName($imageFilename)) {
$zip->extractTo(sys_get_temp_dir(), $imageFilename);
$actualSource = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $imageFilename;
}
}
$zip->close();
} else {
$actualSource = $source;
}
if ($actualSource === null) {
return null;
}
// Read image binary data and convert to hex
if ($this->sourceType == self::SOURCE_GD) {
$imageResource = call_user_func($this->imageCreateFunc, $actualSource);
ob_start();
call_user_func($this->imageFunc, $imageResource);
$imageBinary = ob_get_contents();
ob_end_clean();
} else {
$fileHandle = fopen($actualSource, 'rb', false);
if ($fileHandle !== false) {
$imageBinary = fread($fileHandle, filesize($actualSource));
fclose($fileHandle);
}
}
if ($imageBinary !== null) {
if ($base64) {
$imageData = chunk_split(base64_encode($imageBinary));
} else {
$imageData = chunk_split(bin2hex($imageBinary));
}
}
return $imageData;
}
/** /**
* Check memory image, supported type, image functions, and proportional width/height * Check memory image, supported type, image functions, and proportional width/height
* *

View File

@ -430,7 +430,7 @@ class Table extends Border
/** /**
* Get cell margin * Get cell margin
* *
* @return integer[] * @return int[]
*/ */
public function getCellMargin() public function getCellMargin()
{ {

View File

@ -18,7 +18,6 @@
namespace PhpOffice\PhpWord\Writer\HTML\Element; namespace PhpOffice\PhpWord\Writer\HTML\Element;
use PhpOffice\PhpWord\Element\Image as ImageElement; use PhpOffice\PhpWord\Element\Image as ImageElement;
use PhpOffice\PhpWord\Shared\ZipArchive;
use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter;
/** /**
@ -43,10 +42,11 @@ class Image extends Text
$content = ''; $content = '';
if (!$parentWriter->isPdf()) { if (!$parentWriter->isPdf()) {
$imageData = $this->getBase64ImageData($this->element); $imageData = $this->element->getImageStringData(true);
if (!is_null($imageData)) { if ($imageData !== null) {
$styleWriter = new ImageStyleWriter($this->element->getStyle()); $styleWriter = new ImageStyleWriter($this->element->getStyle());
$style = $styleWriter->write(); $style = $styleWriter->write();
$imageData = 'data:' . $this->element->getImageType() . ';base64,' . $imageData;
$content .= $this->writeOpening(); $content .= $this->writeOpening();
$content .= "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>"; $content .= "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
@ -56,61 +56,4 @@ class Image extends Text
return $content; return $content;
} }
/**
* Get Base64 image data
*
* @param \PhpOffice\PhpWord\Element\Image $element
* @return string|null
*/
private function getBase64ImageData(ImageElement $element)
{
$source = $element->getSource();
$imageType = $element->getImageType();
$imageData = null;
$imageBinary = null;
$actualSource = null;
// Get actual source from archive image or other source
// Return null if not found
if ($element->getSourceType() == ImageElement::SOURCE_ARCHIVE) {
$source = substr($source, 6);
list($zipFilename, $imageFilename) = explode('#', $source);
$zip = new ZipArchive();
if ($zip->open($zipFilename) !== false) {
if ($zip->locateName($imageFilename)) {
$zip->extractTo($this->parentWriter->getTempDir(), $imageFilename);
$actualSource = $this->parentWriter->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
}
}
$zip->close();
} else {
$actualSource = $source;
}
if ($actualSource === null) {
return null;
}
// Read image binary data and convert into Base64
if ($element->getSourceType() == ImageElement::SOURCE_GD) {
$imageResource = call_user_func($element->getImageCreateFunction(), $actualSource);
ob_start();
call_user_func($element->getImageFunction(), $imageResource);
$imageBinary = ob_get_contents();
ob_end_clean();
} else {
$fileHandle = fopen($actualSource, 'rb', false);
if ($fileHandle !== false) {
$imageBinary = fread($fileHandle, filesize($actualSource));
fclose($fileHandle);
}
}
if ($imageBinary !== null) {
$base64 = chunk_split(base64_encode($imageBinary));
$imageData = 'data:' . $imageType . ';base64,' . $base64;
}
return $imageData;
}
} }

View File

@ -32,7 +32,6 @@ use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
*/ */
class Head extends AbstractPart class Head extends AbstractPart
{ {
/** /**
* Write part * Write part
* *

View File

@ -0,0 +1,57 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\RTF\Element;
use PhpOffice\PhpWord\Element\Image as ImageElement;
use PhpOffice\PhpWord\Shared\Font;
/**
* Image element RTF writer
*
* @since 0.11.0
*/
class Image extends AbstractElement
{
/**
* Write element
*
* @return string
*/
public function write()
{
if (!$this->element instanceof ImageElement) {
return '';
}
$this->getStyles();
$style = $this->element->getStyle();
$content = '';
$content .= $this->writeOpening();
$content .= '{\*\shppict {\pict';
$content .= '\pngblip\picscalex100\picscaley100';
$content .= '\picwgoal' . round(Font::pixelSizeToTwips($style->getWidth()));
$content .= '\pichgoal' . round(Font::pixelSizeToTwips($style->getHeight()));
$content .= PHP_EOL;
$content .= $this->element->getImageStringData();
$content .= '}}';
$content .= $this->writeClosing();
return $content;
}
}