ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Drawing.php
Go to the documentation of this file.
1 <?php
37 {
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 {
53  }
54 
55  // XML header
56  $objWriter->startDocument('1.0','UTF-8','yes');
57 
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');
62 
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);
68 
69  $iterator->next();
70  ++$i;
71  }
72 
73  $objWriter->endElement();
74 
75  // Return
76  return $objWriter->getData();
77  }
78 
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]);
95 
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();
103 
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();
109 
110  // xdr:pic
111  $objWriter->startElement('xdr:pic');
112 
113  // xdr:nvPicPr
114  $objWriter->startElement('xdr:nvPicPr');
115 
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();
122 
123  // xdr:cNvPicPr
124  $objWriter->startElement('xdr:cNvPicPr');
125 
126  // a:picLocks
127  $objWriter->startElement('a:picLocks');
128  $objWriter->writeAttribute('noChangeAspect', '1');
129  $objWriter->endElement();
130 
131  $objWriter->endElement();
132 
133  $objWriter->endElement();
134 
135  // xdr:blipFill
136  $objWriter->startElement('xdr:blipFill');
137 
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();
143 
144  // a:stretch
145  $objWriter->startElement('a:stretch');
146  $objWriter->writeElement('a:fillRect', null);
147  $objWriter->endElement();
148 
149  $objWriter->endElement();
150 
151  // xdr:spPr
152  $objWriter->startElement('xdr:spPr');
153 
154  // a:xfrm
155  $objWriter->startElement('a:xfrm');
156  $objWriter->writeAttribute('rot', PHPExcel_Shared_Drawing::degreesToAngle($pDrawing->getRotation()));
157  $objWriter->endElement();
158 
159  // a:prstGeom
160  $objWriter->startElement('a:prstGeom');
161  $objWriter->writeAttribute('prst', 'rect');
162 
163  // a:avLst
164  $objWriter->writeElement('a:avLst', null);
165 
166  $objWriter->endElement();
167 
168 // // a:solidFill
169 // $objWriter->startElement('a:solidFill');
170 
171 // // a:srgbClr
172 // $objWriter->startElement('a:srgbClr');
173 // $objWriter->writeAttribute('val', 'FFFFFF');
174 
176 // // a:shade
177 // $objWriter->startElement('a:shade');
178 // $objWriter->writeAttribute('val', '85000');
179 // $objWriter->endElement();
180 //*/
181 
182 // $objWriter->endElement();
183 
184 // $objWriter->endElement();
185 /*
186  // a:ln
187  $objWriter->startElement('a:ln');
188  $objWriter->writeAttribute('w', '88900');
189  $objWriter->writeAttribute('cap', 'sq');
190 
191  // a:solidFill
192  $objWriter->startElement('a:solidFill');
193 
194  // a:srgbClr
195  $objWriter->startElement('a:srgbClr');
196  $objWriter->writeAttribute('val', 'FFFFFF');
197  $objWriter->endElement();
198 
199  $objWriter->endElement();
200 
201  // a:miter
202  $objWriter->startElement('a:miter');
203  $objWriter->writeAttribute('lim', '800000');
204  $objWriter->endElement();
205 
206  $objWriter->endElement();
207 */
208 
209  if ($pDrawing->getShadow()->getVisible()) {
210  // a:effectLst
211  $objWriter->startElement('a:effectLst');
212 
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');
220 
221  // a:srgbClr
222  $objWriter->startElement('a:srgbClr');
223  $objWriter->writeAttribute('val', $pDrawing->getShadow()->getColor()->getRGB());
224 
225  // a:alpha
226  $objWriter->startElement('a:alpha');
227  $objWriter->writeAttribute('val', $pDrawing->getShadow()->getAlpha() * 1000);
228  $objWriter->endElement();
229 
230  $objWriter->endElement();
231 
232  $objWriter->endElement();
233 
234  $objWriter->endElement();
235  }
236 /*
237 
238  // a:scene3d
239  $objWriter->startElement('a:scene3d');
240 
241  // a:camera
242  $objWriter->startElement('a:camera');
243  $objWriter->writeAttribute('prst', 'orthographicFront');
244  $objWriter->endElement();
245 
246  // a:lightRig
247  $objWriter->startElement('a:lightRig');
248  $objWriter->writeAttribute('rig', 'twoPt');
249  $objWriter->writeAttribute('dir', 't');
250 
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();
257 
258  $objWriter->endElement();
259 
260  $objWriter->endElement();
261 */
262 /*
263  // a:sp3d
264  $objWriter->startElement('a:sp3d');
265 
266  // a:bevelT
267  $objWriter->startElement('a:bevelT');
268  $objWriter->writeAttribute('w', '25400');
269  $objWriter->writeAttribute('h', '19050');
270  $objWriter->endElement();
271 
272  // a:contourClr
273  $objWriter->startElement('a:contourClr');
274 
275  // a:srgbClr
276  $objWriter->startElement('a:srgbClr');
277  $objWriter->writeAttribute('val', 'FFFFFF');
278  $objWriter->endElement();
279 
280  $objWriter->endElement();
281 
282  $objWriter->endElement();
283 */
284  $objWriter->endElement();
285 
286  $objWriter->endElement();
287 
288  // xdr:clientData
289  $objWriter->writeElement('xdr:clientData', null);
290 
291  $objWriter->endElement();
292  } else {
293  throw new Exception("Invalid parameters passed.");
294  }
295  }
296 
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 {
312  }
313 
314  // XML header
315  $objWriter->startDocument('1.0','UTF-8','yes');
316 
317  // Header/footer images
318  $images = $pWorksheet->getHeaderFooter()->getImages();
319 
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');
325 
326  // o:shapelayout
327  $objWriter->startElement('o:shapelayout');
328  $objWriter->writeAttribute('v:ext', 'edit');
329 
330  // o:idmap
331  $objWriter->startElement('o:idmap');
332  $objWriter->writeAttribute('v:ext', 'edit');
333  $objWriter->writeAttribute('data', '1');
334  $objWriter->endElement();
335 
336  $objWriter->endElement();
337 
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');
347 
348  // v:stroke
349  $objWriter->startElement('v:stroke');
350  $objWriter->writeAttribute('joinstyle', 'miter');
351  $objWriter->endElement();
352 
353  // v:formulas
354  $objWriter->startElement('v:formulas');
355 
356  // v:f
357  $objWriter->startElement('v:f');
358  $objWriter->writeAttribute('eqn', 'if lineDrawn pixelLineWidth 0');
359  $objWriter->endElement();
360 
361  // v:f
362  $objWriter->startElement('v:f');
363  $objWriter->writeAttribute('eqn', 'sum @0 1 0');
364  $objWriter->endElement();
365 
366  // v:f
367  $objWriter->startElement('v:f');
368  $objWriter->writeAttribute('eqn', 'sum 0 0 @1');
369  $objWriter->endElement();
370 
371  // v:f
372  $objWriter->startElement('v:f');
373  $objWriter->writeAttribute('eqn', 'prod @2 1 2');
374  $objWriter->endElement();
375 
376  // v:f
377  $objWriter->startElement('v:f');
378  $objWriter->writeAttribute('eqn', 'prod @3 21600 pixelWidth');
379  $objWriter->endElement();
380 
381  // v:f
382  $objWriter->startElement('v:f');
383  $objWriter->writeAttribute('eqn', 'prod @3 21600 pixelHeight');
384  $objWriter->endElement();
385 
386  // v:f
387  $objWriter->startElement('v:f');
388  $objWriter->writeAttribute('eqn', 'sum @0 0 1');
389  $objWriter->endElement();
390 
391  // v:f
392  $objWriter->startElement('v:f');
393  $objWriter->writeAttribute('eqn', 'prod @6 1 2');
394  $objWriter->endElement();
395 
396  // v:f
397  $objWriter->startElement('v:f');
398  $objWriter->writeAttribute('eqn', 'prod @7 21600 pixelWidth');
399  $objWriter->endElement();
400 
401  // v:f
402  $objWriter->startElement('v:f');
403  $objWriter->writeAttribute('eqn', 'sum @8 21600 0');
404  $objWriter->endElement();
405 
406  // v:f
407  $objWriter->startElement('v:f');
408  $objWriter->writeAttribute('eqn', 'prod @7 21600 pixelHeight');
409  $objWriter->endElement();
410 
411  // v:f
412  $objWriter->startElement('v:f');
413  $objWriter->writeAttribute('eqn', 'sum @10 21600 0');
414  $objWriter->endElement();
415 
416  $objWriter->endElement();
417 
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();
424 
425  // o:lock
426  $objWriter->startElement('o:lock');
427  $objWriter->writeAttribute('v:ext', 'edit');
428  $objWriter->writeAttribute('aspectratio', 't');
429  $objWriter->endElement();
430 
431  $objWriter->endElement();
432 
433  // Loop through images
434  foreach ($images as $key => $value) {
435  $this->_writeVMLHeaderFooterImage($objWriter, $key, $value);
436  }
437 
438  $objWriter->endElement();
439 
440  // Return
441  return $objWriter->getData();
442  }
443 
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);
457 
458  // Calculate offset
459  $width = $pImage->getWidth();
460  $height = $pImage->getHeight();
461  $marginLeft = $pImage->getOffsetX();
462  $marginTop = $pImage->getOffsetY();
463 
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");
470 
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();
476 
477  // o:lock
478  $objWriter->startElement('o:lock');
479  $objWriter->writeAttribute('v:ext', 'edit');
480  $objWriter->writeAttribute('rotation', 't');
481  $objWriter->endElement();
482 
483  $objWriter->endElement();
484  }
485 
486 
494  public function allDrawings(PHPExcel $pPHPExcel = null)
495  {
496  // Get an array of all drawings
497  $aDrawings = array();
498 
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();
506 
507  $iterator->next();
508  }
509  }
510 
511  return $aDrawings;
512  }
513 }