Media.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 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. * @since 0.10.0
  35. * @since 0.9.2
  36. *
  37. * @param string $container section|headerx|footerx|footnote|endnote
  38. * @param string $mediaType image|object|link
  39. * @param string $source
  40. * @param \PhpOffice\PhpWord\Element\Image $image
  41. *
  42. * @throws \PhpOffice\PhpWord\Exception\Exception
  43. *
  44. * @return int
  45. */
  46. public static function addElement($container, $mediaType, $source, Image $image = null)
  47. {
  48. // Assign unique media Id and initiate media container if none exists
  49. $mediaId = md5($container . $source);
  50. if (!isset(self::$elements[$container])) {
  51. self::$elements[$container] = array();
  52. }
  53. // Add media if not exists or point to existing media
  54. if (!isset(self::$elements[$container][$mediaId])) {
  55. $mediaCount = self::countElements($container);
  56. $mediaTypeCount = self::countElements($container, $mediaType);
  57. $mediaTypeCount++;
  58. $rId = ++$mediaCount;
  59. $target = null;
  60. $mediaData = array('mediaIndex' => $mediaTypeCount);
  61. switch ($mediaType) {
  62. // Images
  63. case 'image':
  64. if (is_null($image)) {
  65. throw new Exception('Image object not assigned.');
  66. }
  67. $isMemImage = $image->isMemImage();
  68. $extension = $image->getImageExtension();
  69. $mediaData['imageExtension'] = $extension;
  70. $mediaData['imageType'] = $image->getImageType();
  71. if ($isMemImage) {
  72. $mediaData['isMemImage'] = true;
  73. $mediaData['createFunction'] = $image->getImageCreateFunction();
  74. $mediaData['imageFunction'] = $image->getImageFunction();
  75. }
  76. $target = "{$container}_image{$mediaTypeCount}.{$extension}";
  77. $image->setTarget($target);
  78. $image->setMediaIndex($mediaTypeCount);
  79. break;
  80. // Objects
  81. case 'object':
  82. $target = "{$container}_oleObject{$mediaTypeCount}.bin";
  83. break;
  84. // Links
  85. case 'link':
  86. $target = $source;
  87. break;
  88. }
  89. $mediaData['source'] = $source;
  90. $mediaData['target'] = $target;
  91. $mediaData['type'] = $mediaType;
  92. $mediaData['rID'] = $rId;
  93. self::$elements[$container][$mediaId] = $mediaData;
  94. return $rId;
  95. }
  96. $mediaData = self::$elements[$container][$mediaId];
  97. if (!is_null($image)) {
  98. $image->setTarget($mediaData['target']);
  99. $image->setMediaIndex($mediaData['mediaIndex']);
  100. }
  101. return $mediaData['rID'];
  102. }
  103. /**
  104. * Get media elements count
  105. *
  106. * @param string $container section|headerx|footerx|footnote|endnote
  107. * @param string $mediaType image|object|link
  108. * @return int
  109. * @since 0.10.0
  110. */
  111. public static function countElements($container, $mediaType = null)
  112. {
  113. $mediaCount = 0;
  114. if (isset(self::$elements[$container])) {
  115. foreach (self::$elements[$container] as $mediaData) {
  116. if (!is_null($mediaType)) {
  117. if ($mediaType == $mediaData['type']) {
  118. $mediaCount++;
  119. }
  120. } else {
  121. $mediaCount++;
  122. }
  123. }
  124. }
  125. return $mediaCount;
  126. }
  127. /**
  128. * Get media elements
  129. *
  130. * @param string $container section|headerx|footerx|footnote|endnote
  131. * @param string $type image|object|link
  132. * @return array
  133. * @since 0.10.0
  134. */
  135. public static function getElements($container, $type = null)
  136. {
  137. $elements = array();
  138. // If header/footer, search for headerx and footerx where x is number
  139. if ($container == 'header' || $container == 'footer') {
  140. foreach (self::$elements as $key => $val) {
  141. if (substr($key, 0, 6) == $container) {
  142. $elements[$key] = $val;
  143. }
  144. }
  145. return $elements;
  146. }
  147. if (!isset(self::$elements[$container])) {
  148. return $elements;
  149. }
  150. return self::getElementsByType($container, $type);
  151. }
  152. /**
  153. * Get elements by media type
  154. *
  155. * @param string $container section|footnote|endnote
  156. * @param string $type image|object|link
  157. * @return array
  158. * @since 0.11.0 Splitted from `getElements` to reduce complexity
  159. */
  160. private static function getElementsByType($container, $type = null)
  161. {
  162. $elements = array();
  163. foreach (self::$elements[$container] as $key => $data) {
  164. if ($type !== null) {
  165. if ($type == $data['type']) {
  166. $elements[$key] = $data;
  167. }
  168. } else {
  169. $elements[$key] = $data;
  170. }
  171. }
  172. return $elements;
  173. }
  174. /**
  175. * Reset media elements
  176. */
  177. public static function resetElements()
  178. {
  179. self::$elements = array();
  180. }
  181. /**
  182. * Add new Section Media Element
  183. *
  184. * @deprecated 0.10.0
  185. *
  186. * @param string $src
  187. * @param string $type
  188. * @param \PhpOffice\PhpWord\Element\Image $image
  189. *
  190. * @return int
  191. *
  192. * @codeCoverageIgnore
  193. */
  194. public static function addSectionMediaElement($src, $type, Image $image = null)
  195. {
  196. return self::addElement('section', $type, $src, $image);
  197. }
  198. /**
  199. * Add new Section Link Element
  200. *
  201. * @deprecated 0.10.0
  202. *
  203. * @param string $linkSrc
  204. *
  205. * @return int
  206. *
  207. * @codeCoverageIgnore
  208. */
  209. public static function addSectionLinkElement($linkSrc)
  210. {
  211. return self::addElement('section', 'link', $linkSrc);
  212. }
  213. /**
  214. * Get Section Media Elements
  215. *
  216. * @deprecated 0.10.0
  217. *
  218. * @param string $key
  219. *
  220. * @return array
  221. *
  222. * @codeCoverageIgnore
  223. */
  224. public static function getSectionMediaElements($key = null)
  225. {
  226. return self::getElements('section', $key);
  227. }
  228. /**
  229. * Get Section Media Elements Count
  230. *
  231. * @deprecated 0.10.0
  232. *
  233. * @param string $key
  234. *
  235. * @return int
  236. *
  237. * @codeCoverageIgnore
  238. */
  239. public static function countSectionMediaElements($key = null)
  240. {
  241. return self::countElements('section', $key);
  242. }
  243. /**
  244. * Add new Header Media Element
  245. *
  246. * @deprecated 0.10.0
  247. *
  248. * @param int $headerCount
  249. * @param string $src
  250. * @param \PhpOffice\PhpWord\Element\Image $image
  251. *
  252. * @return int
  253. *
  254. * @codeCoverageIgnore
  255. */
  256. public static function addHeaderMediaElement($headerCount, $src, Image $image = null)
  257. {
  258. return self::addElement("header{$headerCount}", 'image', $src, $image);
  259. }
  260. /**
  261. * Get Header Media Elements Count
  262. *
  263. * @deprecated 0.10.0
  264. *
  265. * @param string $key
  266. *
  267. * @return int
  268. *
  269. * @codeCoverageIgnore
  270. */
  271. public static function countHeaderMediaElements($key)
  272. {
  273. return self::countElements($key);
  274. }
  275. /**
  276. * Get Header Media Elements
  277. *
  278. * @deprecated 0.10.0
  279. *
  280. * @return array
  281. *
  282. * @codeCoverageIgnore
  283. */
  284. public static function getHeaderMediaElements()
  285. {
  286. return self::getElements('header');
  287. }
  288. /**
  289. * Add new Footer Media Element
  290. *
  291. * @deprecated 0.10.0
  292. *
  293. * @param int $footerCount
  294. * @param string $src
  295. * @param \PhpOffice\PhpWord\Element\Image $image
  296. *
  297. * @return int
  298. *
  299. * @codeCoverageIgnore
  300. */
  301. public static function addFooterMediaElement($footerCount, $src, Image $image = null)
  302. {
  303. return self::addElement("footer{$footerCount}", 'image', $src, $image);
  304. }
  305. /**
  306. * Get Footer Media Elements Count
  307. *
  308. * @deprecated 0.10.0
  309. *
  310. * @param string $key
  311. *
  312. * @return int
  313. *
  314. * @codeCoverageIgnore
  315. */
  316. public static function countFooterMediaElements($key)
  317. {
  318. return self::countElements($key);
  319. }
  320. /**
  321. * Get Footer Media Elements
  322. *
  323. * @deprecated 0.10.0
  324. *
  325. * @return array
  326. *
  327. * @codeCoverageIgnore
  328. */
  329. public static function getFooterMediaElements()
  330. {
  331. return self::getElements('footer');
  332. }
  333. }