Media.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. public static function GBKToUTF8($str)
  62. {
  63. if (function_exists('mb_convert_encoding'))
  64. {
  65. $str = @mb_convert_encoding($str, 'UTF-8', 'GBK');
  66. }
  67. elseif (function_exists('iconv'))
  68. {
  69. $str = @iconv('GBK', 'UTF-8//IGNORE', $str);
  70. }
  71. else
  72. {
  73. return FALSE;
  74. }
  75. return $str;
  76. }
  77. public static function UTF8ToGBK($str)
  78. {
  79. if (function_exists('mb_convert_encoding'))
  80. {
  81. $str = @mb_convert_encoding($str, 'GBK', 'UTF-8');
  82. }
  83. elseif (function_exists('iconv'))
  84. {
  85. $str = @iconv('UTF-8', 'GBK//IGNORE', $str);
  86. }
  87. else
  88. {
  89. return FALSE;
  90. }
  91. return $str;
  92. }
  93. /**
  94. * Add new Section Media Element
  95. *
  96. * @param string $src
  97. * @param string $type
  98. *
  99. * @return mixed
  100. */
  101. public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null) {
  102. $mediaId = md5($src);
  103. $key = ($type == 'image') ? 'images' : 'embeddings';
  104. if(!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
  105. $cImg = self::countSectionMediaElements('images');
  106. $cObj = self::countSectionMediaElements('embeddings');
  107. $rID = self::countSectionMediaElements() + 7;
  108. $media = array();
  109. if($type == 'image') {
  110. $cImg++;
  111. $inf = pathinfo($src);
  112. $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false;
  113. if($isMemImage) {
  114. $ext = $memoryImage->getImageExtension();
  115. $media['isMemImage'] = true;
  116. $media['createfunction'] = $memoryImage->getImageCreateFunction();
  117. $media['imagefunction'] = $memoryImage->getImageFunction();
  118. } else {
  119. $ext = $inf['extension'];
  120. if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
  121. $ext = 'jpg';
  122. }
  123. }
  124. $folder = 'media';
  125. $file = $type.$cImg.'.'.strtolower($ext);
  126. } elseif($type == 'oleObject') {
  127. $cObj++;
  128. $folder = 'embedding';
  129. $file = $type.$cObj.'.bin';
  130. }
  131. $media['source'] = $src;
  132. $media['target'] = "$folder/section_$file";
  133. $media['type'] = $type;
  134. $media['rID'] = $rID;
  135. self::$_sectionMedia[$key][$mediaId] = $media;
  136. if($type == 'oleObject') {
  137. return array($rID, ++self::$_objectId);
  138. } else {
  139. return $rID;
  140. }
  141. } else {
  142. if($type == 'oleObject') {
  143. $rID = self::$_sectionMedia[$key][$mediaId]['rID'];
  144. return array($rID, ++self::$_objectId);
  145. } else {
  146. return self::$_sectionMedia[$key][$mediaId]['rID'];
  147. }
  148. }
  149. }
  150. /**
  151. * Add new Section Link Element
  152. *
  153. * @param string $linkSrc
  154. * @param string $linkName
  155. *
  156. * @return mixed
  157. */
  158. public static function addSectionLinkElement($linkSrc) {
  159. $rID = self::countSectionMediaElements() + 7;
  160. $link = array();
  161. $link['target'] = $linkSrc;
  162. $link['rID'] = $rID;
  163. $link['type'] = 'hyperlink';
  164. self::$_sectionMedia['links'][] = $link;
  165. return $rID;
  166. }
  167. /**
  168. * Get Section Media Elements
  169. *
  170. * @param string $key
  171. * @return array
  172. */
  173. public static function getSectionMediaElements($key = null) {
  174. if(!is_null($key)) {
  175. return self::$_sectionMedia[$key];
  176. } else {
  177. $arrImages = self::$_sectionMedia['images'];
  178. $arrObjects = self::$_sectionMedia['embeddings'];
  179. $arrLinks = self::$_sectionMedia['links'];
  180. return array_merge($arrImages, $arrObjects, $arrLinks);
  181. }
  182. }
  183. /**
  184. * Get Section Media Elements Count
  185. *
  186. * @param string $key
  187. * @return int
  188. */
  189. public static function countSectionMediaElements($key = null) {
  190. if(!is_null($key)) {
  191. return count(self::$_sectionMedia[$key]);
  192. } else {
  193. $cImages = count(self::$_sectionMedia['images']);
  194. $cObjects = count(self::$_sectionMedia['embeddings']);
  195. $cLinks = count(self::$_sectionMedia['links']);
  196. return ($cImages + $cObjects + $cLinks);
  197. }
  198. }
  199. /**
  200. * Add new Header Media Element
  201. *
  202. * @param int $headerCount
  203. * @param string $src
  204. * @return int
  205. */
  206. public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) {
  207. $mediaId = md5($src);
  208. $key = 'header'.$headerCount;
  209. if(!array_key_exists($key, self::$_headerMedia)) {
  210. self::$_headerMedia[$key] = array();
  211. }
  212. if(!array_key_exists($mediaId, self::$_headerMedia[$key])) {
  213. $cImg = self::countHeaderMediaElements($key);
  214. $rID = $cImg + 1;
  215. $cImg++;
  216. $inf = pathinfo($src);
  217. $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
  218. $media = array();
  219. if($isMemImage) {
  220. $ext = $memoryImage->getImageExtension();
  221. $media['isMemImage'] = true;
  222. $media['createfunction'] = $memoryImage->getImageCreateFunction();
  223. $media['imagefunction'] = $memoryImage->getImageFunction();
  224. } else {
  225. $ext = $inf['extension'];
  226. if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
  227. $ext = 'jpg';
  228. }
  229. }
  230. $file = 'image'.$cImg.'.'.strtolower($ext);
  231. $media['source'] = $src;
  232. $media['target'] = 'media/'.$key.'_'.$file;
  233. $media['type'] = 'image';
  234. $media['rID'] = $rID;
  235. self::$_headerMedia[$key][$mediaId] = $media;
  236. return $rID;
  237. } else {
  238. return self::$_headerMedia[$key][$mediaId]['rID'];
  239. }
  240. }
  241. /**
  242. * Get Header Media Elements Count
  243. *
  244. * @param string $key
  245. * @return int
  246. */
  247. public static function countHeaderMediaElements($key) {
  248. return count(self::$_headerMedia[$key]);
  249. }
  250. /**
  251. * Get Header Media Elements
  252. *
  253. * @return int
  254. */
  255. public static function getHeaderMediaElements() {
  256. return self::$_headerMedia;
  257. }
  258. /**
  259. * Add new Footer Media Element
  260. *
  261. * @param int $footerCount
  262. * @param string $src
  263. * @return int
  264. */
  265. public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) {
  266. $mediaId = md5($src);
  267. $key = 'footer'.$footerCount;
  268. if(!array_key_exists($key, self::$_footerMedia)) {
  269. self::$_footerMedia[$key] = array();
  270. }
  271. if(!array_key_exists($mediaId, self::$_footerMedia[$key])) {
  272. $cImg = self::countFooterMediaElements($key);
  273. $rID = $cImg + 1;
  274. $cImg++;
  275. $inf = pathinfo($src);
  276. $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
  277. $media = array();
  278. if($isMemImage) {
  279. $ext = $memoryImage->getImageExtension();
  280. $media['isMemImage'] = true;
  281. $media['createfunction'] = $memoryImage->getImageCreateFunction();
  282. $media['imagefunction'] = $memoryImage->getImageFunction();
  283. } else {
  284. $ext = $inf['extension'];
  285. if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
  286. $ext = 'jpg';
  287. }
  288. }
  289. $file = 'image'.$cImg.'.'.strtolower($ext);
  290. $media['source'] = $src;
  291. $media['target'] = 'media/'.$key.'_'.$file;
  292. $media['type'] = 'image';
  293. $media['rID'] = $rID;
  294. self::$_footerMedia[$key][$mediaId] = $media;
  295. return $rID;
  296. } else {
  297. return self::$_footerMedia[$key][$mediaId]['rID'];
  298. }
  299. }
  300. /**
  301. * Get Footer Media Elements Count
  302. *
  303. * @param string $key
  304. * @return int
  305. */
  306. public static function countFooterMediaElements($key) {
  307. return count(self::$_footerMedia[$key]);
  308. }
  309. /**
  310. * Get Footer Media Elements
  311. *
  312. * @return int
  313. */
  314. public static function getFooterMediaElements() {
  315. return self::$_footerMedia;
  316. }
  317. }
  318. ?>