30
30
*/
31
31
class PHPWord_Media
32
32
{
33
-
34
33
/**
35
34
* Section Media Elements
36
35
*
37
36
* @var array
38
37
*/
39
- private static $ _sectionMedia = array ('images ' => array (),
38
+ private static $ _sectionMedia = array (
39
+ 'images ' => array (),
40
40
'embeddings ' => array (),
41
- 'links ' => array ());
41
+ 'links ' => array ()
42
+ );
42
43
43
44
/**
44
45
* Header Media Elements
@@ -61,18 +62,18 @@ class PHPWord_Media
61
62
*/
62
63
private static $ _objectId = 1325353440 ;
63
64
64
-
65
65
/**
66
66
* Add new Section Media Element
67
67
*
68
68
* @param string $src
69
69
* @param string $type
70
+ * @param PHPWord_Section_MemoryImage|null $memoryImage
70
71
* @return mixed
71
72
*/
72
73
public static function addSectionMediaElement ($ src , $ type , PHPWord_Section_MemoryImage $ memoryImage = null )
73
74
{
74
75
$ mediaId = md5 ($ src );
75
- $ key = ($ type == 'image ' ) ? 'images ' : 'embeddings ' ;
76
+ $ key = ($ type === 'image ' ) ? 'images ' : 'embeddings ' ;
76
77
77
78
if (!array_key_exists ($ mediaId , self ::$ _sectionMedia [$ key ])) {
78
79
$ cImg = self ::countSectionMediaElements ('images ' );
@@ -81,26 +82,39 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
81
82
82
83
$ media = array ();
83
84
84
- if ($ type == 'image ' ) {
85
+ $ folder = null ;
86
+ $ file = null ;
87
+ if ($ type === 'image ' ) {
85
88
$ 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
+ }
88
93
94
+ $ extension = '' ;
89
95
if ($ isMemImage ) {
90
- $ ext = $ memoryImage ->getImageExtension ();
96
+ $ extension = $ memoryImage ->getImageExtension ();
91
97
$ media ['isMemImage ' ] = true ;
92
98
$ media ['createfunction ' ] = $ memoryImage ->getImageCreateFunction ();
93
99
$ media ['imagefunction ' ] = $ memoryImage ->getImageFunction ();
94
100
} 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 ' ;
98
112
}
99
113
}
100
114
101
115
$ folder = 'media ' ;
102
- $ file = $ type . $ cImg . '. ' . strtolower ($ ext );
103
- } elseif ($ type == 'oleObject ' ) {
116
+ $ file = $ type . $ cImg . '. ' . strtolower ($ extension );
117
+ } elseif ($ type === 'oleObject ' ) {
104
118
$ cObj ++;
105
119
$ folder = 'embedding ' ;
106
120
$ file = $ type . $ cObj . '.bin ' ;
@@ -113,27 +127,24 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
113
127
114
128
self ::$ _sectionMedia [$ key ][$ mediaId ] = $ media ;
115
129
116
- if ($ type == 'oleObject ' ) {
130
+ if ($ type === 'oleObject ' ) {
117
131
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 ' ];
127
132
}
133
+
134
+ return $ rID ;
135
+ }
136
+
137
+ if ($ type === 'oleObject ' ) {
138
+ $ rID = self ::$ _sectionMedia [$ key ][$ mediaId ]['rID ' ];
139
+ return array ($ rID , ++self ::$ _objectId );
128
140
}
141
+ return self ::$ _sectionMedia [$ key ][$ mediaId ]['rID ' ];
129
142
}
130
143
131
144
/**
132
145
* Add new Section Link Element
133
146
*
134
147
* @param string $linkSrc
135
- * @param string $linkName
136
- *
137
148
* @return mixed
138
149
*/
139
150
public static function addSectionLinkElement ($ linkSrc )
@@ -160,12 +171,12 @@ public static function getSectionMediaElements($key = null)
160
171
{
161
172
if (!is_null ($ key )) {
162
173
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 );
168
174
}
175
+
176
+ $ arrImages = self ::$ _sectionMedia ['images ' ];
177
+ $ arrObjects = self ::$ _sectionMedia ['embeddings ' ];
178
+ $ arrLinks = self ::$ _sectionMedia ['links ' ];
179
+ return array_merge ($ arrImages , $ arrObjects , $ arrLinks );
169
180
}
170
181
171
182
/**
@@ -178,19 +189,20 @@ public static function countSectionMediaElements($key = null)
178
189
{
179
190
if (!is_null ($ key )) {
180
191
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 );
186
192
}
193
+
194
+ $ cImages = count (self ::$ _sectionMedia ['images ' ]);
195
+ $ cObjects = count (self ::$ _sectionMedia ['embeddings ' ]);
196
+ $ cLinks = count (self ::$ _sectionMedia ['links ' ]);
197
+ return ($ cImages + $ cObjects + $ cLinks );
187
198
}
188
199
189
200
/**
190
201
* Add new Header Media Element
191
202
*
192
203
* @param int $headerCount
193
204
* @param string $src
205
+ * @param PHPWord_Section_MemoryImage|null $memoryImage
194
206
* @return int
195
207
*/
196
208
public static function addHeaderMediaElement ($ headerCount , $ src , PHPWord_Section_MemoryImage $ memoryImage = null )
@@ -232,9 +244,8 @@ public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section
232
244
self ::$ _headerMedia [$ key ][$ mediaId ] = $ media ;
233
245
234
246
return $ rID ;
235
- } else {
236
- return self ::$ _headerMedia [$ key ][$ mediaId ]['rID ' ];
237
247
}
248
+ return self ::$ _headerMedia [$ key ][$ mediaId ]['rID ' ];
238
249
}
239
250
240
251
/**
@@ -263,6 +274,7 @@ public static function getHeaderMediaElements()
263
274
*
264
275
* @param int $footerCount
265
276
* @param string $src
277
+ * @param PHPWord_Section_MemoryImage|null $memoryImage
266
278
* @return int
267
279
*/
268
280
public static function addFooterMediaElement ($ footerCount , $ src , PHPWord_Section_MemoryImage $ memoryImage = null )
@@ -304,9 +316,8 @@ public static function addFooterMediaElement($footerCount, $src, PHPWord_Section
304
316
self ::$ _footerMedia [$ key ][$ mediaId ] = $ media ;
305
317
306
318
return $ rID ;
307
- } else {
308
- return self ::$ _footerMedia [$ key ][$ mediaId ]['rID ' ];
309
319
}
320
+ return self ::$ _footerMedia [$ key ][$ mediaId ]['rID ' ];
310
321
}
311
322
312
323
/**
0 commit comments