From eed86f3768e4a12c53648cde00b4ee20037cd0fe Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 24 Mar 2014 16:24:50 +0700 Subject: [PATCH] Merge MemoryImage with Image, but left addMemoryImage for backward compatibility --- CHANGELOG.md | 6 +- src/PhpWord/Media.php | 134 ++++------ src/PhpWord/Section.php | 25 +- src/PhpWord/Section/Footer.php | 21 +- src/PhpWord/Section/Header.php | 25 +- src/PhpWord/Section/Image.php | 223 +++++++++++++---- src/PhpWord/Section/MemoryImage.php | 232 ------------------ src/PhpWord/Section/Table/Cell.php | 32 +-- src/PhpWord/Section/TextRun.php | 4 +- src/PhpWord/Writer/ODText/Content.php | 4 +- src/PhpWord/Writer/RTF.php | 4 +- src/PhpWord/Writer/Word2007/Base.php | 5 +- src/PhpWord/Writer/Word2007/Document.php | 5 +- src/PhpWord/Writer/Word2007/Footer.php | 5 +- src/PhpWord/Writer/Word2007/Header.php | 5 +- test/PhpWord/Tests/Section/FooterTest.php | 6 +- test/PhpWord/Tests/Section/HeaderTest.php | 6 +- test/PhpWord/Tests/Section/ImageTest.php | 52 ++++ .../PhpWord/Tests/Section/MemoryImageTest.php | 78 ------ test/PhpWord/Tests/Section/Table/CellTest.php | 18 +- test/PhpWord/Tests/SectionTest.php | 5 +- 21 files changed, 331 insertions(+), 564 deletions(-) delete mode 100644 src/PhpWord/Section/MemoryImage.php delete mode 100644 test/PhpWord/Tests/Section/MemoryImageTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 50f1dd87..867bb2aa 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PhpWord/Media.php b/src/PhpWord/Media.php index 5a91bb08..7b76d879 100755 --- a/src/PhpWord/Media.php +++ b/src/PhpWord/Media.php @@ -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']; } /** diff --git a/src/PhpWord/Section.php b/src/PhpWord/Section.php index 68154915..11ad41cd 100644 --- a/src/PhpWord/Section.php +++ b/src/PhpWord/Section.php @@ -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); } /** diff --git a/src/PhpWord/Section/Footer.php b/src/PhpWord/Section/Footer.php index a9800722..b33211a8 100755 --- a/src/PhpWord/Section/Footer.php +++ b/src/PhpWord/Section/Footer.php @@ -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); } /** diff --git a/src/PhpWord/Section/Header.php b/src/PhpWord/Section/Header.php index 6f407934..8faf135e 100755 --- a/src/PhpWord/Section/Header.php +++ b/src/PhpWord/Section/Header.php @@ -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 { diff --git a/src/PhpWord/Section/Image.php b/src/PhpWord/Section/Image.php index dfe38abd..844eb382 100755 --- a/src/PhpWord/Section/Image.php +++ b/src/PhpWord/Section/Image.php @@ -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; + } } } diff --git a/src/PhpWord/Section/MemoryImage.php b/src/PhpWord/Section/MemoryImage.php deleted file mode 100644 index 4fce71f0..00000000 --- a/src/PhpWord/Section/MemoryImage.php +++ /dev/null @@ -1,232 +0,0 @@ -_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; - } -} diff --git a/src/PhpWord/Section/Table/Cell.php b/src/PhpWord/Section/Table/Cell.php index 46e9da4f..80e66493 100755 --- a/src/PhpWord/Section/Table/Cell.php +++ b/src/PhpWord/Section/Table/Cell.php @@ -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]; diff --git a/src/PhpWord/Section/TextRun.php b/src/PhpWord/Section/TextRun.php index 0fe25af2..77b203eb 100755 --- a/src/PhpWord/Section/TextRun.php +++ b/src/PhpWord/Section/TextRun.php @@ -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 { diff --git a/src/PhpWord/Writer/ODText/Content.php b/src/PhpWord/Writer/ODText/Content.php index 0b2c4ae7..b9dcba49 100644 --- a/src/PhpWord/Writer/ODText/Content.php +++ b/src/PhpWord/Writer/ODText/Content.php @@ -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'); diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 5bf2c9be..ced98aaa 100755 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -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'); diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 977b10a0..6ee0247c 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -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); diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index a598ccb0..e515a923 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -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); diff --git a/src/PhpWord/Writer/Word2007/Footer.php b/src/PhpWord/Writer/Word2007/Footer.php index 2a780af7..0db12b09 100644 --- a/src/PhpWord/Writer/Word2007/Footer.php +++ b/src/PhpWord/Writer/Word2007/Footer.php @@ -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); diff --git a/src/PhpWord/Writer/Word2007/Header.php b/src/PhpWord/Writer/Word2007/Header.php index 5dd73723..d2e97bb8 100644 --- a/src/PhpWord/Writer/Word2007/Header.php +++ b/src/PhpWord/Writer/Word2007/Header.php @@ -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 { diff --git a/test/PhpWord/Tests/Section/FooterTest.php b/test/PhpWord/Tests/Section/FooterTest.php index b2531757..2abb774e 100644 --- a/test/PhpWord/Tests/Section/FooterTest.php +++ b/test/PhpWord/Tests/Section/FooterTest.php @@ -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() diff --git a/test/PhpWord/Tests/Section/HeaderTest.php b/test/PhpWord/Tests/Section/HeaderTest.php index 9b40c140..4a568639 100644 --- a/test/PhpWord/Tests/Section/HeaderTest.php +++ b/test/PhpWord/Tests/Section/HeaderTest.php @@ -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() diff --git a/test/PhpWord/Tests/Section/ImageTest.php b/test/PhpWord/Tests/Section/ImageTest.php index 5d1d58e5..1de1b47f 100644 --- a/test/PhpWord/Tests/Section/ImageTest.php +++ b/test/PhpWord/Tests/Section/ImageTest.php @@ -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'); + } } diff --git a/test/PhpWord/Tests/Section/MemoryImageTest.php b/test/PhpWord/Tests/Section/MemoryImageTest.php deleted file mode 100644 index 5dfef076..00000000 --- a/test/PhpWord/Tests/Section/MemoryImageTest.php +++ /dev/null @@ -1,78 +0,0 @@ -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); - } -} diff --git a/test/PhpWord/Tests/Section/Table/CellTest.php b/test/PhpWord/Tests/Section/Table/CellTest.php index 9f1a6da5..17ca8239 100644 --- a/test/PhpWord/Tests/Section/Table/CellTest.php +++ b/test/PhpWord/Tests/Section/Table/CellTest.php @@ -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() diff --git a/test/PhpWord/Tests/SectionTest.php b/test/PhpWord/Tests/SectionTest.php index 216956e7..629f8dde 100644 --- a/test/PhpWord/Tests/SectionTest.php +++ b/test/PhpWord/Tests/SectionTest.php @@ -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) {