Media.php
9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Exception\Exception;
/**
* Media collection
*/
class Media
{
/**
* Media elements
*
* @var array
*/
private static $elements = array();
/**
* Add new media element
*
* @since 0.10.0
* @since 0.9.2
*
* @param string $container section|headerx|footerx|footnote|endnote
* @param string $mediaType image|object|link
* @param string $source
* @param \PhpOffice\PhpWord\Element\Image $image
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*
* @return int
*/
public static function addElement($container, $mediaType, $source, Image $image = null)
{
// Assign unique media Id and initiate media container if none exists
$mediaId = md5($container . $source);
if (!isset(self::$elements[$container])) {
self::$elements[$container] = array();
}
// Add media if not exists or point to existing media
if (!isset(self::$elements[$container][$mediaId])) {
$mediaCount = self::countElements($container);
$mediaTypeCount = self::countElements($container, $mediaType);
$mediaTypeCount++;
$rId = ++$mediaCount;
$target = null;
$mediaData = array('mediaIndex' => $mediaTypeCount);
switch ($mediaType) {
// Images
case 'image':
if (is_null($image)) {
throw new Exception('Image object not assigned.');
}
$isMemImage = $image->isMemImage();
$extension = $image->getImageExtension();
$mediaData['imageExtension'] = $extension;
$mediaData['imageType'] = $image->getImageType();
if ($isMemImage) {
$mediaData['isMemImage'] = true;
$mediaData['createFunction'] = $image->getImageCreateFunction();
$mediaData['imageFunction'] = $image->getImageFunction();
}
$target = "{$container}_image{$mediaTypeCount}.{$extension}";
$image->setTarget($target);
$image->setMediaIndex($mediaTypeCount);
break;
// Objects
case 'object':
$target = "{$container}_oleObject{$mediaTypeCount}.bin";
break;
// Links
case 'link':
$target = $source;
break;
}
$mediaData['source'] = $source;
$mediaData['target'] = $target;
$mediaData['type'] = $mediaType;
$mediaData['rID'] = $rId;
self::$elements[$container][$mediaId] = $mediaData;
return $rId;
}
$mediaData = self::$elements[$container][$mediaId];
if (!is_null($image)) {
$image->setTarget($mediaData['target']);
$image->setMediaIndex($mediaData['mediaIndex']);
}
return $mediaData['rID'];
}
/**
* Get media elements count
*
* @param string $container section|headerx|footerx|footnote|endnote
* @param string $mediaType image|object|link
* @return int
* @since 0.10.0
*/
public static function countElements($container, $mediaType = null)
{
$mediaCount = 0;
if (isset(self::$elements[$container])) {
foreach (self::$elements[$container] as $mediaData) {
if (!is_null($mediaType)) {
if ($mediaType == $mediaData['type']) {
$mediaCount++;
}
} else {
$mediaCount++;
}
}
}
return $mediaCount;
}
/**
* Get media elements
*
* @param string $container section|headerx|footerx|footnote|endnote
* @param string $type image|object|link
* @return array
* @since 0.10.0
*/
public static function getElements($container, $type = null)
{
$elements = array();
// If header/footer, search for headerx and footerx where x is number
if ($container == 'header' || $container == 'footer') {
foreach (self::$elements as $key => $val) {
if (substr($key, 0, 6) == $container) {
$elements[$key] = $val;
}
}
return $elements;
}
if (!isset(self::$elements[$container])) {
return $elements;
}
return self::getElementsByType($container, $type);
}
/**
* Get elements by media type
*
* @param string $container section|footnote|endnote
* @param string $type image|object|link
* @return array
* @since 0.11.0 Splitted from `getElements` to reduce complexity
*/
private static function getElementsByType($container, $type = null)
{
$elements = array();
foreach (self::$elements[$container] as $key => $data) {
if ($type !== null) {
if ($type == $data['type']) {
$elements[$key] = $data;
}
} else {
$elements[$key] = $data;
}
}
return $elements;
}
/**
* Reset media elements
*/
public static function resetElements()
{
self::$elements = array();
}
/**
* Add new Section Media Element
*
* @deprecated 0.10.0
*
* @param string $src
* @param string $type
* @param \PhpOffice\PhpWord\Element\Image $image
*
* @return int
*
* @codeCoverageIgnore
*/
public static function addSectionMediaElement($src, $type, Image $image = null)
{
return self::addElement('section', $type, $src, $image);
}
/**
* Add new Section Link Element
*
* @deprecated 0.10.0
*
* @param string $linkSrc
*
* @return int
*
* @codeCoverageIgnore
*/
public static function addSectionLinkElement($linkSrc)
{
return self::addElement('section', 'link', $linkSrc);
}
/**
* Get Section Media Elements
*
* @deprecated 0.10.0
*
* @param string $key
*
* @return array
*
* @codeCoverageIgnore
*/
public static function getSectionMediaElements($key = null)
{
return self::getElements('section', $key);
}
/**
* Get Section Media Elements Count
*
* @deprecated 0.10.0
*
* @param string $key
*
* @return int
*
* @codeCoverageIgnore
*/
public static function countSectionMediaElements($key = null)
{
return self::countElements('section', $key);
}
/**
* Add new Header Media Element
*
* @deprecated 0.10.0
*
* @param int $headerCount
* @param string $src
* @param \PhpOffice\PhpWord\Element\Image $image
*
* @return int
*
* @codeCoverageIgnore
*/
public static function addHeaderMediaElement($headerCount, $src, Image $image = null)
{
return self::addElement("header{$headerCount}", 'image', $src, $image);
}
/**
* Get Header Media Elements Count
*
* @deprecated 0.10.0
*
* @param string $key
*
* @return int
*
* @codeCoverageIgnore
*/
public static function countHeaderMediaElements($key)
{
return self::countElements($key);
}
/**
* Get Header Media Elements
*
* @deprecated 0.10.0
*
* @return array
*
* @codeCoverageIgnore
*/
public static function getHeaderMediaElements()
{
return self::getElements('header');
}
/**
* Add new Footer Media Element
*
* @deprecated 0.10.0
*
* @param int $footerCount
* @param string $src
* @param \PhpOffice\PhpWord\Element\Image $image
*
* @return int
*
* @codeCoverageIgnore
*/
public static function addFooterMediaElement($footerCount, $src, Image $image = null)
{
return self::addElement("footer{$footerCount}", 'image', $src, $image);
}
/**
* Get Footer Media Elements Count
*
* @deprecated 0.10.0
*
* @param string $key
*
* @return int
*
* @codeCoverageIgnore
*/
public static function countFooterMediaElements($key)
{
return self::countElements($key);
}
/**
* Get Footer Media Elements
*
* @deprecated 0.10.0
*
* @return array
*
* @codeCoverageIgnore
*/
public static function getFooterMediaElements()
{
return self::getElements('footer');
}
}