Merge MemoryImage with Image, but left addMemoryImage for backward compatibility

This commit is contained in:
Ivan Lanin 2014-03-24 16:24:50 +07:00
parent b24550d060
commit eed86f3768
21 changed files with 331 additions and 564 deletions

View File

@ -8,7 +8,8 @@ This release marked the transformation to namespaces (PHP 5.3+).
### Features
None yet.
- Image: Ability to use remote or GD images using `addImage()` on sections, headers, footer, cells, and textruns - @ivanlanin
- Header: Ability to use remote or GD images using `addWatermark()` - @ivanlanin
### Bugfixes
@ -21,7 +22,8 @@ None yet.
- Use `PhpOffice\PhpWord` namespace for PSR compliance - @RomanSyroeshko @gabrielbull GH-159 GH-58
- Restructure folders and change folder name `Classes` to `src` and `Tests` to `test` for PSR compliance - @RomanSyroeshko @gabrielbull
- Compliance to phpDocumentor - @ivanlanin
- Merge Style\TableFull into Style\Table - @ivanlanin GH-160
- Merge Style\TableFull into Style\Table. Style\TableFull is deprecated - @ivanlanin GH-160
- Merge Section\MemoryImage into Section\Image. Section\Image is deprecated - @ivanlanin GH-160
## 0.8.1 - 17 Mar 2014

View File

@ -25,7 +25,7 @@
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Image;
/**
* Media
@ -69,54 +69,33 @@ class Media
*
* @param string $src
* @param string $type
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage
* @param \PhpOffice\PhpWord\Section\Image $image
* @return mixed
*/
public static function addSectionMediaElement($src, $type, MemoryImage $memoryImage = null)
public static function addSectionMediaElement($src, $type, Image $image = null)
{
$mediaId = md5($src);
$key = ($type === 'image') ? 'images' : 'embeddings';
if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
$cImg = self::countSectionMediaElements('images');
$cObj = self::countSectionMediaElements('embeddings');
$rID = self::countSectionMediaElements() + 7;
$media = array();
$folder = null;
$file = null;
if ($type === 'image') {
$cImg++;
//Detect if it's a memory image first by php ext and second by regex
$isMemImage = false;
if (stripos(strrev($src), strrev('.php')) === 0) {
$isMemImage = true;
}
if (!$isMemImage) {
$isMemImage = (filter_var($src, \FILTER_VALIDATE_URL) !== false);
}
$extension = '';
if ($isMemImage) {
$extension = $memoryImage->getImageExtension();
$media['isMemImage'] = true;
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
if (!is_null($image)) {
$isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension();
} else {
$imageType = exif_imagetype($src);
if ($imageType === \IMAGETYPE_JPEG) {
$extension = 'jpg';
} elseif ($imageType === \IMAGETYPE_GIF) {
$extension = 'gif';
} elseif ($imageType === \IMAGETYPE_PNG) {
$extension = 'png';
} elseif ($imageType === \IMAGETYPE_BMP) {
$extension = 'bmp';
} elseif ($imageType === \IMAGETYPE_TIFF_II || $imageType === \IMAGETYPE_TIFF_MM) {
$extension = 'tif';
}
$isMemImage = false;
}
if ($isMemImage) {
$media['isMemImage'] = true;
$media['createfunction'] = $image->getImageCreateFunction();
$media['imagefunction'] = $image->getImageFunction();
}
$folder = 'media';
$file = $type . $cImg . '.' . strtolower($extension);
} elseif ($type === 'oleObject') {
@ -124,26 +103,22 @@ class Media
$folder = 'embedding';
$file = $type . $cObj . '.bin';
}
$media['source'] = $src;
$media['target'] = "$folder/section_$file";
$media['type'] = $type;
$media['rID'] = $rID;
self::$_sectionMedia[$key][$mediaId] = $media;
if ($type === 'oleObject') {
return array($rID, ++self::$_objectId);
}
return $rID;
} else {
if ($type === 'oleObject') {
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
return array($rID, ++self::$_objectId);
}
return self::$_sectionMedia[$key][$mediaId]['rID'];
}
if ($type === 'oleObject') {
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
return array($rID, ++self::$_objectId);
}
return self::$_sectionMedia[$key][$mediaId]['rID'];
}
/**
@ -207,50 +182,42 @@ class Media
*
* @param int $headerCount
* @param string $src
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage
* @param \PhpOffice\PhpWord\Section\Image $image
* @return int
*/
public static function addHeaderMediaElement($headerCount, $src, MemoryImage $memoryImage = null)
public static function addHeaderMediaElement($headerCount, $src, Image $image = null)
{
$mediaId = md5($src);
$key = 'header' . $headerCount;
if (!array_key_exists($key, self::$_headerMedia)) {
self::$_headerMedia[$key] = array();
}
if (!array_key_exists($mediaId, self::$_headerMedia[$key])) {
$cImg = self::countHeaderMediaElements($key);
$rID = $cImg + 1;
$cImg++;
$inf = pathinfo($src);
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
$media = array();
if ($isMemImage) {
$ext = $memoryImage->getImageExtension();
$media['isMemImage'] = true;
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
if (!is_null($image)) {
$isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension();
} else {
$ext = $inf['extension'];
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
$ext = 'jpg';
}
$isMemImage = false;
}
$file = 'image' . $cImg . '.' . strtolower($ext);
if ($isMemImage) {
$media['isMemImage'] = true;
$media['createfunction'] = $image->getImageCreateFunction();
$media['imagefunction'] = $image->getImageFunction();
}
$file = 'image' . $cImg . '.' . strtolower($extension);
$media['source'] = $src;
$media['target'] = 'media/' . $key . '_' . $file;
$media['type'] = 'image';
$media['rID'] = $rID;
self::$_headerMedia[$key][$mediaId] = $media;
return $rID;
} else {
return self::$_headerMedia[$key][$mediaId]['rID'];
}
return self::$_headerMedia[$key][$mediaId]['rID'];
}
/**
@ -279,50 +246,41 @@ class Media
*
* @param int $footerCount
* @param string $src
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage
* @param \PhpOffice\PhpWord\Section\Image $image
* @return int
*/
public static function addFooterMediaElement($footerCount, $src, MemoryImage $memoryImage = null)
public static function addFooterMediaElement($footerCount, $src, Image $image = null)
{
$mediaId = md5($src);
$key = 'footer' . $footerCount;
if (!array_key_exists($key, self::$_footerMedia)) {
self::$_footerMedia[$key] = array();
}
if (!array_key_exists($mediaId, self::$_footerMedia[$key])) {
$cImg = self::countFooterMediaElements($key);
$rID = $cImg + 1;
$cImg++;
$inf = pathinfo($src);
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
$media = array();
if ($isMemImage) {
$ext = $memoryImage->getImageExtension();
$media['isMemImage'] = true;
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
if (!is_null($image)) {
$isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension();
} else {
$ext = $inf['extension'];
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
$ext = 'jpg';
}
$isMemImage = false;
}
$file = 'image' . $cImg . '.' . strtolower($ext);
if ($isMemImage) {
$media['isMemImage'] = true;
$media['createfunction'] = $image->getImageCreateFunction();
$media['imagefunction'] = $image->getImageFunction();
}
$file = 'image' . $cImg . '.' . strtolower($extension);
$media['source'] = $src;
$media['target'] = 'media/' . $key . '_' . $file;
$media['type'] = 'image';
$media['rID'] = $rID;
self::$_footerMedia[$key][$mediaId] = $media;
return $rID;
} else {
return self::$_footerMedia[$key][$mediaId]['rID'];
}
return self::$_footerMedia[$key][$mediaId]['rID'];
}
/**

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Header;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Settings;
@ -250,7 +249,7 @@ class Section
$iconSrc .= '_' . $ext . '.png';
}
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image');
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc));
$data = Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0];
$objectId = $data[1];
@ -276,15 +275,14 @@ class Section
public function addImage($src, $style = null)
{
$image = new Image($src, $style);
if (!is_null($image->getSource())) {
$rID = Media::addSectionMediaElement($src, 'image');
$rID = Media::addSectionMediaElement($src, 'image', $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {
throw new Exception('Source does not exist or unsupported image type.');
}
throw new Exception('Source does not exist or unsupported image type.');
}
/**
@ -292,20 +290,11 @@ class Section
*
* @param string $link
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\MemoryImage
* @throws \PhpOffice\PhpWord\Exceptions\Exception
* @deprecated
*/
public function addMemoryImage($link, $style = null)
public function addMemoryImage($src, $style = null)
{
$memoryImage = new MemoryImage($link, $style);
if (!is_null($memoryImage->getSource())) {
$rID = Media::addSectionMediaElement($link, 'image', $memoryImage);
$memoryImage->setRelationId($rID);
$this->_elementCollection[] = $memoryImage;
return $memoryImage;
}
throw new Exception('Unsupported image type.');
return $this->addImage($src, $style);
}
/**

View File

@ -134,15 +134,13 @@ class Footer
public function addImage($src, $style = null)
{
$image = new Image($src, $style);
if (!is_null($image->getSource())) {
$rID = Media::addFooterMediaElement($this->_footerCount, $src);
$rID = Media::addFooterMediaElement($this->_footerCount, $src, $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {
throw new Exception('Src does not exist or invalid image type.');
throw new Exception('Source does not exist or unsupported image type.');
}
}
@ -151,20 +149,11 @@ class Footer
*
* @param string $link
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\MemoryImage
* @deprecated
*/
public function addMemoryImage($link, $style = null)
public function addMemoryImage($src, $style = null)
{
$memoryImage = new MemoryImage($link, $style);
if (!is_null($memoryImage->getSource())) {
$rID = Media::addFooterMediaElement($this->_footerCount, $link, $memoryImage);
$memoryImage->setRelationId($rID);
$this->_elementCollection[] = $memoryImage;
return $memoryImage;
} else {
throw new Exception('Unsupported image type.');
}
return $this->addImage($src, $style);
}
/**

View File

@ -163,15 +163,13 @@ class Header
public function addImage($src, $style = null)
{
$image = new Image($src, $style);
if (!is_null($image->getSource())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $src);
$rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {
throw new Exception('Src does not exist or invalid image type.');
throw new Exception('Source does not exist or unsupported image type.');
}
}
@ -180,20 +178,11 @@ class Header
*
* @param string $link
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\MemoryImage
* @deprecated
*/
public function addMemoryImage($link, $style = null)
public function addMemoryImage($src, $style = null)
{
$memoryImage = new MemoryImage($link, $style);
if (!is_null($memoryImage->getSource())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $link, $memoryImage);
$memoryImage->setRelationId($rID);
$this->_elementCollection[] = $memoryImage;
return $memoryImage;
} else {
throw new Exception('Unsupported image type.');
}
return $this->addImage($src, $style);
}
/**
@ -224,11 +213,9 @@ class Header
public function addWatermark($src, $style = null)
{
$image = new Image($src, $style, true);
if (!is_null($image->getSource())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $src);
$rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {

View File

@ -34,77 +34,131 @@ use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
class Image
{
/**
* Image Src
* Image source
*
* @var string
*/
private $_src;
private $source;
/**
* Image Style
* Image style
*
* @var \PhpOffice\PhpWord\Style\Image
*/
private $_style;
private $style;
/**
* Image Relation ID
* Image relation ID specific only for DOCX
*
* @var string
*/
private $_rId;
private $rId;
/**
* Is Watermark
* Is watermark
*
* @var bool
*/
private $_isWatermark;
private $isWatermark;
/**
* Create a new Image
* Image type
*
* @param string $src
* @var string
*/
private $imageType;
/**
* Image create function
*
* @var string
*/
private $imageCreateFunc;
/**
* Image function
*
* @var string
*/
private $imageFunc;
/**
* Image extension
*
* @var string
*/
private $imageExtension;
/**
* Is memory image
*
* @var string
*/
private $isMemImage;
/**
* Create new image element
*
* @param string $source
* @param mixed $style
* @param bool $isWatermark
* @throws \PhpOffice\PhpWord\Exceptions\InvalidImageException
* @throws \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
*/
public function __construct($src, $style = null, $isWatermark = false)
public function __construct($source, $style = null, $isWatermark = false)
{
$supportedImageTypes = array(\IMAGETYPE_JPEG, \IMAGETYPE_GIF, \IMAGETYPE_PNG, \IMAGETYPE_BMP, \IMAGETYPE_TIFF_II, \IMAGETYPE_TIFF_MM);
if (!\file_exists($src)) {
throw new InvalidImageException;
// Detect if it's a memory image, by .php ext or by URL
if (stripos(strrev($source), strrev('.php')) === 0) {
$this->isMemImage = true;
} else {
$this->isMemImage = (filter_var($source, \FILTER_VALIDATE_URL) !== false);
}
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
throw new UnsupportedImageTypeException;
// Check supported types
if ($this->isMemImage) {
$supportedTypes = array('image/jpeg', 'image/gif', 'image/png');
$imgData = getimagesize($source);
$this->imageType = $imgData['mime']; // string
if (!in_array($this->imageType, $supportedTypes)) {
throw new UnsupportedImageTypeException;
}
} else {
$supportedTypes = array(
\IMAGETYPE_JPEG, \IMAGETYPE_GIF,
\IMAGETYPE_PNG, \IMAGETYPE_BMP,
\IMAGETYPE_TIFF_II, \IMAGETYPE_TIFF_MM
);
if (!\file_exists($source)) {
throw new InvalidImageException;
}
$imgData = getimagesize($source);
$this->imageType = exif_imagetype($source);
if (!in_array($this->imageType, $supportedTypes)) {
throw new UnsupportedImageTypeException;
}
$this->imageType = \image_type_to_mime_type($this->imageType);
}
$this->_src = $src;
$this->_isWatermark = $isWatermark;
$this->_style = new \PhpOffice\PhpWord\Style\Image();
// Set private properties
$this->source = $source;
$this->isWatermark = $isWatermark;
$this->style = new \PhpOffice\PhpWord\Style\Image();
if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
$this->style->setStyleValue($key, $value);
}
}
if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']);
$this->style->setWrappingStyle($style['wrappingStyle']);
}
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$imgData = getimagesize($this->_src);
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
if ($this->style->getWidth() == null && $this->style->getHeight() == null) {
$this->style->setWidth($imgData[0]);
$this->style->setHeight($imgData[1]);
}
$this->setImageFunctions();
}
/**
@ -114,66 +168,149 @@ class Image
*/
public function getStyle()
{
return $this->_style;
return $this->style;
}
/**
* Get Image Relation ID
* Get image relation ID
*
* @return int
*/
public function getRelationId()
{
return $this->_rId;
return $this->rId;
}
/**
* Set Image Relation ID
* Set image relation ID
*
* @param int $rId
*/
public function setRelationId($rId)
{
$this->_rId = $rId;
$this->rId = $rId;
}
/**
* Get Image Source
* Get image source
*
* @return string
*/
public function getSource()
{
return $this->_src;
return $this->source;
}
/**
* Get Image Media ID
* Get image media ID
*
* @return string
*/
public function getMediaId()
{
return md5($this->_src);
return md5($this->source);
}
/**
* Get IsWatermark
* Get is watermark
*
* @return int
*/
public function getIsWatermark()
{
return $this->_isWatermark;
return $this->isWatermark;
}
/**
* Set IsWatermark
* Set is watermark
*
* @param bool $pValue
*/
public function setIsWatermark($pValue)
{
$this->_isWatermark = $pValue;
$this->isWatermark = $pValue;
}
/**
* Get image type
*
* @return string
*/
public function getImageType()
{
return $this->imageType;
}
/**
* Get image create function
*
* @return string
*/
public function getImageCreateFunction()
{
return $this->imageCreateFunc;
}
/**
* Get image function
*
* @return string
*/
public function getImageFunction()
{
return $this->imageFunc;
}
/**
* Get image extension
*
* @return string
*/
public function getImageExtension()
{
return $this->imageExtension;
}
/**
* Get is memory image
*
* @return boolean
*/
public function getIsMemImage()
{
return $this->isMemImage;
}
/**
* Set image functions
*/
private function setImageFunctions()
{
switch ($this->imageType) {
case 'image/png':
$this->imageCreateFunc = 'imagecreatefrompng';
$this->imageFunc = 'imagepng';
$this->imageExtension = 'png';
break;
case 'image/gif':
$this->imageCreateFunc = 'imagecreatefromgif';
$this->imageFunc = 'imagegif';
$this->imageExtension = 'gif';
break;
case 'image/jpeg':
case 'image/jpg':
$this->imageCreateFunc = 'imagecreatefromjpeg';
$this->imageFunc = 'imagejpeg';
$this->imageExtension = 'jpg';
break;
case 'image/x-ms-bmp':
case 'image/bmp':
$this->imageType = 'image/bmp';
$this->imageExtension = 'bmp';
break;
case 'image/tiff':
$this->imageExtension = 'tif';
break;
}
}
}

View File

@ -1,232 +0,0 @@
<?php
/**
* PhpWord
*
* Copyright (c) 2014 PhpWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @copyright Copyright (c) 2014 PhpWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 0.8.0
*/
namespace PhpOffice\PhpWord\Section;
/**
* Memory image element
*/
class MemoryImage
{
/**
* Image Src
*
* @var string
*/
private $_src;
/**
* Image Style
*
* @var \PhpOffice\PhpWord\Style\Image
*/
private $_style;
/**
* Image Relation ID
*
* @var string
*/
private $_rId;
/**
* Image Type
*
* @var string
*/
private $_imageType;
/**
* Image Create function
*
* @var string
*/
private $_imageCreateFunc;
/**
* Image function
*
* @var string
*/
private $_imageFunc;
/**
* Image function
*
* @var string
*/
private $_imageExtension;
/**
* Create a new Image
*
* @param string $src
* @param mixed $style
*/
public function __construct($src, $style = null)
{
$imgData = getimagesize($src);
$this->_imageType = $imgData['mime'];
$_supportedImageTypes = array('image/jpeg', 'image/gif', 'image/png');
if (in_array($this->_imageType, $_supportedImageTypes)) {
$this->_src = $src;
$this->_style = new \PhpOffice\PhpWord\Style\Image();
if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
}
}
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
}
$this->_setFunctions();
}
}
/**
* Set Functions
*/
private function _setFunctions()
{
switch ($this->_imageType) {
case 'image/png':
$this->_imageCreateFunc = 'imagecreatefrompng';
$this->_imageFunc = 'imagepng';
$this->_imageExtension = 'png';
break;
case 'image/gif':
$this->_imageCreateFunc = 'imagecreatefromgif';
$this->_imageFunc = 'imagegif';
$this->_imageExtension = 'gif';
break;
case 'image/jpeg':
case 'image/jpg':
$this->_imageCreateFunc = 'imagecreatefromjpeg';
$this->_imageFunc = 'imagejpeg';
$this->_imageExtension = 'jpg';
break;
}
}
/**
* Get Image style
*
* @return \PhpOffice\PhpWord\Style\Image
*/
public function getStyle()
{
return $this->_style;
}
/**
* Get Image Relation ID
*
* @return int
*/
public function getRelationId()
{
return $this->_rId;
}
/**
* Set Image Relation ID
*
* @param int $rId
*/
public function setRelationId($rId)
{
$this->_rId = $rId;
}
/**
* Get Image Source
*
* @return string
*/
public function getSource()
{
return $this->_src;
}
/**
* Get Image Media ID
*
* @return string
*/
public function getMediaId()
{
return md5($this->_src);
}
/**
* Get Image Type
*
* @return string
*/
public function getImageType()
{
return $this->_imageType;
}
/**
* Get Image Create Function
*
* @return string
*/
public function getImageCreateFunction()
{
return $this->_imageCreateFunc;
}
/**
* Get Image Function
*
* @return string
*/
public function getImageFunction()
{
return $this->_imageFunc;
}
/**
* Get Image Extension
*
* @return string
*/
public function getImageExtension()
{
return $this->_imageExtension;
}
}

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\Section\Footer\PreserveText;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak;
@ -201,17 +200,15 @@ class Cell
public function addImage($src, $style = null)
{
$image = new Image($src, $style);
if (!is_null($image->getSource())) {
if ($this->_insideOf == 'section') {
$rID = Media::addSectionMediaElement($src, 'image');
$rID = Media::addSectionMediaElement($src, 'image', $image);
} elseif ($this->_insideOf == 'header') {
$rID = Media::addHeaderMediaElement($this->_pCount, $src);
$rID = Media::addHeaderMediaElement($this->_pCount, $src, $image);
} elseif ($this->_insideOf == 'footer') {
$rID = Media::addFooterMediaElement($this->_pCount, $src);
$rID = Media::addFooterMediaElement($this->_pCount, $src, $image);
}
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {
@ -224,26 +221,11 @@ class Cell
*
* @param string $link
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\MemoryImage
* @deprecated
*/
public function addMemoryImage($link, $style = null)
public function addMemoryImage($src, $style = null)
{
$memoryImage = new MemoryImage($link, $style);
if (!is_null($memoryImage->getSource())) {
if ($this->_insideOf == 'section') {
$rID = Media::addSectionMediaElement($link, 'image', $memoryImage);
} elseif ($this->_insideOf == 'header') {
$rID = Media::addHeaderMediaElement($this->_pCount, $link, $memoryImage);
} elseif ($this->_insideOf == 'footer') {
$rID = Media::addFooterMediaElement($this->_pCount, $link, $memoryImage);
}
$memoryImage->setRelationId($rID);
$this->_elementCollection[] = $memoryImage;
return $memoryImage;
} else {
throw new Exception('Unsupported image type.');
}
return $this->addImage($src, $style);
}
/**
@ -271,7 +253,7 @@ class Cell
$iconSrc .= '_' . $ext . '.png';
}
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image');
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc));
$data = Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0];
$objectId = $data[1];

View File

@ -125,11 +125,9 @@ class TextRun
public function addImage($imageSrc, $style = null)
{
$image = new Image($imageSrc, $style);
if (!is_null($image->getSource())) {
$rID = Media::addSectionMediaElement($imageSrc, 'image');
$rID = Media::addSectionMediaElement($imageSrc, 'image', $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
} else {

View File

@ -30,7 +30,6 @@ use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Table;
@ -281,8 +280,7 @@ class Content extends WriterPart
$this->writeUnsupportedElement($xmlWriter, 'Table');
} elseif ($element instanceof ListItem) {
$this->writeUnsupportedElement($xmlWriter, 'List Item');
} elseif ($element instanceof Image ||
$element instanceof MemoryImage) {
} elseif ($element instanceof Image) {
$this->writeUnsupportedElement($xmlWriter, 'Image');
} elseif ($element instanceof Object) {
$this->writeUnsupportedElement($xmlWriter, 'Object');

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\HashTable;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Table;
@ -379,8 +378,7 @@ class RTF implements IWriter
$sRTFBody .= $this->getDataContentUnsupportedElement('Table');
} elseif ($element instanceof ListItem) {
$sRTFBody .= $this->getDataContentUnsupportedElement('List Item');
} elseif ($element instanceof Image ||
$element instanceof MemoryImage) {
} elseif ($element instanceof Image) {
$sRTFBody .= $this->getDataContentUnsupportedElement('Image');
} elseif ($element instanceof Object) {
$sRTFBody .= $this->getDataContentUnsupportedElement('Object');

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\Section\Footnote;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text;
@ -652,9 +651,7 @@ class Base extends WriterPart
$this->_writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof ListItem) {
$this->_writeListItem($xmlWriter, $element);
} elseif ($element instanceof Image ||
$element instanceof MemoryImage
) {
} elseif ($element instanceof Image) {
$this->_writeImage($xmlWriter, $element);
} elseif ($element instanceof Object) {
$this->_writeObject($xmlWriter, $element);

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\Section\Footnote;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Table;
@ -108,9 +107,7 @@ class Document extends Base
$this->_writeTable($xmlWriter, $element);
} elseif ($element instanceof ListItem) {
$this->_writeListItem($xmlWriter, $element);
} elseif ($element instanceof Image ||
$element instanceof MemoryImage
) {
} elseif ($element instanceof Image) {
$this->_writeImage($xmlWriter, $element);
} elseif ($element instanceof Object) {
$this->_writeObject($xmlWriter, $element);

View File

@ -27,7 +27,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Section\Footer\PreserveText;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak;
@ -79,9 +78,7 @@ class Footer extends Base
$this->_writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof Table) {
$this->_writeTable($xmlWriter, $element);
} elseif ($element instanceof Image ||
$element instanceof MemoryImage
) {
} elseif ($element instanceof Image) {
$this->_writeImage($xmlWriter, $element);
} elseif ($element instanceof PreserveText) {
$this->_writePreserveText($xmlWriter, $element);

View File

@ -27,7 +27,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Section\Footer\PreserveText;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak;
@ -79,9 +78,7 @@ class Header extends Base
$this->_writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof Table) {
$this->_writeTable($xmlWriter, $element);
} elseif ($element instanceof Image ||
$element instanceof MemoryImage
) {
} elseif ($element instanceof Image) {
if (!$element->getIsWatermark()) {
$this->_writeImage($xmlWriter, $element);
} else {

View File

@ -79,15 +79,15 @@ class FooterTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddMemoryImage()
public function testAddImageByUrl()
{
$oFooter = new Footer(1);
$element = $oFooter->addMemoryImage(
$element = $oFooter->addImage(
'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
);
$this->assertCount(1, $oFooter->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $element);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddPreserveText()

View File

@ -76,15 +76,15 @@ class HeaderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddMemoryImage()
public function testAddImageByUrl()
{
$oHeader = new Header(1);
$element = $oHeader->addMemoryImage(
$element = $oHeader->addImage(
'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
);
$this->assertCount(1, $oHeader->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $element);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddPreserveText()

View File

@ -87,4 +87,56 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$oImage->setIsWatermark(true);
$this->assertEquals($oImage->getIsWatermark(), true);
}
public function testPNG()
{
$src = __DIR__ . "/../_files/images/firefox.png";
$oImage = new Image($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage);
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefrompng');
$this->assertEquals($oImage->getImageFunction(), 'imagepng');
$this->assertEquals($oImage->getImageExtension(), 'png');
$this->assertEquals($oImage->getImageType(), 'image/png');
}
public function testGIF()
{
$src = __DIR__ . "/../_files/images/mario.gif";
$oImage = new Image($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage);
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefromgif');
$this->assertEquals($oImage->getImageFunction(), 'imagegif');
$this->assertEquals($oImage->getImageExtension(), 'gif');
$this->assertEquals($oImage->getImageType(), 'image/gif');
}
public function testJPG()
{
$src = __DIR__ . "/../_files/images/earth.jpg";
$oImage = new Image($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage);
$this->assertEquals($oImage->getSource(), $src);
$this->assertEquals($oImage->getMediaId(), md5($src));
$this->assertEquals($oImage->getImageCreateFunction(), 'imagecreatefromjpeg');
$this->assertEquals($oImage->getImageFunction(), 'imagejpeg');
$this->assertEquals($oImage->getImageExtension(), 'jpg');
$this->assertEquals($oImage->getImageType(), 'image/jpeg');
}
public function testBMP()
{
$oImage = new Image(__DIR__ . "/../_files/images/duke_nukem.bmp");
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $oImage);
$this->assertEquals($oImage->getImageCreateFunction(), null);
$this->assertEquals($oImage->getImageFunction(), null);
$this->assertEquals($oImage->getImageExtension(), 'bmp');
$this->assertEquals($oImage->getImageType(), 'image/bmp');
}
}

View File

@ -1,78 +0,0 @@
<?php
namespace PhpOffice\PhpWord\Tests\Section;
use PhpOffice\PhpWord\Section\MemoryImage;
class MemoryImageTest extends \PHPUnit_Framework_TestCase
{
public function testPNG()
{
$src = __DIR__ . "/../_files/images/firefox.png";
$oMemoryImage = new MemoryImage($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $oMemoryImage);
$this->assertEquals($oMemoryImage->getSource(), $src);
$this->assertEquals($oMemoryImage->getMediaId(), md5($src));
$this->assertEquals($oMemoryImage->getImageCreateFunction(), 'imagecreatefrompng');
$this->assertEquals($oMemoryImage->getImageFunction(), 'imagepng');
$this->assertEquals($oMemoryImage->getImageExtension(), 'png');
$this->assertEquals($oMemoryImage->getImageType(), 'image/png');
}
public function testGIF()
{
$src = __DIR__ . "/../_files/images/mario.gif";
$oMemoryImage = new MemoryImage($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $oMemoryImage);
$this->assertEquals($oMemoryImage->getSource(), $src);
$this->assertEquals($oMemoryImage->getMediaId(), md5($src));
$this->assertEquals($oMemoryImage->getImageCreateFunction(), 'imagecreatefromgif');
$this->assertEquals($oMemoryImage->getImageFunction(), 'imagegif');
$this->assertEquals($oMemoryImage->getImageExtension(), 'gif');
$this->assertEquals($oMemoryImage->getImageType(), 'image/gif');
}
public function testJPG()
{
$src = __DIR__ . "/../_files/images/earth.jpg";
$oMemoryImage = new MemoryImage($src);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $oMemoryImage);
$this->assertEquals($oMemoryImage->getSource(), $src);
$this->assertEquals($oMemoryImage->getMediaId(), md5($src));
$this->assertEquals($oMemoryImage->getImageCreateFunction(), 'imagecreatefromjpeg');
$this->assertEquals($oMemoryImage->getImageFunction(), 'imagejpeg');
$this->assertEquals($oMemoryImage->getImageExtension(), 'jpg');
$this->assertEquals($oMemoryImage->getImageType(), 'image/jpeg');
}
public function testBMP()
{
$oMemoryImage = new MemoryImage(__DIR__ . "/../_files/images/duke_nukem.bmp");
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $oMemoryImage);
$this->assertEquals($oMemoryImage->getImageCreateFunction(), null);
$this->assertEquals($oMemoryImage->getImageFunction(), null);
$this->assertEquals($oMemoryImage->getImageExtension(), null);
$this->assertEquals($oMemoryImage->getImageType(), 'image/x-ms-bmp');
}
public function testStyle()
{
$oMemoryImage = new MemoryImage(
__DIR__ . "/../_files/images/earth.jpg",
array('width' => 210, 'height' => 210, 'align' => 'center')
);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oMemoryImage->getStyle());
}
public function testRelationID()
{
$oMemoryImage = new MemoryImage(__DIR__ . "/../_files/images/earth.jpg");
$iVal = rand(1, 1000);
$oMemoryImage->setRelationId($iVal);
$this->assertEquals($oMemoryImage->getRelationId(), $iVal);
}
}

View File

@ -117,37 +117,37 @@ class CellTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddMemoryImageSection()
public function testAddSectionImageByUrl()
{
$oCell = new Cell('section', 1);
$element = $oCell->addMemoryImage(
$element = $oCell->addImage(
'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
);
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $element);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddMemoryImageHeader()
public function testAddHeaderImageByUrl()
{
$oCell = new Cell('header', 1);
$element = $oCell->addMemoryImage(
$element = $oCell->addImage(
'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
);
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $element);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddMemoryImageFooter()
public function testAddFooterImageByUrl()
{
$oCell = new Cell('footer', 1);
$element = $oCell->addMemoryImage(
$element = $oCell->addImage(
'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
);
$this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $element);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
}
public function testAddObjectXLS()

View File

@ -65,7 +65,6 @@ class SectionTest extends \PHPUnit_Framework_TestCase
* @covers ::addListItem
* @covers ::addObject
* @covers ::addImage
* @covers ::addMemoryImage
* @covers ::addTOC
* @covers ::addTitle
* @covers ::createTextRun
@ -86,7 +85,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
$section->addListItem(utf8_decode('ä'));
$section->addObject($objectSource);
$section->addImage($imageSource);
$section->addMemoryImage($imageUrl);
$section->addImage($imageUrl);
$section->addTOC();
$section->addTitle(utf8_decode('ä'), 1);
$section->createTextRun();
@ -96,7 +95,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase
$elementType = 'Link';
$this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[1]);
// $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak',
// 'Table', 'ListItem', 'Object', 'Image', 'MemoryImage', 'TOC',
// 'Table', 'ListItem', 'Object', 'Image', 'Image', 'TOC',
// 'Title', 'TextRun');
// $i = 0;
// foreach ($elementTypes as $elementType) {