Media.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_Media
  29. *
  30. * @category PHPWord
  31. * @package PHPWord
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_Media {
  35. /**
  36. * Section Media Elements
  37. *
  38. * @var array
  39. */
  40. private static $_sectionMedia = array('images'=>array(),
  41. 'embeddings'=>array(),
  42. 'links'=>array());
  43. /**
  44. * Header Media Elements
  45. *
  46. * @var array
  47. */
  48. private static $_headerMedia = array();
  49. /**
  50. * Footer Media Elements
  51. *
  52. * @var array
  53. */
  54. private static $_footerMedia = array();
  55. /**
  56. * ObjectID Counter
  57. *
  58. * @var int
  59. */
  60. private static $_objectId = 1325353440;
  61. /**
  62. * Add new Section Media Element
  63. *
  64. * @param string $src
  65. * @param string $type
  66. *
  67. * @return mixed
  68. */
  69. public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null) {
  70. $mediaId = md5($src);
  71. $key = ($type == 'image') ? 'images' : 'embeddings';
  72. if(!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
  73. $cImg = self::countSectionMediaElements('images');
  74. $cObj = self::countSectionMediaElements('embeddings');
  75. $rID = self::countSectionMediaElements() + 7;
  76. $media = array();
  77. if($type == 'image') {
  78. $cImg++;
  79. $inf = pathinfo($src);
  80. $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false;
  81. if($isMemImage) {
  82. $ext = $memoryImage->getImageExtension();
  83. $media['isMemImage'] = true;
  84. $media['createfunction'] = $memoryImage->getImageCreateFunction();
  85. $media['imagefunction'] = $memoryImage->getImageFunction();
  86. } else {
  87. $ext = $inf['extension'];
  88. if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
  89. $ext = 'jpg';
  90. }
  91. }
  92. $folder = 'media';
  93. $file = $type.$cImg.'.'.strtolower($ext);
  94. } elseif($type == 'oleObject') {
  95. $cObj++;
  96. $folder = 'embedding';
  97. $file = $type.$cObj.'.bin';
  98. }
  99. $media['source'] = $src;
  100. $media['target'] = "$folder/section_$file";
  101. $media['type'] = $type;
  102. $media['rID'] = $rID;
  103. self::$_sectionMedia[$key][$mediaId] = $media;
  104. if($type == 'oleObject') {
  105. return array($rID, ++self::$_objectId);
  106. } else {
  107. return $rID;
  108. }
  109. } else {
  110. if($type == 'oleObject') {
  111. $rID = self::$_sectionMedia[$key][$mediaId]['rID'];
  112. return array($rID, ++self::$_objectId);
  113. } else {
  114. return self::$_sectionMedia[$key][$mediaId]['rID'];
  115. }
  116. }
  117. }
  118. /**
  119. * Add new Section Link Element
  120. *
  121. * @param string $linkSrc
  122. * @param string $linkName
  123. *
  124. * @return mixed
  125. */
  126. public static function addSectionLinkElement($linkSrc) {
  127. $rID = self::countSectionMediaElements() + 7;
  128. $link = array();
  129. $link['target'] = $linkSrc;
  130. $link['rID'] = $rID;
  131. $link['type'] = 'hyperlink';
  132. self::$_sectionMedia['links'][] = $link;
  133. return $rID;
  134. }
  135. /**
  136. * Get Section Media Elements
  137. *
  138. * @param string $key
  139. * @return array
  140. */
  141. public static function getSectionMediaElements($key = null) {
  142. if(!is_null($key)) {
  143. return self::$_sectionMedia[$key];
  144. } else {
  145. $arrImages = self::$_sectionMedia['images'];
  146. $arrObjects = self::$_sectionMedia['embeddings'];
  147. $arrLinks = self::$_sectionMedia['links'];
  148. return array_merge($arrImages, $arrObjects, $arrLinks);
  149. }
  150. }
  151. /**
  152. * Get Section Media Elements Count
  153. *
  154. * @param string $key
  155. * @return int
  156. */
  157. public static function countSectionMediaElements($key = null) {
  158. if(!is_null($key)) {
  159. return count(self::$_sectionMedia[$key]);
  160. } else {
  161. $cImages = count(self::$_sectionMedia['images']);
  162. $cObjects = count(self::$_sectionMedia['embeddings']);
  163. $cLinks = count(self::$_sectionMedia['links']);
  164. return ($cImages + $cObjects + $cLinks);
  165. }
  166. }
  167. /**
  168. * Add new Header Media Element
  169. *
  170. * @param int $headerCount
  171. * @param string $src
  172. * @return int
  173. */
  174. public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) {
  175. $mediaId = md5($src);
  176. $key = 'header'.$headerCount;
  177. if(!array_key_exists($key, self::$_headerMedia)) {
  178. self::$_headerMedia[$key] = array();
  179. }
  180. if(!array_key_exists($mediaId, self::$_headerMedia[$key])) {
  181. $cImg = self::countHeaderMediaElements($key);
  182. $rID = $cImg + 1;
  183. $cImg++;
  184. $inf = pathinfo($src);
  185. $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
  186. $media = array();
  187. if($isMemImage) {
  188. $ext = $memoryImage->getImageExtension();
  189. $media['isMemImage'] = true;
  190. $media['createfunction'] = $memoryImage->getImageCreateFunction();
  191. $media['imagefunction'] = $memoryImage->getImageFunction();
  192. } else {
  193. $ext = $inf['extension'];
  194. if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
  195. $ext = 'jpg';
  196. }
  197. }
  198. $file = 'image'.$cImg.'.'.strtolower($ext);
  199. $media['source'] = $src;
  200. $media['target'] = 'media/'.$key.'_'.$file;
  201. $media['type'] = 'image';
  202. $media['rID'] = $rID;
  203. self::$_headerMedia[$key][$mediaId] = $media;
  204. return $rID;
  205. } else {
  206. return self::$_headerMedia[$key][$mediaId]['rID'];
  207. }
  208. }
  209. /**
  210. * Get Header Media Elements Count
  211. *
  212. * @param string $key
  213. * @return int
  214. */
  215. public static function countHeaderMediaElements($key) {
  216. return count(self::$_headerMedia[$key]);
  217. }
  218. /**
  219. * Get Header Media Elements
  220. *
  221. * @return int
  222. */
  223. public static function getHeaderMediaElements() {
  224. return self::$_headerMedia;
  225. }
  226. /**
  227. * Add new Footer Media Element
  228. *
  229. * @param int $footerCount
  230. * @param string $src
  231. * @return int
  232. */
  233. public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) {
  234. $mediaId = md5($src);
  235. $key = 'footer'.$footerCount;
  236. if(!array_key_exists($key, self::$_footerMedia)) {
  237. self::$_footerMedia[$key] = array();
  238. }
  239. if(!array_key_exists($mediaId, self::$_footerMedia[$key])) {
  240. $cImg = self::countFooterMediaElements($key);
  241. $rID = $cImg + 1;
  242. $cImg++;
  243. $inf = pathinfo($src);
  244. $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
  245. $media = array();
  246. if($isMemImage) {
  247. $ext = $memoryImage->getImageExtension();
  248. $media['isMemImage'] = true;
  249. $media['createfunction'] = $memoryImage->getImageCreateFunction();
  250. $media['imagefunction'] = $memoryImage->getImageFunction();
  251. } else {
  252. $ext = $inf['extension'];
  253. if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
  254. $ext = 'jpg';
  255. }
  256. }
  257. $file = 'image'.$cImg.'.'.strtolower($ext);
  258. $media['source'] = $src;
  259. $media['target'] = 'media/'.$key.'_'.$file;
  260. $media['type'] = 'image';
  261. $media['rID'] = $rID;
  262. self::$_footerMedia[$key][$mediaId] = $media;
  263. return $rID;
  264. } else {
  265. return self::$_footerMedia[$key][$mediaId]['rID'];
  266. }
  267. }
  268. /**
  269. * Get Footer Media Elements Count
  270. *
  271. * @param string $key
  272. * @return int
  273. */
  274. public static function countFooterMediaElements($key) {
  275. return count(self::$_footerMedia[$key]);
  276. }
  277. /**
  278. * Get Footer Media Elements
  279. *
  280. * @return int
  281. */
  282. public static function getFooterMediaElements() {
  283. return self::$_footerMedia;
  284. }
  285. }
  286. ?>