Overview

Packages

  • PHP
  • PHPWord

Classes

  • PHPWord
  • PHPWord_Autoloader
  • PHPWord_DocumentProperties
  • PHPWord_HashTable
  • PHPWord_IOFactory
  • PHPWord_Media
  • PHPWord_Section
  • PHPWord_Section_Footer
  • PHPWord_Section_Footer_PreserveText
  • PHPWord_Section_Header
  • PHPWord_Section_Image
  • PHPWord_Section_Link
  • PHPWord_Section_ListItem
  • PHPWord_Section_MemoryImage
  • PHPWord_Section_Object
  • PHPWord_Section_PageBreak
  • PHPWord_Section_Settings
  • PHPWord_Section_Table
  • PHPWord_Section_Table_Cell
  • PHPWord_Section_Table_Row
  • PHPWord_Section_Text
  • PHPWord_Section_TextBreak
  • PHPWord_Section_TextRun
  • PHPWord_Section_Title
  • PHPWord_Shared_Drawing
  • PHPWord_Shared_File
  • PHPWord_Shared_Font
  • PHPWord_Shared_String
  • PHPWord_Shared_XMLWriter
  • PHPWord_Shared_ZipStreamWrapper
  • PHPWord_Style
  • PHPWord_Style_Cell
  • PHPWord_Style_Font
  • PHPWord_Style_Image
  • PHPWord_Style_ListItem
  • PHPWord_Style_Paragraph
  • PHPWord_Style_Row
  • PHPWord_Style_Tab
  • PHPWord_Style_Table
  • PHPWord_Style_TableFull
  • PHPWord_Style_Tabs
  • PHPWord_Style_TOC
  • PHPWord_Template
  • PHPWord_TOC
  • PHPWord_Writer_ODText
  • PHPWord_Writer_ODText_Content
  • PHPWord_Writer_ODText_Manifest
  • PHPWord_Writer_ODText_Meta
  • PHPWord_Writer_ODText_Mimetype
  • PHPWord_Writer_ODText_Styles
  • PHPWord_Writer_ODText_WriterPart
  • PHPWord_Writer_RTF
  • PHPWord_Writer_Word2007
  • PHPWord_Writer_Word2007_Base
  • PHPWord_Writer_Word2007_ContentTypes
  • PHPWord_Writer_Word2007_DocProps
  • PHPWord_Writer_Word2007_Document
  • PHPWord_Writer_Word2007_DocumentRels
  • PHPWord_Writer_Word2007_Footer
  • PHPWord_Writer_Word2007_Header
  • PHPWord_Writer_Word2007_Rels
  • PHPWord_Writer_Word2007_Styles
  • PHPWord_Writer_Word2007_WriterPart

Interfaces

  • PHPWord_Writer_IWriter

Exceptions

  • PHPWord_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * PHPWord
  4:  *
  5:  * Copyright (c) 2013 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) 2013 PHPWord
 24:  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 25:  * @version    0.7.0
 26:  */
 27: 
 28: /**
 29:  * Class PHPWord_Media
 30:  */
 31: class PHPWord_Media
 32: {
 33: 
 34:     /**
 35:      * Section Media Elements
 36:      *
 37:      * @var array
 38:      */
 39:     private static $_sectionMedia = array('images' => array(),
 40:         'embeddings' => array(),
 41:         'links' => array());
 42: 
 43:     /**
 44:      * Header Media Elements
 45:      *
 46:      * @var array
 47:      */
 48:     private static $_headerMedia = array();
 49: 
 50:     /**
 51:      * Footer Media Elements
 52:      *
 53:      * @var array
 54:      */
 55:     private static $_footerMedia = array();
 56: 
 57:     /**
 58:      * ObjectID Counter
 59:      *
 60:      * @var int
 61:      */
 62:     private static $_objectId = 1325353440;
 63: 
 64: 
 65:     /**
 66:      * Add new Section Media Element
 67:      *
 68:      * @param string $src
 69:      * @param string $type
 70:      * @return mixed
 71:      */
 72:     public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null)
 73:     {
 74:         $mediaId = md5($src);
 75:         $key = ($type == 'image') ? 'images' : 'embeddings';
 76: 
 77:         if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
 78:             $cImg = self::countSectionMediaElements('images');
 79:             $cObj = self::countSectionMediaElements('embeddings');
 80:             $rID = self::countSectionMediaElements() + 7;
 81: 
 82:             $media = array();
 83: 
 84:             if ($type == 'image') {
 85:                 $cImg++;
 86:                 $inf = pathinfo($src);
 87:                 $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false;
 88: 
 89:                 if ($isMemImage) {
 90:                     $ext = $memoryImage->getImageExtension();
 91:                     $media['isMemImage'] = true;
 92:                     $media['createfunction'] = $memoryImage->getImageCreateFunction();
 93:                     $media['imagefunction'] = $memoryImage->getImageFunction();
 94:                 } else {
 95:                     $ext = $inf['extension'];
 96:                     if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
 97:                         $ext = 'jpg';
 98:                     }
 99:                 }
100: 
101:                 $folder = 'media';
102:                 $file = $type . $cImg . '.' . strtolower($ext);
103:             } elseif ($type == 'oleObject') {
104:                 $cObj++;
105:                 $folder = 'embedding';
106:                 $file = $type . $cObj . '.bin';
107:             }
108: 
109:             $media['source'] = $src;
110:             $media['target'] = "$folder/section_$file";
111:             $media['type'] = $type;
112:             $media['rID'] = $rID;
113: 
114:             self::$_sectionMedia[$key][$mediaId] = $media;
115: 
116:             if ($type == 'oleObject') {
117:                 return array($rID, ++self::$_objectId);
118:             } else {
119:                 return $rID;
120:             }
121:         } else {
122:             if ($type == 'oleObject') {
123:                 $rID = self::$_sectionMedia[$key][$mediaId]['rID'];
124:                 return array($rID, ++self::$_objectId);
125:             } else {
126:                 return self::$_sectionMedia[$key][$mediaId]['rID'];
127:             }
128:         }
129:     }
130: 
131:     /**
132:      * Add new Section Link Element
133:      *
134:      * @param string $linkSrc
135:      * @param string $linkName
136:      *
137:      * @return mixed
138:      */
139:     public static function addSectionLinkElement($linkSrc)
140:     {
141:         $rID = self::countSectionMediaElements() + 7;
142: 
143:         $link = array();
144:         $link['target'] = $linkSrc;
145:         $link['rID'] = $rID;
146:         $link['type'] = 'hyperlink';
147: 
148:         self::$_sectionMedia['links'][] = $link;
149: 
150:         return $rID;
151:     }
152: 
153:     /**
154:      * Get Section Media Elements
155:      *
156:      * @param string $key
157:      * @return array
158:      */
159:     public static function getSectionMediaElements($key = null)
160:     {
161:         if (!is_null($key)) {
162:             return self::$_sectionMedia[$key];
163:         } else {
164:             $arrImages = self::$_sectionMedia['images'];
165:             $arrObjects = self::$_sectionMedia['embeddings'];
166:             $arrLinks = self::$_sectionMedia['links'];
167:             return array_merge($arrImages, $arrObjects, $arrLinks);
168:         }
169:     }
170: 
171:     /**
172:      * Get Section Media Elements Count
173:      *
174:      * @param string $key
175:      * @return int
176:      */
177:     public static function countSectionMediaElements($key = null)
178:     {
179:         if (!is_null($key)) {
180:             return count(self::$_sectionMedia[$key]);
181:         } else {
182:             $cImages = count(self::$_sectionMedia['images']);
183:             $cObjects = count(self::$_sectionMedia['embeddings']);
184:             $cLinks = count(self::$_sectionMedia['links']);
185:             return ($cImages + $cObjects + $cLinks);
186:         }
187:     }
188: 
189:     /**
190:      * Add new Header Media Element
191:      *
192:      * @param int $headerCount
193:      * @param string $src
194:      * @return int
195:      */
196:     public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
197:     {
198:         $mediaId = md5($src);
199:         $key = 'header' . $headerCount;
200: 
201:         if (!array_key_exists($key, self::$_headerMedia)) {
202:             self::$_headerMedia[$key] = array();
203:         }
204: 
205:         if (!array_key_exists($mediaId, self::$_headerMedia[$key])) {
206:             $cImg = self::countHeaderMediaElements($key);
207:             $rID = $cImg + 1;
208: 
209:             $cImg++;
210:             $inf = pathinfo($src);
211:             $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
212: 
213:             $media = array();
214:             if ($isMemImage) {
215:                 $ext = $memoryImage->getImageExtension();
216:                 $media['isMemImage'] = true;
217:                 $media['createfunction'] = $memoryImage->getImageCreateFunction();
218:                 $media['imagefunction'] = $memoryImage->getImageFunction();
219:             } else {
220:                 $ext = $inf['extension'];
221:                 if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
222:                     $ext = 'jpg';
223:                 }
224:             }
225:             $file = 'image' . $cImg . '.' . strtolower($ext);
226: 
227:             $media['source'] = $src;
228:             $media['target'] = 'media/' . $key . '_' . $file;
229:             $media['type'] = 'image';
230:             $media['rID'] = $rID;
231: 
232:             self::$_headerMedia[$key][$mediaId] = $media;
233: 
234:             return $rID;
235:         } else {
236:             return self::$_headerMedia[$key][$mediaId]['rID'];
237:         }
238:     }
239: 
240:     /**
241:      * Get Header Media Elements Count
242:      *
243:      * @param string $key
244:      * @return int
245:      */
246:     public static function countHeaderMediaElements($key)
247:     {
248:         return count(self::$_headerMedia[$key]);
249:     }
250: 
251:     /**
252:      * Get Header Media Elements
253:      *
254:      * @return int
255:      */
256:     public static function getHeaderMediaElements()
257:     {
258:         return self::$_headerMedia;
259:     }
260: 
261:     /**
262:      * Add new Footer Media Element
263:      *
264:      * @param int $footerCount
265:      * @param string $src
266:      * @return int
267:      */
268:     public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
269:     {
270:         $mediaId = md5($src);
271:         $key = 'footer' . $footerCount;
272: 
273:         if (!array_key_exists($key, self::$_footerMedia)) {
274:             self::$_footerMedia[$key] = array();
275:         }
276: 
277:         if (!array_key_exists($mediaId, self::$_footerMedia[$key])) {
278:             $cImg = self::countFooterMediaElements($key);
279:             $rID = $cImg + 1;
280: 
281:             $cImg++;
282:             $inf = pathinfo($src);
283:             $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
284: 
285:             $media = array();
286:             if ($isMemImage) {
287:                 $ext = $memoryImage->getImageExtension();
288:                 $media['isMemImage'] = true;
289:                 $media['createfunction'] = $memoryImage->getImageCreateFunction();
290:                 $media['imagefunction'] = $memoryImage->getImageFunction();
291:             } else {
292:                 $ext = $inf['extension'];
293:                 if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
294:                     $ext = 'jpg';
295:                 }
296:             }
297:             $file = 'image' . $cImg . '.' . strtolower($ext);
298: 
299:             $media['source'] = $src;
300:             $media['target'] = 'media/' . $key . '_' . $file;
301:             $media['type'] = 'image';
302:             $media['rID'] = $rID;
303: 
304:             self::$_footerMedia[$key][$mediaId] = $media;
305: 
306:             return $rID;
307:         } else {
308:             return self::$_footerMedia[$key][$mediaId]['rID'];
309:         }
310:     }
311: 
312:     /**
313:      * Get Footer Media Elements Count
314:      *
315:      * @param string $key
316:      * @return int
317:      */
318:     public static function countFooterMediaElements($key)
319:     {
320:         return count(self::$_footerMedia[$key]);
321:     }
322: 
323:     /**
324:      * Get Footer Media Elements
325:      *
326:      * @return int
327:      */
328:     public static function getFooterMediaElements()
329:     {
330:         return self::$_footerMedia;
331:     }
332: }
333: 
334: 
PHPWord API Docs API documentation generated by ApiGen 2.8.0