Bez popisu

Image.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\ODText\Element;
  18. use PhpOffice\PhpWord\Shared\Converter;
  19. /**
  20. * Image element writer
  21. *
  22. * @since 0.10.0
  23. */
  24. class Image extends AbstractElement
  25. {
  26. /**
  27. * Write element
  28. */
  29. public function write()
  30. {
  31. $xmlWriter = $this->getXmlWriter();
  32. $element = $this->getElement();
  33. if (!$element instanceof \PhpOffice\PhpWord\Element\Image) {
  34. return;
  35. }
  36. $mediaIndex = $element->getMediaIndex();
  37. $target = 'Pictures/' . $element->getTarget();
  38. $style = $element->getStyle();
  39. $width = Converter::pixelToCm($style->getWidth());
  40. $height = Converter::pixelToCm($style->getHeight());
  41. $xmlWriter->startElement('text:p');
  42. $xmlWriter->writeAttribute('text:style-name', 'Standard');
  43. $xmlWriter->startElement('draw:frame');
  44. $xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex);
  45. $xmlWriter->writeAttribute('draw:name', $element->getElementId());
  46. $xmlWriter->writeAttribute('text:anchor-type', 'as-char');
  47. $xmlWriter->writeAttribute('svg:width', $width . 'cm');
  48. $xmlWriter->writeAttribute('svg:height', $height . 'cm');
  49. $xmlWriter->writeAttribute('draw:z-index', $mediaIndex);
  50. $xmlWriter->startElement('draw:image');
  51. $xmlWriter->writeAttribute('xlink:href', $target);
  52. $xmlWriter->writeAttribute('xlink:type', 'simple');
  53. $xmlWriter->writeAttribute('xlink:show', 'embed');
  54. $xmlWriter->writeAttribute('xlink:actuate', 'onLoad');
  55. $xmlWriter->endElement(); // draw:image
  56. $xmlWriter->endElement(); // draw:frame
  57. $xmlWriter->endElement(); // text:p
  58. }
  59. }