File Source for Drawing.php

PHPExcel_Writer_Excel2007

# Source for file Drawing.php

Documentation is available at Drawing.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (c) 2006 - 2011 PHPExcel
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 PHPExcel
22. * @package PHPExcel_Writer_Excel2007
23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
29. /**
30. * PHPExcel_Writer_Excel2007_Drawing
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Writer_Excel2007
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Writer_Excel2007_Drawing extends PHPExcel_Writer_Excel2007_WriterPart
37. {
38. /**
39. * Write drawings to XML format
40. *
41. * @param PHPExcel_Worksheet $pWorksheet
42. * @return string XML Output
43. * @throws Exception
44. */
45. public function writeDrawings(PHPExcel_Worksheet $pWorksheet = null)
46. {
47. // Create XML writer
48. $objWriter = null;
49. if ($this-> getParentWriter()->getUseDiskCaching()) {
50. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_DISK, $this-> getParentWriter()->getDiskCachingDirectory());
51. } else {
52. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_MEMORY);
53. }
55. // XML header
56. $objWriter->startDocument('1.0','UTF-8','yes');
58. // xdr:wsDr
59. $objWriter->startElement('xdr:wsDr');
60. $objWriter->writeAttribute('xmlns:xdr', 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing');
61. $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
63. // Loop through images and write drawings
64. $i = 1;
65. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
66. while ($iterator->valid()) {
67. $this->_writeDrawing($objWriter, $iterator->current(), $i);
69. $iterator->next();
70. ++$i;
71. }
73. $objWriter->endElement();
75. // Return
76. return $objWriter->getData();
77. }
79. /**
80. * Write drawings to XML format
81. *
82. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
83. * @param PHPExcel_Worksheet_BaseDrawing $pDrawing
84. * @param int $pRelationId
85. * @throws Exception
86. */
87. public function _writeDrawing(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet_BaseDrawing $pDrawing = null, $pRelationId = -1)
88. {
89. if ($pRelationId >= 0) {
90. // xdr:oneCellAnchor
91. $objWriter->startElement('xdr:oneCellAnchor');
92. // Image location
93. $aCoordinates = PHPExcel_Cell:: coordinateFromString($pDrawing->getCoordinates());
94. $aCoordinates[0] = PHPExcel_Cell:: columnIndexFromString($aCoordinates[0]);
96. // xdr:from
97. $objWriter->startElement('xdr:from');
98. $objWriter->writeElement('xdr:col', $aCoordinates[0] - 1);
99. $objWriter->writeElement('xdr:colOff', PHPExcel_Shared_Drawing:: pixelsToEMU($pDrawing->getOffsetX()));
100. $objWriter->writeElement('xdr:row', $aCoordinates[1] - 1);
101. $objWriter->writeElement('xdr:rowOff', PHPExcel_Shared_Drawing:: pixelsToEMU($pDrawing->getOffsetY()));
102. $objWriter->endElement();
104. // xdr:ext
105. $objWriter->startElement('xdr:ext');
106. $objWriter->writeAttribute('cx', PHPExcel_Shared_Drawing:: pixelsToEMU($pDrawing->getWidth()));
107. $objWriter->writeAttribute('cy', PHPExcel_Shared_Drawing:: pixelsToEMU($pDrawing->getHeight()));
108. $objWriter->endElement();
110. // xdr:pic
111. $objWriter->startElement('xdr:pic');
113. // xdr:nvPicPr
114. $objWriter->startElement('xdr:nvPicPr');
116. // xdr:cNvPr
117. $objWriter->startElement('xdr:cNvPr');
118. $objWriter->writeAttribute('id', $pRelationId);
119. $objWriter->writeAttribute('name', $pDrawing->getName());
120. $objWriter->writeAttribute('descr', $pDrawing->getDescription());
121. $objWriter->endElement();
123. // xdr:cNvPicPr
124. $objWriter->startElement('xdr:cNvPicPr');
126. // a:picLocks
127. $objWriter->startElement('a:picLocks');
128. $objWriter->writeAttribute('noChangeAspect', '1');
129. $objWriter->endElement();
131. $objWriter->endElement();
133. $objWriter->endElement();
135. // xdr:blipFill
136. $objWriter->startElement('xdr:blipFill');
138. // a:blip
139. $objWriter->startElement('a:blip');
140. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
141. $objWriter->writeAttribute('r:embed', 'rId' . $pRelationId);
142. $objWriter->endElement();
144. // a:stretch
145. $objWriter->startElement('a:stretch');
146. $objWriter->writeElement('a:fillRect', null);
147. $objWriter->endElement();
149. $objWriter->endElement();
151. // xdr:spPr
152. $objWriter->startElement('xdr:spPr');
154. // a:xfrm
155. $objWriter->startElement('a:xfrm');
156. $objWriter->writeAttribute('rot', PHPExcel_Shared_Drawing:: degreesToAngle($pDrawing->getRotation()));
157. $objWriter->endElement();
159. // a:prstGeom
160. $objWriter->startElement('a:prstGeom');
161. $objWriter->writeAttribute('prst', 'rect');
163. // a:avLst
164. $objWriter->writeElement('a:avLst', null);
166. $objWriter->endElement();
168. // // a:solidFill
169. // $objWriter->startElement('a:solidFill');
171. // // a:srgbClr
172. // $objWriter->startElement('a:srgbClr');
173. // $objWriter->writeAttribute('val', 'FFFFFF');
175. ///* SHADE
176. // // a:shade
177. // $objWriter->startElement('a:shade');
178. // $objWriter->writeAttribute('val', '85000');
179. // $objWriter->endElement();
180. //*/
182. // $objWriter->endElement();
184. // $objWriter->endElement();
185. /*
186. // a:ln
187. $objWriter->startElement('a:ln');
188. $objWriter->writeAttribute('w', '88900');
189. $objWriter->writeAttribute('cap', 'sq');
191. // a:solidFill
192. $objWriter->startElement('a:solidFill');
194. // a:srgbClr
195. $objWriter->startElement('a:srgbClr');
196. $objWriter->writeAttribute('val', 'FFFFFF');
197. $objWriter->endElement();
199. $objWriter->endElement();
201. // a:miter
202. $objWriter->startElement('a:miter');
203. $objWriter->writeAttribute('lim', '800000');
204. $objWriter->endElement();
206. $objWriter->endElement();
207. */
209. if ($pDrawing->getShadow()->getVisible()) {
210. // a:effectLst
211. $objWriter->startElement('a:effectLst');
213. // a:outerShdw
214. $objWriter->startElement('a:outerShdw');
215. $objWriter->writeAttribute('blurRad', PHPExcel_Shared_Drawing:: pixelsToEMU($pDrawing->getShadow()->getBlurRadius()));
216. $objWriter->writeAttribute('dist', PHPExcel_Shared_Drawing:: pixelsToEMU($pDrawing->getShadow()->getDistance()));
217. $objWriter->writeAttribute('dir', PHPExcel_Shared_Drawing:: degreesToAngle($pDrawing->getShadow()->getDirection()));
218. $objWriter->writeAttribute('algn', $pDrawing->getShadow()->getAlignment());
219. $objWriter->writeAttribute('rotWithShape', '0');
221. // a:srgbClr
222. $objWriter->startElement('a:srgbClr');
223. $objWriter->writeAttribute('val', $pDrawing->getShadow()->getColor()->getRGB());
225. // a:alpha
226. $objWriter->startElement('a:alpha');
227. $objWriter->writeAttribute('val', $pDrawing->getShadow()->getAlpha() * 1000);
228. $objWriter->endElement();
230. $objWriter->endElement();
232. $objWriter->endElement();
234. $objWriter->endElement();
235. }
236. /*
238. // a:scene3d
239. $objWriter->startElement('a:scene3d');
241. // a:camera
242. $objWriter->startElement('a:camera');
243. $objWriter->writeAttribute('prst', 'orthographicFront');
244. $objWriter->endElement();
246. // a:lightRig
247. $objWriter->startElement('a:lightRig');
248. $objWriter->writeAttribute('rig', 'twoPt');
249. $objWriter->writeAttribute('dir', 't');
251. // a:rot
252. $objWriter->startElement('a:rot');
253. $objWriter->writeAttribute('lat', '0');
254. $objWriter->writeAttribute('lon', '0');
255. $objWriter->writeAttribute('rev', '0');
256. $objWriter->endElement();
258. $objWriter->endElement();
260. $objWriter->endElement();
261. */
262. /*
263. // a:sp3d
264. $objWriter->startElement('a:sp3d');
266. // a:bevelT
267. $objWriter->startElement('a:bevelT');
268. $objWriter->writeAttribute('w', '25400');
269. $objWriter->writeAttribute('h', '19050');
270. $objWriter->endElement();
272. // a:contourClr
273. $objWriter->startElement('a:contourClr');
275. // a:srgbClr
276. $objWriter->startElement('a:srgbClr');
277. $objWriter->writeAttribute('val', 'FFFFFF');
278. $objWriter->endElement();
280. $objWriter->endElement();
282. $objWriter->endElement();
283. */
284. $objWriter->endElement();
286. $objWriter->endElement();
288. // xdr:clientData
289. $objWriter->writeElement('xdr:clientData', null);
291. $objWriter->endElement();
292. } else {
293. throw new Exception("Invalid parameters passed.");
294. }
295. }
297. /**
298. * Write VML header/footer images to XML format
299. *
300. * @param PHPExcel_Worksheet $pWorksheet
301. * @return string XML Output
302. * @throws Exception
303. */
304. public function writeVMLHeaderFooterImages(PHPExcel_Worksheet $pWorksheet = null)
305. {
306. // Create XML writer
307. $objWriter = null;
308. if ($this-> getParentWriter()->getUseDiskCaching()) {
309. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_DISK, $this-> getParentWriter()->getDiskCachingDirectory());
310. } else {
311. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_MEMORY);
312. }
314. // XML header
315. $objWriter->startDocument('1.0','UTF-8','yes');
317. // Header/footer images
318. $images = $pWorksheet->getHeaderFooter()->getImages();
320. // xml
321. $objWriter->startElement('xml');
322. $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
323. $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
324. $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
326. // o:shapelayout
327. $objWriter->startElement('o:shapelayout');
328. $objWriter->writeAttribute('v:ext', 'edit');
330. // o:idmap
331. $objWriter->startElement('o:idmap');
332. $objWriter->writeAttribute('v:ext', 'edit');
333. $objWriter->writeAttribute('data', '1');
334. $objWriter->endElement();
336. $objWriter->endElement();
338. // v:shapetype
339. $objWriter->startElement('v:shapetype');
340. $objWriter->writeAttribute('id', '_x0000_t75');
341. $objWriter->writeAttribute('coordsize', '21600,21600');
342. $objWriter->writeAttribute('o:spt', '75');
343. $objWriter->writeAttribute('o:preferrelative', 't');
344. $objWriter->writeAttribute('path', 'm@4@5l@4@11@9@11@9@5xe');
345. $objWriter->writeAttribute('filled', 'f');
346. $objWriter->writeAttribute('stroked', 'f');
348. // v:stroke
349. $objWriter->startElement('v:stroke');
350. $objWriter->writeAttribute('joinstyle', 'miter');
351. $objWriter->endElement();
353. // v:formulas
354. $objWriter->startElement('v:formulas');
356. // v:f
357. $objWriter->startElement('v:f');
358. $objWriter->writeAttribute('eqn', 'if lineDrawn pixelLineWidth 0');
359. $objWriter->endElement();
361. // v:f
362. $objWriter->startElement('v:f');
363. $objWriter->writeAttribute('eqn', 'sum @0 1 0');
364. $objWriter->endElement();
366. // v:f
367. $objWriter->startElement('v:f');
368. $objWriter->writeAttribute('eqn', 'sum 0 0 @1');
369. $objWriter->endElement();
371. // v:f
372. $objWriter->startElement('v:f');
373. $objWriter->writeAttribute('eqn', 'prod @2 1 2');
374. $objWriter->endElement();
376. // v:f
377. $objWriter->startElement('v:f');
378. $objWriter->writeAttribute('eqn', 'prod @3 21600 pixelWidth');
379. $objWriter->endElement();
381. // v:f
382. $objWriter->startElement('v:f');
383. $objWriter->writeAttribute('eqn', 'prod @3 21600 pixelHeight');
384. $objWriter->endElement();
386. // v:f
387. $objWriter->startElement('v:f');
388. $objWriter->writeAttribute('eqn', 'sum @0 0 1');
389. $objWriter->endElement();
391. // v:f
392. $objWriter->startElement('v:f');
393. $objWriter->writeAttribute('eqn', 'prod @6 1 2');
394. $objWriter->endElement();
396. // v:f
397. $objWriter->startElement('v:f');
398. $objWriter->writeAttribute('eqn', 'prod @7 21600 pixelWidth');
399. $objWriter->endElement();
401. // v:f
402. $objWriter->startElement('v:f');
403. $objWriter->writeAttribute('eqn', 'sum @8 21600 0');
404. $objWriter->endElement();
406. // v:f
407. $objWriter->startElement('v:f');
408. $objWriter->writeAttribute('eqn', 'prod @7 21600 pixelHeight');
409. $objWriter->endElement();
411. // v:f
412. $objWriter->startElement('v:f');
413. $objWriter->writeAttribute('eqn', 'sum @10 21600 0');
414. $objWriter->endElement();
416. $objWriter->endElement();
418. // v:path
419. $objWriter->startElement('v:path');
420. $objWriter->writeAttribute('o:extrusionok', 'f');
421. $objWriter->writeAttribute('gradientshapeok', 't');
422. $objWriter->writeAttribute('o:connecttype', 'rect');
423. $objWriter->endElement();
425. // o:lock
426. $objWriter->startElement('o:lock');
427. $objWriter->writeAttribute('v:ext', 'edit');
428. $objWriter->writeAttribute('aspectratio', 't');
429. $objWriter->endElement();
431. $objWriter->endElement();
433. // Loop through images
434. foreach ($images as $key => $value) {
435. $this->_writeVMLHeaderFooterImage($objWriter, $key, $value);
436. }
438. $objWriter->endElement();
440. // Return
441. return $objWriter->getData();
442. }
444. /**
445. * Write VML comment to XML format
446. *
447. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
448. * @param string $pReference Reference
449. * @param PHPExcel_Worksheet_HeaderFooterDrawing $pImage Image
450. * @throws Exception
451. */
452. public function _writeVMLHeaderFooterImage(PHPExcel_Shared_XMLWriter $objWriter = null, $pReference = '', PHPExcel_Worksheet_HeaderFooterDrawing $pImage = null)
453. {
454. // Calculate object id
455. preg_match('{(\d+)}', md5($pReference), $m);
456. $id = 1500 + ( substr($m[1], 0, 2) * 1);
458. // Calculate offset
459. $width = $pImage->getWidth();
460. $height = $pImage->getHeight();
461. $marginLeft = $pImage->getOffsetX();
462. $marginTop = $pImage->getOffsetY();
464. // v:shape
465. $objWriter->startElement('v:shape');
466. $objWriter->writeAttribute('id', $pReference);
467. $objWriter->writeAttribute('o:spid', '_x0000_s' . $id);
468. $objWriter->writeAttribute('type', '#_x0000_t75');
469. $objWriter->writeAttribute('style', "position:absolute;margin-left:{$marginLeft}px;margin-top:{$marginTop}px;width:{$width}px;height:{$height}px;z-index:1");
471. // v:imagedata
472. $objWriter->startElement('v:imagedata');
473. $objWriter->writeAttribute('o:relid', 'rId' . $pReference);
474. $objWriter->writeAttribute('o:title', $pImage->getName());
475. $objWriter->endElement();
477. // o:lock
478. $objWriter->startElement('o:lock');
479. $objWriter->writeAttribute('v:ext', 'edit');
480. $objWriter->writeAttribute('rotation', 't');
481. $objWriter->endElement();
483. $objWriter->endElement();
484. }
487. /**
488. * Get an array of all drawings
489. *
490. * @param PHPExcel $pPHPExcel
491. * @return PHPExcel_Worksheet_Drawing[] All drawings in PHPExcel
492. * @throws Exception
493. */
494. public function allDrawings(PHPExcel $pPHPExcel = null)
495. {
496. // Get an array of all drawings
497. $aDrawings = array();
499. // Loop through PHPExcel
500. $sheetCount = $pPHPExcel->getSheetCount();
501. for ($i = 0; $i < $sheetCount; ++$i) {
502. // Loop through images and add to array
503. $iterator = $pPHPExcel->getSheet($i)->getDrawingCollection()->getIterator();
504. while ($iterator->valid()) {
505. $aDrawings[] = $iterator->current();
507. $iterator->next();
508. }
509. }
511. return $aDrawings;
512. }
513. }

Documentation generated on Sun, 27 Feb 2011 16:30:15 -0800 by phpDocumentor 1.4.3