PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/PhpWord/Media.php

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