123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php
- /**
- * PHPWord
- *
- * Copyright (c) 2011 PHPWord
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * @category PHPWord
- * @package PHPWord
- * @copyright Copyright (c) 010 PHPWord
- * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
- * @version Beta 0.6.3, 08.07.2011
- */
- /**
- * PHPWord_Media
- *
- * @category PHPWord
- * @package PHPWord
- * @copyright Copyright (c) 2011 PHPWord
- */
- class PHPWord_Media {
-
- /**
- * Section Media Elements
- *
- * @var array
- */
- private static $_sectionMedia = array('images'=>array(),
- 'embeddings'=>array(),
- 'links'=>array());
-
- /**
- * Header Media Elements
- *
- * @var array
- */
- private static $_headerMedia = array();
-
- /**
- * Footer Media Elements
- *
- * @var array
- */
- private static $_footerMedia = array();
-
- /**
- * ObjectID Counter
- *
- * @var int
- */
- private static $_objectId = 1325353440;
-
- public static function GBKToUTF8($str)
- {
- if (function_exists('mb_convert_encoding'))
- {
- $str = @mb_convert_encoding($str, 'UTF-8', 'GBK');
- }
- elseif (function_exists('iconv'))
- {
- $str = @iconv('GBK', 'UTF-8//IGNORE', $str);
- }
- else
- {
- return FALSE;
- }
- return $str;
- }
-
- public static function UTF8ToGBK($str)
- {
- if (function_exists('mb_convert_encoding'))
- {
- $str = @mb_convert_encoding($str, 'GBK', 'UTF-8');
- }
- elseif (function_exists('iconv'))
- {
- $str = @iconv('UTF-8', 'GBK//IGNORE', $str);
- }
- else
- {
- return FALSE;
- }
- return $str;
- }
-
- /**
- * Add new Section Media Element
- *
- * @param string $src
- * @param string $type
- *
- * @return mixed
- */
- public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null) {
- $mediaId = md5($src);
- $key = ($type == 'image') ? 'images' : 'embeddings';
-
- if(!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
- $cImg = self::countSectionMediaElements('images');
- $cObj = self::countSectionMediaElements('embeddings');
- $rID = self::countSectionMediaElements() + 7;
-
- $media = array();
-
- if($type == 'image') {
- $cImg++;
- $inf = pathinfo($src);
- $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false;
-
- if($isMemImage) {
- $ext = $memoryImage->getImageExtension();
- $media['isMemImage'] = true;
- $media['createfunction'] = $memoryImage->getImageCreateFunction();
- $media['imagefunction'] = $memoryImage->getImageFunction();
- } else {
- $ext = $inf['extension'];
- if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
- $ext = 'jpg';
- }
- }
-
- $folder = 'media';
- $file = $type.$cImg.'.'.strtolower($ext);
- } elseif($type == 'oleObject') {
- $cObj++;
- $folder = 'embedding';
- $file = $type.$cObj.'.bin';
- }
-
- $media['source'] = $src;
- $media['target'] = "$folder/section_$file";
- $media['type'] = $type;
- $media['rID'] = $rID;
-
- self::$_sectionMedia[$key][$mediaId] = $media;
-
- if($type == 'oleObject') {
- return array($rID, ++self::$_objectId);
- } else {
- return $rID;
- }
- } else {
- if($type == 'oleObject') {
- $rID = self::$_sectionMedia[$key][$mediaId]['rID'];
- return array($rID, ++self::$_objectId);
- } else {
- return self::$_sectionMedia[$key][$mediaId]['rID'];
- }
- }
- }
-
- /**
- * Add new Section Link Element
- *
- * @param string $linkSrc
- * @param string $linkName
- *
- * @return mixed
- */
- public static function addSectionLinkElement($linkSrc) {
- $rID = self::countSectionMediaElements() + 7;
-
- $link = array();
- $link['target'] = $linkSrc;
- $link['rID'] = $rID;
- $link['type'] = 'hyperlink';
-
- self::$_sectionMedia['links'][] = $link;
-
- return $rID;
- }
-
- /**
- * Get Section Media Elements
- *
- * @param string $key
- * @return array
- */
- public static function getSectionMediaElements($key = null) {
- if(!is_null($key)) {
- return self::$_sectionMedia[$key];
- } else {
- $arrImages = self::$_sectionMedia['images'];
- $arrObjects = self::$_sectionMedia['embeddings'];
- $arrLinks = self::$_sectionMedia['links'];
- return array_merge($arrImages, $arrObjects, $arrLinks);
- }
- }
-
- /**
- * Get Section Media Elements Count
- *
- * @param string $key
- * @return int
- */
- public static function countSectionMediaElements($key = null) {
- if(!is_null($key)) {
- return count(self::$_sectionMedia[$key]);
- } else {
- $cImages = count(self::$_sectionMedia['images']);
- $cObjects = count(self::$_sectionMedia['embeddings']);
- $cLinks = count(self::$_sectionMedia['links']);
- return ($cImages + $cObjects + $cLinks);
- }
- }
-
- /**
- * Add new Header Media Element
- *
- * @param int $headerCount
- * @param string $src
- * @return int
- */
- public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) {
- $mediaId = md5($src);
- $key = 'header'.$headerCount;
-
- if(!array_key_exists($key, self::$_headerMedia)) {
- self::$_headerMedia[$key] = array();
- }
-
- if(!array_key_exists($mediaId, self::$_headerMedia[$key])) {
- $cImg = self::countHeaderMediaElements($key);
- $rID = $cImg + 1;
-
- $cImg++;
- $inf = pathinfo($src);
- $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
-
- $media = array();
- if($isMemImage) {
- $ext = $memoryImage->getImageExtension();
- $media['isMemImage'] = true;
- $media['createfunction'] = $memoryImage->getImageCreateFunction();
- $media['imagefunction'] = $memoryImage->getImageFunction();
- } else {
- $ext = $inf['extension'];
- if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
- $ext = 'jpg';
- }
- }
- $file = 'image'.$cImg.'.'.strtolower($ext);
-
- $media['source'] = $src;
- $media['target'] = 'media/'.$key.'_'.$file;
- $media['type'] = 'image';
- $media['rID'] = $rID;
-
- self::$_headerMedia[$key][$mediaId] = $media;
-
- return $rID;
- } else {
- return self::$_headerMedia[$key][$mediaId]['rID'];
- }
- }
-
- /**
- * Get Header Media Elements Count
- *
- * @param string $key
- * @return int
- */
- public static function countHeaderMediaElements($key) {
- return count(self::$_headerMedia[$key]);
- }
-
- /**
- * Get Header Media Elements
- *
- * @return int
- */
- public static function getHeaderMediaElements() {
- return self::$_headerMedia;
- }
-
- /**
- * Add new Footer Media Element
- *
- * @param int $footerCount
- * @param string $src
- * @return int
- */
- public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) {
- $mediaId = md5($src);
- $key = 'footer'.$footerCount;
-
- if(!array_key_exists($key, self::$_footerMedia)) {
- self::$_footerMedia[$key] = array();
- }
-
- if(!array_key_exists($mediaId, self::$_footerMedia[$key])) {
- $cImg = self::countFooterMediaElements($key);
- $rID = $cImg + 1;
-
- $cImg++;
- $inf = pathinfo($src);
- $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
-
- $media = array();
- if($isMemImage) {
- $ext = $memoryImage->getImageExtension();
- $media['isMemImage'] = true;
- $media['createfunction'] = $memoryImage->getImageCreateFunction();
- $media['imagefunction'] = $memoryImage->getImageFunction();
- } else {
- $ext = $inf['extension'];
- if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
- $ext = 'jpg';
- }
- }
- $file = 'image'.$cImg.'.'.strtolower($ext);
-
- $media['source'] = $src;
- $media['target'] = 'media/'.$key.'_'.$file;
- $media['type'] = 'image';
- $media['rID'] = $rID;
-
- self::$_footerMedia[$key][$mediaId] = $media;
-
- return $rID;
- } else {
- return self::$_footerMedia[$key][$mediaId]['rID'];
- }
- }
-
- /**
- * Get Footer Media Elements Count
- *
- * @param string $key
- * @return int
- */
- public static function countFooterMediaElements($key) {
- return count(self::$_footerMedia[$key]);
- }
-
- /**
- * Get Footer Media Elements
- *
- * @return int
- */
- public static function getFooterMediaElements() {
- return self::$_footerMedia;
- }
- }
- ?>
|