|
25 | 25 |
|
26 | 26 | namespace PhpOffice\PhpWord;
|
27 | 27 |
|
28 |
| -use PhpOffice\PhpWord\Section\MemoryImage; |
| 28 | +use PhpOffice\PhpWord\Section\Image; |
29 | 29 |
|
30 | 30 | /**
|
31 | 31 | * Media
|
@@ -69,81 +69,56 @@ class Media
|
69 | 69 | *
|
70 | 70 | * @param string $src
|
71 | 71 | * @param string $type
|
72 |
| - * @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage |
| 72 | + * @param \PhpOffice\PhpWord\Section\Image $image |
73 | 73 | * @return mixed
|
74 | 74 | */
|
75 |
| - public static function addSectionMediaElement($src, $type, MemoryImage $memoryImage = null) |
| 75 | + public static function addSectionMediaElement($src, $type, Image $image = null) |
76 | 76 | {
|
77 | 77 | $mediaId = md5($src);
|
78 | 78 | $key = ($type === 'image') ? 'images' : 'embeddings';
|
79 |
| - |
80 | 79 | if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
|
81 | 80 | $cImg = self::countSectionMediaElements('images');
|
82 | 81 | $cObj = self::countSectionMediaElements('embeddings');
|
83 | 82 | $rID = self::countSectionMediaElements() + 7;
|
84 |
| - |
85 | 83 | $media = array();
|
86 |
| - |
87 | 84 | $folder = null;
|
88 | 85 | $file = null;
|
89 | 86 | if ($type === 'image') {
|
90 | 87 | $cImg++;
|
91 |
| - //Detect if it's a memory image first by php ext and second by regex |
92 |
| - $isMemImage = false; |
93 |
| - if (stripos(strrev($src), strrev('.php')) === 0) { |
94 |
| - $isMemImage = true; |
95 |
| - } |
96 |
| - if (!$isMemImage) { |
97 |
| - $isMemImage = (filter_var($src, \FILTER_VALIDATE_URL) !== false); |
| 88 | + if (!is_null($image)) { |
| 89 | + $isMemImage = $image->getIsMemImage(); |
| 90 | + $extension = $image->getImageExtension(); |
| 91 | + } else { |
| 92 | + $isMemImage = false; |
98 | 93 | }
|
99 |
| - $extension = ''; |
100 | 94 | if ($isMemImage) {
|
101 |
| - $extension = $memoryImage->getImageExtension(); |
102 | 95 | $media['isMemImage'] = true;
|
103 |
| - $media['createfunction'] = $memoryImage->getImageCreateFunction(); |
104 |
| - $media['imagefunction'] = $memoryImage->getImageFunction(); |
105 |
| - } else { |
106 |
| - $imageType = exif_imagetype($src); |
107 |
| - if ($imageType === \IMAGETYPE_JPEG) { |
108 |
| - $extension = 'jpg'; |
109 |
| - } elseif ($imageType === \IMAGETYPE_GIF) { |
110 |
| - $extension = 'gif'; |
111 |
| - } elseif ($imageType === \IMAGETYPE_PNG) { |
112 |
| - $extension = 'png'; |
113 |
| - } elseif ($imageType === \IMAGETYPE_BMP) { |
114 |
| - $extension = 'bmp'; |
115 |
| - } elseif ($imageType === \IMAGETYPE_TIFF_II || $imageType === \IMAGETYPE_TIFF_MM) { |
116 |
| - $extension = 'tif'; |
117 |
| - } |
| 96 | + $media['createfunction'] = $image->getImageCreateFunction(); |
| 97 | + $media['imagefunction'] = $image->getImageFunction(); |
118 | 98 | }
|
119 |
| - |
120 | 99 | $folder = 'media';
|
121 | 100 | $file = $type . $cImg . '.' . strtolower($extension);
|
122 | 101 | } elseif ($type === 'oleObject') {
|
123 | 102 | $cObj++;
|
124 | 103 | $folder = 'embedding';
|
125 | 104 | $file = $type . $cObj . '.bin';
|
126 | 105 | }
|
127 |
| - |
128 | 106 | $media['source'] = $src;
|
129 | 107 | $media['target'] = "$folder/section_$file";
|
130 | 108 | $media['type'] = $type;
|
131 | 109 | $media['rID'] = $rID;
|
132 |
| - |
133 | 110 | self::$_sectionMedia[$key][$mediaId] = $media;
|
134 |
| - |
135 | 111 | if ($type === 'oleObject') {
|
136 | 112 | return array($rID, ++self::$_objectId);
|
137 | 113 | }
|
138 |
| - |
139 | 114 | return $rID;
|
| 115 | + } else { |
| 116 | + if ($type === 'oleObject') { |
| 117 | + $rID = self::$_sectionMedia[$key][$mediaId]['rID']; |
| 118 | + return array($rID, ++self::$_objectId); |
| 119 | + } |
| 120 | + return self::$_sectionMedia[$key][$mediaId]['rID']; |
140 | 121 | }
|
141 |
| - |
142 |
| - if ($type === 'oleObject') { |
143 |
| - $rID = self::$_sectionMedia[$key][$mediaId]['rID']; |
144 |
| - return array($rID, ++self::$_objectId); |
145 |
| - } |
146 |
| - return self::$_sectionMedia[$key][$mediaId]['rID']; |
147 | 122 | }
|
148 | 123 |
|
149 | 124 | /**
|
@@ -207,50 +182,42 @@ public static function countSectionMediaElements($key = null)
|
207 | 182 | *
|
208 | 183 | * @param int $headerCount
|
209 | 184 | * @param string $src
|
210 |
| - * @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage |
| 185 | + * @param \PhpOffice\PhpWord\Section\Image $image |
211 | 186 | * @return int
|
212 | 187 | */
|
213 |
| - public static function addHeaderMediaElement($headerCount, $src, MemoryImage $memoryImage = null) |
| 188 | + public static function addHeaderMediaElement($headerCount, $src, Image $image = null) |
214 | 189 | {
|
215 | 190 | $mediaId = md5($src);
|
216 | 191 | $key = 'header' . $headerCount;
|
217 |
| - |
218 | 192 | if (!array_key_exists($key, self::$_headerMedia)) {
|
219 | 193 | self::$_headerMedia[$key] = array();
|
220 | 194 | }
|
221 |
| - |
222 | 195 | if (!array_key_exists($mediaId, self::$_headerMedia[$key])) {
|
223 | 196 | $cImg = self::countHeaderMediaElements($key);
|
224 | 197 | $rID = $cImg + 1;
|
225 |
| - |
226 | 198 | $cImg++;
|
227 |
| - $inf = pathinfo($src); |
228 |
| - $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false; |
229 |
| - |
230 | 199 | $media = array();
|
| 200 | + if (!is_null($image)) { |
| 201 | + $isMemImage = $image->getIsMemImage(); |
| 202 | + $extension = $image->getImageExtension(); |
| 203 | + } else { |
| 204 | + $isMemImage = false; |
| 205 | + } |
231 | 206 | if ($isMemImage) {
|
232 |
| - $ext = $memoryImage->getImageExtension(); |
233 | 207 | $media['isMemImage'] = true;
|
234 |
| - $media['createfunction'] = $memoryImage->getImageCreateFunction(); |
235 |
| - $media['imagefunction'] = $memoryImage->getImageFunction(); |
236 |
| - } else { |
237 |
| - $ext = $inf['extension']; |
238 |
| - if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg |
239 |
| - $ext = 'jpg'; |
240 |
| - } |
| 208 | + $media['createfunction'] = $image->getImageCreateFunction(); |
| 209 | + $media['imagefunction'] = $image->getImageFunction(); |
241 | 210 | }
|
242 |
| - $file = 'image' . $cImg . '.' . strtolower($ext); |
243 |
| - |
| 211 | + $file = 'image' . $cImg . '.' . strtolower($extension); |
244 | 212 | $media['source'] = $src;
|
245 | 213 | $media['target'] = 'media/' . $key . '_' . $file;
|
246 | 214 | $media['type'] = 'image';
|
247 | 215 | $media['rID'] = $rID;
|
248 |
| - |
249 | 216 | self::$_headerMedia[$key][$mediaId] = $media;
|
250 |
| - |
251 | 217 | return $rID;
|
| 218 | + } else { |
| 219 | + return self::$_headerMedia[$key][$mediaId]['rID']; |
252 | 220 | }
|
253 |
| - return self::$_headerMedia[$key][$mediaId]['rID']; |
254 | 221 | }
|
255 | 222 |
|
256 | 223 | /**
|
@@ -279,50 +246,41 @@ public static function getHeaderMediaElements()
|
279 | 246 | *
|
280 | 247 | * @param int $footerCount
|
281 | 248 | * @param string $src
|
282 |
| - * @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage |
| 249 | + * @param \PhpOffice\PhpWord\Section\Image $image |
283 | 250 | * @return int
|
284 | 251 | */
|
285 |
| - public static function addFooterMediaElement($footerCount, $src, MemoryImage $memoryImage = null) |
| 252 | + public static function addFooterMediaElement($footerCount, $src, Image $image = null) |
286 | 253 | {
|
287 | 254 | $mediaId = md5($src);
|
288 | 255 | $key = 'footer' . $footerCount;
|
289 |
| - |
290 | 256 | if (!array_key_exists($key, self::$_footerMedia)) {
|
291 | 257 | self::$_footerMedia[$key] = array();
|
292 | 258 | }
|
293 |
| - |
294 | 259 | if (!array_key_exists($mediaId, self::$_footerMedia[$key])) {
|
295 | 260 | $cImg = self::countFooterMediaElements($key);
|
296 | 261 | $rID = $cImg + 1;
|
297 |
| - |
298 | 262 | $cImg++;
|
299 |
| - $inf = pathinfo($src); |
300 |
| - $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false; |
301 |
| - |
302 |
| - $media = array(); |
| 263 | + if (!is_null($image)) { |
| 264 | + $isMemImage = $image->getIsMemImage(); |
| 265 | + $extension = $image->getImageExtension(); |
| 266 | + } else { |
| 267 | + $isMemImage = false; |
| 268 | + } |
303 | 269 | if ($isMemImage) {
|
304 |
| - $ext = $memoryImage->getImageExtension(); |
305 | 270 | $media['isMemImage'] = true;
|
306 |
| - $media['createfunction'] = $memoryImage->getImageCreateFunction(); |
307 |
| - $media['imagefunction'] = $memoryImage->getImageFunction(); |
308 |
| - } else { |
309 |
| - $ext = $inf['extension']; |
310 |
| - if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg |
311 |
| - $ext = 'jpg'; |
312 |
| - } |
| 271 | + $media['createfunction'] = $image->getImageCreateFunction(); |
| 272 | + $media['imagefunction'] = $image->getImageFunction(); |
313 | 273 | }
|
314 |
| - $file = 'image' . $cImg . '.' . strtolower($ext); |
315 |
| - |
| 274 | + $file = 'image' . $cImg . '.' . strtolower($extension); |
316 | 275 | $media['source'] = $src;
|
317 | 276 | $media['target'] = 'media/' . $key . '_' . $file;
|
318 | 277 | $media['type'] = 'image';
|
319 | 278 | $media['rID'] = $rID;
|
320 |
| - |
321 | 279 | self::$_footerMedia[$key][$mediaId] = $media;
|
322 |
| - |
323 | 280 | return $rID;
|
| 281 | + } else { |
| 282 | + return self::$_footerMedia[$key][$mediaId]['rID']; |
324 | 283 | }
|
325 |
| - return self::$_footerMedia[$key][$mediaId]['rID']; |
326 | 284 | }
|
327 | 285 |
|
328 | 286 | /**
|
|
0 commit comments