Skip to content

Commit 68de9ab

Browse files
committed
Merge pull request #114 from gabrielbull/develop
Use exif_imagetype to check image format instead of extension name
2 parents 9c0e70a + 492f88d commit 68de9ab

File tree

5 files changed

+84
-51
lines changed

5 files changed

+84
-51
lines changed

Classes/PHPWord/Media.php

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@
3030
*/
3131
class PHPWord_Media
3232
{
33-
3433
/**
3534
* Section Media Elements
3635
*
3736
* @var array
3837
*/
39-
private static $_sectionMedia = array('images' => array(),
38+
private static $_sectionMedia = array(
39+
'images' => array(),
4040
'embeddings' => array(),
41-
'links' => array());
41+
'links' => array()
42+
);
4243

4344
/**
4445
* Header Media Elements
@@ -61,18 +62,18 @@ class PHPWord_Media
6162
*/
6263
private static $_objectId = 1325353440;
6364

64-
6565
/**
6666
* Add new Section Media Element
6767
*
6868
* @param string $src
6969
* @param string $type
70+
* @param PHPWord_Section_MemoryImage|null $memoryImage
7071
* @return mixed
7172
*/
7273
public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null)
7374
{
7475
$mediaId = md5($src);
75-
$key = ($type == 'image') ? 'images' : 'embeddings';
76+
$key = ($type === 'image') ? 'images' : 'embeddings';
7677

7778
if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
7879
$cImg = self::countSectionMediaElements('images');
@@ -81,26 +82,39 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
8182

8283
$media = array();
8384

84-
if ($type == 'image') {
85+
$folder = null;
86+
$file = null;
87+
if ($type === 'image') {
8588
$cImg++;
86-
$inf = pathinfo($src);
87-
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false;
89+
$isMemImage = false;
90+
if (stripos(strrev($src), strrev('.php')) === 0) {
91+
$isMemImage = true;
92+
}
8893

94+
$extension = '';
8995
if ($isMemImage) {
90-
$ext = $memoryImage->getImageExtension();
96+
$extension = $memoryImage->getImageExtension();
9197
$media['isMemImage'] = true;
9298
$media['createfunction'] = $memoryImage->getImageCreateFunction();
9399
$media['imagefunction'] = $memoryImage->getImageFunction();
94100
} else {
95-
$ext = $inf['extension'];
96-
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
97-
$ext = 'jpg';
101+
$imageType = exif_imagetype($src);
102+
if ($imageType === IMAGETYPE_JPEG) {
103+
$extension = 'jpg';
104+
} elseif ($imageType === IMAGETYPE_GIF) {
105+
$extension = 'gif';
106+
} elseif ($imageType === IMAGETYPE_PNG) {
107+
$extension = 'png';
108+
} elseif ($imageType === IMAGETYPE_BMP) {
109+
$extension = 'bmp';
110+
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
111+
$extension = 'tif';
98112
}
99113
}
100114

101115
$folder = 'media';
102-
$file = $type . $cImg . '.' . strtolower($ext);
103-
} elseif ($type == 'oleObject') {
116+
$file = $type . $cImg . '.' . strtolower($extension);
117+
} elseif ($type === 'oleObject') {
104118
$cObj++;
105119
$folder = 'embedding';
106120
$file = $type . $cObj . '.bin';
@@ -113,27 +127,24 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
113127

114128
self::$_sectionMedia[$key][$mediaId] = $media;
115129

116-
if ($type == 'oleObject') {
130+
if ($type === 'oleObject') {
117131
return array($rID, ++self::$_objectId);
118-
} else {
119-
return $rID;
120-
}
121-
} else {
122-
if ($type == 'oleObject') {
123-
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
124-
return array($rID, ++self::$_objectId);
125-
} else {
126-
return self::$_sectionMedia[$key][$mediaId]['rID'];
127132
}
133+
134+
return $rID;
135+
}
136+
137+
if ($type === 'oleObject') {
138+
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
139+
return array($rID, ++self::$_objectId);
128140
}
141+
return self::$_sectionMedia[$key][$mediaId]['rID'];
129142
}
130143

131144
/**
132145
* Add new Section Link Element
133146
*
134147
* @param string $linkSrc
135-
* @param string $linkName
136-
*
137148
* @return mixed
138149
*/
139150
public static function addSectionLinkElement($linkSrc)
@@ -160,12 +171,12 @@ public static function getSectionMediaElements($key = null)
160171
{
161172
if (!is_null($key)) {
162173
return self::$_sectionMedia[$key];
163-
} else {
164-
$arrImages = self::$_sectionMedia['images'];
165-
$arrObjects = self::$_sectionMedia['embeddings'];
166-
$arrLinks = self::$_sectionMedia['links'];
167-
return array_merge($arrImages, $arrObjects, $arrLinks);
168174
}
175+
176+
$arrImages = self::$_sectionMedia['images'];
177+
$arrObjects = self::$_sectionMedia['embeddings'];
178+
$arrLinks = self::$_sectionMedia['links'];
179+
return array_merge($arrImages, $arrObjects, $arrLinks);
169180
}
170181

171182
/**
@@ -178,19 +189,20 @@ public static function countSectionMediaElements($key = null)
178189
{
179190
if (!is_null($key)) {
180191
return count(self::$_sectionMedia[$key]);
181-
} else {
182-
$cImages = count(self::$_sectionMedia['images']);
183-
$cObjects = count(self::$_sectionMedia['embeddings']);
184-
$cLinks = count(self::$_sectionMedia['links']);
185-
return ($cImages + $cObjects + $cLinks);
186192
}
193+
194+
$cImages = count(self::$_sectionMedia['images']);
195+
$cObjects = count(self::$_sectionMedia['embeddings']);
196+
$cLinks = count(self::$_sectionMedia['links']);
197+
return ($cImages + $cObjects + $cLinks);
187198
}
188199

189200
/**
190201
* Add new Header Media Element
191202
*
192203
* @param int $headerCount
193204
* @param string $src
205+
* @param PHPWord_Section_MemoryImage|null $memoryImage
194206
* @return int
195207
*/
196208
public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
@@ -232,9 +244,8 @@ public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section
232244
self::$_headerMedia[$key][$mediaId] = $media;
233245

234246
return $rID;
235-
} else {
236-
return self::$_headerMedia[$key][$mediaId]['rID'];
237247
}
248+
return self::$_headerMedia[$key][$mediaId]['rID'];
238249
}
239250

240251
/**
@@ -263,6 +274,7 @@ public static function getHeaderMediaElements()
263274
*
264275
* @param int $footerCount
265276
* @param string $src
277+
* @param PHPWord_Section_MemoryImage|null $memoryImage
266278
* @return int
267279
*/
268280
public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
@@ -304,9 +316,8 @@ public static function addFooterMediaElement($footerCount, $src, PHPWord_Section
304316
self::$_footerMedia[$key][$mediaId] = $media;
305317

306318
return $rID;
307-
} else {
308-
return self::$_footerMedia[$key][$mediaId]['rID'];
309319
}
320+
return self::$_footerMedia[$key][$mediaId]['rID'];
310321
}
311322

312323
/**

Classes/PHPWord/Section/MemoryImage.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131
class PHPWord_Section_MemoryImage
3232
{
33-
3433
/**
3534
* Image Src
3635
*
@@ -85,7 +84,7 @@ class PHPWord_Section_MemoryImage
8584
* Create a new Image
8685
*
8786
* @param string $src
88-
* @param mixed style
87+
* @param mixed $style
8988
*/
9089
public function __construct($src, $style = null)
9190
{
@@ -113,10 +112,6 @@ public function __construct($src, $style = null)
113112
}
114113

115114
$this->_setFunctions();
116-
117-
return $this;
118-
} else {
119-
return false;
120115
}
121116
}
122117

@@ -145,7 +140,6 @@ private function _setFunctions()
145140
}
146141
}
147142

148-
149143
/**
150144
* Get Image style
151145
*
@@ -235,4 +229,4 @@ public function getImageExtension()
235229
{
236230
return $this->_imageExtension;
237231
}
238-
}
232+
}

Tests/PHPWord/MediaTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPUnit_Framework_TestCase;
55
use PHPWord_Media;
6+
use PHPWord_Section;
67

78
class MediaTest extends \PHPUnit_Framework_TestCase
89
{
@@ -25,4 +26,26 @@ public function testGetFooterMediaElements()
2526
{
2627
$this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia', 'PHPWord_Media');
2728
}
28-
}
29+
30+
/**
31+
* Todo: add memory image to this test
32+
*
33+
* @covers PHPWord_Media::addSectionMediaElement
34+
*/
35+
public function testAddSectionMediaElement()
36+
{
37+
$section = new PHPWord_Section(0);
38+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
39+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
40+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
41+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
42+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
43+
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
44+
45+
$elements = $section->getElements();
46+
$this->assertEquals(6, count($elements));
47+
foreach ($elements as $element) {
48+
$this->assertInstanceOf('PHPWord_Section_Image', $element);
49+
}
50+
}
51+
}

Tests/PHPWord/Section/ImageTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function testConstructWithStyle()
3737
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
3838
}
3939

40+
/**
41+
* @covers PHPWord_Section_Image::__construct
42+
*/
4043
public function testValidImageTypes()
4144
{
4245
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
@@ -49,6 +52,7 @@ public function testValidImageTypes()
4952

5053
/**
5154
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException
55+
* @covers PHPWord_Section_Image::__construct
5256
*/
5357
public function testImageNotFound()
5458
{
@@ -57,6 +61,7 @@ public function testImageNotFound()
5761

5862
/**
5963
* @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
64+
* @covers PHPWord_Section_Image::__construct
6065
*/
6166
public function testInvalidImageTypes()
6267
{

Tests/PHPWord/Style/TabsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
class TabsTest extends \PHPUnit_Framework_TestCase
1717
{
1818
/**
19-
* Executed before each method of the class
20-
*/
19+
* Executed before each method of the class
20+
*/
2121
public function tearDown()
2222
{
2323
TestHelperDOCX::clear();

0 commit comments

Comments
 (0)