diff --git a/Classes/PHPWord/Media.php b/Classes/PHPWord/Media.php index 413b73de55..28d3d03361 100755 --- a/Classes/PHPWord/Media.php +++ b/Classes/PHPWord/Media.php @@ -30,15 +30,16 @@ */ class PHPWord_Media { - /** * Section Media Elements * * @var array */ - private static $_sectionMedia = array('images' => array(), + private static $_sectionMedia = array( + 'images' => array(), 'embeddings' => array(), - 'links' => array()); + 'links' => array() + ); /** * Header Media Elements @@ -61,18 +62,18 @@ class PHPWord_Media */ private static $_objectId = 1325353440; - /** * Add new Section Media Element * * @param string $src * @param string $type + * @param PHPWord_Section_MemoryImage|null $memoryImage * @return mixed */ public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null) { $mediaId = md5($src); - $key = ($type == 'image') ? 'images' : 'embeddings'; + $key = ($type === 'image') ? 'images' : 'embeddings'; if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) { $cImg = self::countSectionMediaElements('images'); @@ -81,26 +82,39 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor $media = array(); - if ($type == 'image') { + $folder = null; + $file = null; + if ($type === 'image') { $cImg++; - $inf = pathinfo($src); - $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false; + $isMemImage = false; + if (stripos(strrev($src), strrev('.php')) === 0) { + $isMemImage = true; + } + $extension = ''; if ($isMemImage) { - $ext = $memoryImage->getImageExtension(); + $extension = $memoryImage->getImageExtension(); $media['isMemImage'] = true; $media['createfunction'] = $memoryImage->getImageCreateFunction(); $media['imagefunction'] = $memoryImage->getImageFunction(); } else { - $ext = $inf['extension']; - if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg - $ext = 'jpg'; + $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'; } } $folder = 'media'; - $file = $type . $cImg . '.' . strtolower($ext); - } elseif ($type == 'oleObject') { + $file = $type . $cImg . '.' . strtolower($extension); + } elseif ($type === 'oleObject') { $cObj++; $folder = 'embedding'; $file = $type . $cObj . '.bin'; @@ -113,27 +127,24 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor self::$_sectionMedia[$key][$mediaId] = $media; - if ($type == 'oleObject') { + if ($type === 'oleObject') { return array($rID, ++self::$_objectId); - } else { - return $rID; - } - } else { - if ($type == 'oleObject') { - $rID = self::$_sectionMedia[$key][$mediaId]['rID']; - return array($rID, ++self::$_objectId); - } else { - return self::$_sectionMedia[$key][$mediaId]['rID']; } + + return $rID; + } + + if ($type === 'oleObject') { + $rID = self::$_sectionMedia[$key][$mediaId]['rID']; + return array($rID, ++self::$_objectId); } + return self::$_sectionMedia[$key][$mediaId]['rID']; } /** * Add new Section Link Element * * @param string $linkSrc - * @param string $linkName - * * @return mixed */ public static function addSectionLinkElement($linkSrc) @@ -160,12 +171,12 @@ public static function getSectionMediaElements($key = null) { if (!is_null($key)) { return self::$_sectionMedia[$key]; - } else { - $arrImages = self::$_sectionMedia['images']; - $arrObjects = self::$_sectionMedia['embeddings']; - $arrLinks = self::$_sectionMedia['links']; - return array_merge($arrImages, $arrObjects, $arrLinks); } + + $arrImages = self::$_sectionMedia['images']; + $arrObjects = self::$_sectionMedia['embeddings']; + $arrLinks = self::$_sectionMedia['links']; + return array_merge($arrImages, $arrObjects, $arrLinks); } /** @@ -178,12 +189,12 @@ public static function countSectionMediaElements($key = null) { if (!is_null($key)) { return count(self::$_sectionMedia[$key]); - } else { - $cImages = count(self::$_sectionMedia['images']); - $cObjects = count(self::$_sectionMedia['embeddings']); - $cLinks = count(self::$_sectionMedia['links']); - return ($cImages + $cObjects + $cLinks); } + + $cImages = count(self::$_sectionMedia['images']); + $cObjects = count(self::$_sectionMedia['embeddings']); + $cLinks = count(self::$_sectionMedia['links']); + return ($cImages + $cObjects + $cLinks); } /** @@ -191,6 +202,7 @@ public static function countSectionMediaElements($key = null) * * @param int $headerCount * @param string $src + * @param PHPWord_Section_MemoryImage|null $memoryImage * @return int */ public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) @@ -232,9 +244,8 @@ public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section self::$_headerMedia[$key][$mediaId] = $media; return $rID; - } else { - return self::$_headerMedia[$key][$mediaId]['rID']; } + return self::$_headerMedia[$key][$mediaId]['rID']; } /** @@ -263,6 +274,7 @@ public static function getHeaderMediaElements() * * @param int $footerCount * @param string $src + * @param PHPWord_Section_MemoryImage|null $memoryImage * @return int */ public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) @@ -304,9 +316,8 @@ public static function addFooterMediaElement($footerCount, $src, PHPWord_Section self::$_footerMedia[$key][$mediaId] = $media; return $rID; - } else { - return self::$_footerMedia[$key][$mediaId]['rID']; } + return self::$_footerMedia[$key][$mediaId]['rID']; } /** diff --git a/Classes/PHPWord/Section/MemoryImage.php b/Classes/PHPWord/Section/MemoryImage.php index 9652955cdc..8486a2a313 100755 --- a/Classes/PHPWord/Section/MemoryImage.php +++ b/Classes/PHPWord/Section/MemoryImage.php @@ -30,7 +30,6 @@ */ class PHPWord_Section_MemoryImage { - /** * Image Src * @@ -85,7 +84,7 @@ class PHPWord_Section_MemoryImage * Create a new Image * * @param string $src - * @param mixed style + * @param mixed $style */ public function __construct($src, $style = null) { @@ -113,10 +112,6 @@ public function __construct($src, $style = null) } $this->_setFunctions(); - - return $this; - } else { - return false; } } @@ -145,7 +140,6 @@ private function _setFunctions() } } - /** * Get Image style * @@ -235,4 +229,4 @@ public function getImageExtension() { return $this->_imageExtension; } -} +} \ No newline at end of file diff --git a/Tests/PHPWord/MediaTest.php b/Tests/PHPWord/MediaTest.php index 86e9651ab8..e572272435 100644 --- a/Tests/PHPWord/MediaTest.php +++ b/Tests/PHPWord/MediaTest.php @@ -3,6 +3,7 @@ use PHPUnit_Framework_TestCase; use PHPWord_Media; +use PHPWord_Section; class MediaTest extends \PHPUnit_Framework_TestCase { @@ -25,4 +26,26 @@ public function testGetFooterMediaElements() { $this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia', 'PHPWord_Media'); } -} + + /** + * Todo: add memory image to this test + * + * @covers PHPWord_Media::addSectionMediaElement + */ + public function testAddSectionMediaElement() + { + $section = new PHPWord_Section(0); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif"); + + $elements = $section->getElements(); + $this->assertEquals(6, count($elements)); + foreach ($elements as $element) { + $this->assertInstanceOf('PHPWord_Section_Image', $element); + } + } +} \ No newline at end of file diff --git a/Tests/PHPWord/Section/ImageTest.php b/Tests/PHPWord/Section/ImageTest.php index 5832b8c308..9362be191c 100644 --- a/Tests/PHPWord/Section/ImageTest.php +++ b/Tests/PHPWord/Section/ImageTest.php @@ -37,6 +37,9 @@ public function testConstructWithStyle() $this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle()); } + /** + * @covers PHPWord_Section_Image::__construct + */ public function testValidImageTypes() { new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); @@ -49,6 +52,7 @@ public function testValidImageTypes() /** * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException + * @covers PHPWord_Section_Image::__construct */ public function testImageNotFound() { @@ -57,6 +61,7 @@ public function testImageNotFound() /** * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException + * @covers PHPWord_Section_Image::__construct */ public function testInvalidImageTypes() { diff --git a/Tests/PHPWord/Style/TabsTest.php b/Tests/PHPWord/Style/TabsTest.php index 1be0c350e0..3a3d69df0e 100644 --- a/Tests/PHPWord/Style/TabsTest.php +++ b/Tests/PHPWord/Style/TabsTest.php @@ -16,8 +16,8 @@ class TabsTest extends \PHPUnit_Framework_TestCase { /** - * Executed before each method of the class - */ + * Executed before each method of the class + */ public function tearDown() { TestHelperDOCX::clear();