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 ### 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 ### Bugfixes
@ -21,7 +22,8 @@ None yet.
- Use `PhpOffice\PhpWord` namespace for PSR compliance - @RomanSyroeshko @gabrielbull GH-159 GH-58 - 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 - Restructure folders and change folder name `Classes` to `src` and `Tests` to `test` for PSR compliance - @RomanSyroeshko @gabrielbull
- Compliance to phpDocumentor - @ivanlanin - 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 ## 0.8.1 - 17 Mar 2014

View File

@ -25,7 +25,7 @@
namespace PhpOffice\PhpWord; namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Section\MemoryImage; use PhpOffice\PhpWord\Section\Image;
/** /**
* Media * Media
@ -69,54 +69,33 @@ class Media
* *
* @param string $src * @param string $src
* @param string $type * @param string $type
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage * @param \PhpOffice\PhpWord\Section\Image $image
* @return mixed * @return mixed
*/ */
public static function addSectionMediaElement($src, $type, MemoryImage $memoryImage = null) public static function addSectionMediaElement($src, $type, Image $image = null)
{ {
$mediaId = md5($src); $mediaId = md5($src);
$key = ($type === 'image') ? 'images' : 'embeddings'; $key = ($type === 'image') ? 'images' : 'embeddings';
if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) { if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
$cImg = self::countSectionMediaElements('images'); $cImg = self::countSectionMediaElements('images');
$cObj = self::countSectionMediaElements('embeddings'); $cObj = self::countSectionMediaElements('embeddings');
$rID = self::countSectionMediaElements() + 7; $rID = self::countSectionMediaElements() + 7;
$media = array(); $media = array();
$folder = null; $folder = null;
$file = null; $file = null;
if ($type === 'image') { if ($type === 'image') {
$cImg++; $cImg++;
//Detect if it's a memory image first by php ext and second by regex if (!is_null($image)) {
$isMemImage = false; $isMemImage = $image->getIsMemImage();
if (stripos(strrev($src), strrev('.php')) === 0) { $extension = $image->getImageExtension();
$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();
} else { } else {
$imageType = exif_imagetype($src); $isMemImage = false;
if ($imageType === \IMAGETYPE_JPEG) { }
$extension = 'jpg'; if ($isMemImage) {
} elseif ($imageType === \IMAGETYPE_GIF) { $media['isMemImage'] = true;
$extension = 'gif'; $media['createfunction'] = $image->getImageCreateFunction();
} elseif ($imageType === \IMAGETYPE_PNG) { $media['imagefunction'] = $image->getImageFunction();
$extension = 'png';
} elseif ($imageType === \IMAGETYPE_BMP) {
$extension = 'bmp';
} elseif ($imageType === \IMAGETYPE_TIFF_II || $imageType === \IMAGETYPE_TIFF_MM) {
$extension = 'tif';
}
} }
$folder = 'media'; $folder = 'media';
$file = $type . $cImg . '.' . strtolower($extension); $file = $type . $cImg . '.' . strtolower($extension);
} elseif ($type === 'oleObject') { } elseif ($type === 'oleObject') {
@ -124,26 +103,22 @@ class Media
$folder = 'embedding'; $folder = 'embedding';
$file = $type . $cObj . '.bin'; $file = $type . $cObj . '.bin';
} }
$media['source'] = $src; $media['source'] = $src;
$media['target'] = "$folder/section_$file"; $media['target'] = "$folder/section_$file";
$media['type'] = $type; $media['type'] = $type;
$media['rID'] = $rID; $media['rID'] = $rID;
self::$_sectionMedia[$key][$mediaId] = $media; self::$_sectionMedia[$key][$mediaId] = $media;
if ($type === 'oleObject') { if ($type === 'oleObject') {
return array($rID, ++self::$_objectId); return array($rID, ++self::$_objectId);
} }
return $rID; 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 int $headerCount
* @param string $src * @param string $src
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage * @param \PhpOffice\PhpWord\Section\Image $image
* @return int * @return int
*/ */
public static function addHeaderMediaElement($headerCount, $src, MemoryImage $memoryImage = null) public static function addHeaderMediaElement($headerCount, $src, Image $image = null)
{ {
$mediaId = md5($src); $mediaId = md5($src);
$key = 'header' . $headerCount; $key = 'header' . $headerCount;
if (!array_key_exists($key, self::$_headerMedia)) { if (!array_key_exists($key, self::$_headerMedia)) {
self::$_headerMedia[$key] = array(); self::$_headerMedia[$key] = array();
} }
if (!array_key_exists($mediaId, self::$_headerMedia[$key])) { if (!array_key_exists($mediaId, self::$_headerMedia[$key])) {
$cImg = self::countHeaderMediaElements($key); $cImg = self::countHeaderMediaElements($key);
$rID = $cImg + 1; $rID = $cImg + 1;
$cImg++; $cImg++;
$inf = pathinfo($src);
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
$media = array(); $media = array();
if ($isMemImage) { if (!is_null($image)) {
$ext = $memoryImage->getImageExtension(); $isMemImage = $image->getIsMemImage();
$media['isMemImage'] = true; $extension = $image->getImageExtension();
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
} else { } else {
$ext = $inf['extension']; $isMemImage = false;
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
$ext = 'jpg';
}
} }
$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['source'] = $src;
$media['target'] = 'media/' . $key . '_' . $file; $media['target'] = 'media/' . $key . '_' . $file;
$media['type'] = 'image'; $media['type'] = 'image';
$media['rID'] = $rID; $media['rID'] = $rID;
self::$_headerMedia[$key][$mediaId] = $media; self::$_headerMedia[$key][$mediaId] = $media;
return $rID; 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 int $footerCount
* @param string $src * @param string $src
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage * @param \PhpOffice\PhpWord\Section\Image $image
* @return int * @return int
*/ */
public static function addFooterMediaElement($footerCount, $src, MemoryImage $memoryImage = null) public static function addFooterMediaElement($footerCount, $src, Image $image = null)
{ {
$mediaId = md5($src); $mediaId = md5($src);
$key = 'footer' . $footerCount; $key = 'footer' . $footerCount;
if (!array_key_exists($key, self::$_footerMedia)) { if (!array_key_exists($key, self::$_footerMedia)) {
self::$_footerMedia[$key] = array(); self::$_footerMedia[$key] = array();
} }
if (!array_key_exists($mediaId, self::$_footerMedia[$key])) { if (!array_key_exists($mediaId, self::$_footerMedia[$key])) {
$cImg = self::countFooterMediaElements($key); $cImg = self::countFooterMediaElements($key);
$rID = $cImg + 1; $rID = $cImg + 1;
$cImg++; $cImg++;
$inf = pathinfo($src); if (!is_null($image)) {
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false; $isMemImage = $image->getIsMemImage();
$extension = $image->getImageExtension();
$media = array();
if ($isMemImage) {
$ext = $memoryImage->getImageExtension();
$media['isMemImage'] = true;
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
} else { } else {
$ext = $inf['extension']; $isMemImage = false;
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
$ext = 'jpg';
}
} }
$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['source'] = $src;
$media['target'] = 'media/' . $key . '_' . $file; $media['target'] = 'media/' . $key . '_' . $file;
$media['type'] = 'image'; $media['type'] = 'image';
$media['rID'] = $rID; $media['rID'] = $rID;
self::$_footerMedia[$key][$mediaId] = $media; self::$_footerMedia[$key][$mediaId] = $media;
return $rID; 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\Header;
use PhpOffice\PhpWord\Section\Link; use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem; use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object; use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak; use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Settings; use PhpOffice\PhpWord\Section\Settings;
@ -250,7 +249,7 @@ class Section
$iconSrc .= '_' . $ext . '.png'; $iconSrc .= '_' . $ext . '.png';
} }
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image'); $rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc));
$data = Media::addSectionMediaElement($src, 'oleObject'); $data = Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0]; $rID = $data[0];
$objectId = $data[1]; $objectId = $data[1];
@ -276,15 +275,14 @@ class Section
public function addImage($src, $style = null) public function addImage($src, $style = null)
{ {
$image = new Image($src, $style); $image = new Image($src, $style);
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
$rID = Media::addSectionMediaElement($src, 'image'); $rID = Media::addSectionMediaElement($src, 'image', $image);
$image->setRelationId($rID); $image->setRelationId($rID);
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $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 string $link
* @param mixed $style * @param mixed $style
* @return \PhpOffice\PhpWord\Section\MemoryImage * @deprecated
* @throws \PhpOffice\PhpWord\Exceptions\Exception
*/ */
public function addMemoryImage($link, $style = null) public function addMemoryImage($src, $style = null)
{ {
$memoryImage = new MemoryImage($link, $style); return $this->addImage($src, $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.');
} }
/** /**

View File

@ -134,15 +134,13 @@ class Footer
public function addImage($src, $style = null) public function addImage($src, $style = null)
{ {
$image = new Image($src, $style); $image = new Image($src, $style);
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
$rID = Media::addFooterMediaElement($this->_footerCount, $src); $rID = Media::addFooterMediaElement($this->_footerCount, $src, $image);
$image->setRelationId($rID); $image->setRelationId($rID);
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } 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 string $link
* @param mixed $style * @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); return $this->addImage($src, $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.');
}
} }
/** /**

View File

@ -163,15 +163,13 @@ class Header
public function addImage($src, $style = null) public function addImage($src, $style = null)
{ {
$image = new Image($src, $style); $image = new Image($src, $style);
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $src); $rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image);
$image->setRelationId($rID); $image->setRelationId($rID);
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } 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 string $link
* @param mixed $style * @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); return $this->addImage($src, $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.');
}
} }
/** /**
@ -224,11 +213,9 @@ class Header
public function addWatermark($src, $style = null) public function addWatermark($src, $style = null)
{ {
$image = new Image($src, $style, true); $image = new Image($src, $style, true);
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
$rID = Media::addHeaderMediaElement($this->_headerCount, $src); $rID = Media::addHeaderMediaElement($this->_headerCount, $src, $image);
$image->setRelationId($rID); $image->setRelationId($rID);
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {

View File

@ -34,77 +34,131 @@ use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
class Image class Image
{ {
/** /**
* Image Src * Image source
* *
* @var string * @var string
*/ */
private $_src; private $source;
/** /**
* Image Style * Image style
* *
* @var \PhpOffice\PhpWord\Style\Image * @var \PhpOffice\PhpWord\Style\Image
*/ */
private $_style; private $style;
/** /**
* Image Relation ID * Image relation ID specific only for DOCX
* *
* @var string * @var string
*/ */
private $_rId; private $rId;
/** /**
* Is Watermark * Is watermark
* *
* @var bool * @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 mixed $style
* @param bool $isWatermark * @param bool $isWatermark
* @throws \PhpOffice\PhpWord\Exceptions\InvalidImageException * @throws \PhpOffice\PhpWord\Exceptions\InvalidImageException
* @throws \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException * @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); // Detect if it's a memory image, by .php ext or by URL
if (stripos(strrev($source), strrev('.php')) === 0) {
if (!\file_exists($src)) { $this->isMemImage = true;
throw new InvalidImageException; } else {
$this->isMemImage = (filter_var($source, \FILTER_VALIDATE_URL) !== false);
} }
if (!in_array(exif_imagetype($src), $supportedImageTypes)) { // Check supported types
throw new UnsupportedImageTypeException; 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; // Set private properties
$this->_isWatermark = $isWatermark; $this->source = $source;
$this->_style = new \PhpOffice\PhpWord\Style\Image(); $this->isWatermark = $isWatermark;
$this->style = new \PhpOffice\PhpWord\Style\Image();
if (!is_null($style) && is_array($style)) { if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) { foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
$this->_style->setStyleValue($key, $value); $this->style->setStyleValue($key, $value);
} }
} }
if (isset($style['wrappingStyle'])) { if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']); $this->style->setWrappingStyle($style['wrappingStyle']);
} }
if ($this->style->getWidth() == null && $this->style->getHeight() == null) {
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) { $this->style->setWidth($imgData[0]);
$imgData = getimagesize($this->_src); $this->style->setHeight($imgData[1]);
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
} }
$this->setImageFunctions();
} }
/** /**
@ -114,66 +168,149 @@ class Image
*/ */
public function getStyle() public function getStyle()
{ {
return $this->_style; return $this->style;
} }
/** /**
* Get Image Relation ID * Get image relation ID
* *
* @return int * @return int
*/ */
public function getRelationId() public function getRelationId()
{ {
return $this->_rId; return $this->rId;
} }
/** /**
* Set Image Relation ID * Set image relation ID
* *
* @param int $rId * @param int $rId
*/ */
public function setRelationId($rId) public function setRelationId($rId)
{ {
$this->_rId = $rId; $this->rId = $rId;
} }
/** /**
* Get Image Source * Get image source
* *
* @return string * @return string
*/ */
public function getSource() public function getSource()
{ {
return $this->_src; return $this->source;
} }
/** /**
* Get Image Media ID * Get image media ID
* *
* @return string * @return string
*/ */
public function getMediaId() public function getMediaId()
{ {
return md5($this->_src); return md5($this->source);
} }
/** /**
* Get IsWatermark * Get is watermark
* *
* @return int * @return int
*/ */
public function getIsWatermark() public function getIsWatermark()
{ {
return $this->_isWatermark; return $this->isWatermark;
} }
/** /**
* Set IsWatermark * Set is watermark
* *
* @param bool $pValue * @param bool $pValue
*/ */
public function setIsWatermark($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\Image;
use PhpOffice\PhpWord\Section\Link; use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem; use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object; use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak; use PhpOffice\PhpWord\Section\TextBreak;
@ -201,17 +200,15 @@ class Cell
public function addImage($src, $style = null) public function addImage($src, $style = null)
{ {
$image = new Image($src, $style); $image = new Image($src, $style);
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
if ($this->_insideOf == 'section') { if ($this->_insideOf == 'section') {
$rID = Media::addSectionMediaElement($src, 'image'); $rID = Media::addSectionMediaElement($src, 'image', $image);
} elseif ($this->_insideOf == 'header') { } elseif ($this->_insideOf == 'header') {
$rID = Media::addHeaderMediaElement($this->_pCount, $src); $rID = Media::addHeaderMediaElement($this->_pCount, $src, $image);
} elseif ($this->_insideOf == 'footer') { } elseif ($this->_insideOf == 'footer') {
$rID = Media::addFooterMediaElement($this->_pCount, $src); $rID = Media::addFooterMediaElement($this->_pCount, $src, $image);
} }
$image->setRelationId($rID); $image->setRelationId($rID);
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
@ -224,26 +221,11 @@ class Cell
* *
* @param string $link * @param string $link
* @param mixed $style * @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); return $this->addImage($src, $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.');
}
} }
/** /**
@ -271,7 +253,7 @@ class Cell
$iconSrc .= '_' . $ext . '.png'; $iconSrc .= '_' . $ext . '.png';
} }
$rIDimg = Media::addSectionMediaElement($iconSrc, 'image'); $rIDimg = Media::addSectionMediaElement($iconSrc, 'image', new Image($iconSrc));
$data = Media::addSectionMediaElement($src, 'oleObject'); $data = Media::addSectionMediaElement($src, 'oleObject');
$rID = $data[0]; $rID = $data[0];
$objectId = $data[1]; $objectId = $data[1];

View File

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

View File

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

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\HashTable;
use PhpOffice\PhpWord\Section\Image; use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link; use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem; use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object; use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak; use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Table; use PhpOffice\PhpWord\Section\Table;
@ -379,8 +378,7 @@ class RTF implements IWriter
$sRTFBody .= $this->getDataContentUnsupportedElement('Table'); $sRTFBody .= $this->getDataContentUnsupportedElement('Table');
} elseif ($element instanceof ListItem) { } elseif ($element instanceof ListItem) {
$sRTFBody .= $this->getDataContentUnsupportedElement('List Item'); $sRTFBody .= $this->getDataContentUnsupportedElement('List Item');
} elseif ($element instanceof Image || } elseif ($element instanceof Image) {
$element instanceof MemoryImage) {
$sRTFBody .= $this->getDataContentUnsupportedElement('Image'); $sRTFBody .= $this->getDataContentUnsupportedElement('Image');
} elseif ($element instanceof Object) { } elseif ($element instanceof Object) {
$sRTFBody .= $this->getDataContentUnsupportedElement('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\Image;
use PhpOffice\PhpWord\Section\Link; use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem; use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object; use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\Table; use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\Text;
@ -652,9 +651,7 @@ class Base extends WriterPart
$this->_writeTextBreak($xmlWriter, $element); $this->_writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof ListItem) { } elseif ($element instanceof ListItem) {
$this->_writeListItem($xmlWriter, $element); $this->_writeListItem($xmlWriter, $element);
} elseif ($element instanceof Image || } elseif ($element instanceof Image) {
$element instanceof MemoryImage
) {
$this->_writeImage($xmlWriter, $element); $this->_writeImage($xmlWriter, $element);
} elseif ($element instanceof Object) { } elseif ($element instanceof Object) {
$this->_writeObject($xmlWriter, $element); $this->_writeObject($xmlWriter, $element);

View File

@ -31,7 +31,6 @@ use PhpOffice\PhpWord\Section\Footnote;
use PhpOffice\PhpWord\Section\Image; use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Link; use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem; use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Object; use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak; use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Table; use PhpOffice\PhpWord\Section\Table;
@ -108,9 +107,7 @@ class Document extends Base
$this->_writeTable($xmlWriter, $element); $this->_writeTable($xmlWriter, $element);
} elseif ($element instanceof ListItem) { } elseif ($element instanceof ListItem) {
$this->_writeListItem($xmlWriter, $element); $this->_writeListItem($xmlWriter, $element);
} elseif ($element instanceof Image || } elseif ($element instanceof Image) {
$element instanceof MemoryImage
) {
$this->_writeImage($xmlWriter, $element); $this->_writeImage($xmlWriter, $element);
} elseif ($element instanceof Object) { } elseif ($element instanceof Object) {
$this->_writeObject($xmlWriter, $element); $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\Footer\PreserveText;
use PhpOffice\PhpWord\Section\Image; use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Table; use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak; use PhpOffice\PhpWord\Section\TextBreak;
@ -79,9 +78,7 @@ class Footer extends Base
$this->_writeTextBreak($xmlWriter, $element); $this->_writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof Table) { } elseif ($element instanceof Table) {
$this->_writeTable($xmlWriter, $element); $this->_writeTable($xmlWriter, $element);
} elseif ($element instanceof Image || } elseif ($element instanceof Image) {
$element instanceof MemoryImage
) {
$this->_writeImage($xmlWriter, $element); $this->_writeImage($xmlWriter, $element);
} elseif ($element instanceof PreserveText) { } elseif ($element instanceof PreserveText) {
$this->_writePreserveText($xmlWriter, $element); $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\Footer\PreserveText;
use PhpOffice\PhpWord\Section\Image; use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\MemoryImage;
use PhpOffice\PhpWord\Section\Table; use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text; use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak; use PhpOffice\PhpWord\Section\TextBreak;
@ -79,9 +78,7 @@ class Header extends Base
$this->_writeTextBreak($xmlWriter, $element); $this->_writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof Table) { } elseif ($element instanceof Table) {
$this->_writeTable($xmlWriter, $element); $this->_writeTable($xmlWriter, $element);
} elseif ($element instanceof Image || } elseif ($element instanceof Image) {
$element instanceof MemoryImage
) {
if (!$element->getIsWatermark()) { if (!$element->getIsWatermark()) {
$this->_writeImage($xmlWriter, $element); $this->_writeImage($xmlWriter, $element);
} else { } else {

View File

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

View File

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

View File

@ -87,4 +87,56 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$oImage->setIsWatermark(true); $oImage->setIsWatermark(true);
$this->assertEquals($oImage->getIsWatermark(), 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); $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
} }
public function testAddMemoryImageSection() public function testAddSectionImageByUrl()
{ {
$oCell = new Cell('section', 1); $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' 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
); );
$this->assertCount(1, $oCell->getElements()); $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); $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' 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
); );
$this->assertCount(1, $oCell->getElements()); $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); $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' 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'
); );
$this->assertCount(1, $oCell->getElements()); $this->assertCount(1, $oCell->getElements());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\MemoryImage', $element); $this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\Image', $element);
} }
public function testAddObjectXLS() public function testAddObjectXLS()

View File

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