OLEObject.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Element;
  18. use PhpOffice\PhpWord\Exception\InvalidObjectException;
  19. use PhpOffice\PhpWord\Style\Image as ImageStyle;
  20. /**
  21. * OLEObject element
  22. */
  23. class OLEObject extends AbstractElement
  24. {
  25. /**
  26. * Ole-Object Src
  27. *
  28. * @var string
  29. */
  30. private $source;
  31. /**
  32. * Image Style
  33. *
  34. * @var \PhpOffice\PhpWord\Style\Image
  35. */
  36. private $style;
  37. /**
  38. * Icon
  39. *
  40. * @var string
  41. */
  42. private $icon;
  43. /**
  44. * Image Relation ID
  45. *
  46. * @var int
  47. */
  48. private $imageRelationId;
  49. /**
  50. * Has media relation flag; true for Link, Image, and Object
  51. *
  52. * @var bool
  53. */
  54. protected $mediaRelation = true;
  55. /**
  56. * Create a new Ole-Object Element
  57. *
  58. * @param string $source
  59. * @param mixed $style
  60. *
  61. * @throws \PhpOffice\PhpWord\Exception\InvalidObjectException
  62. */
  63. public function __construct($source, $style = null)
  64. {
  65. $supportedTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx');
  66. $pathInfo = pathinfo($source);
  67. if (file_exists($source) && in_array($pathInfo['extension'], $supportedTypes)) {
  68. $ext = $pathInfo['extension'];
  69. if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
  70. $ext = substr($ext, 0, -1);
  71. }
  72. $this->source = $source;
  73. $this->style = $this->setNewStyle(new ImageStyle(), $style, true);
  74. $this->icon = realpath(__DIR__ . "/../resources/{$ext}.png");
  75. return;
  76. }
  77. throw new InvalidObjectException();
  78. }
  79. /**
  80. * Get object source
  81. *
  82. * @return string
  83. */
  84. public function getSource()
  85. {
  86. return $this->source;
  87. }
  88. /**
  89. * Get object style
  90. *
  91. * @return \PhpOffice\PhpWord\Style\Image
  92. */
  93. public function getStyle()
  94. {
  95. return $this->style;
  96. }
  97. /**
  98. * Get object icon
  99. *
  100. * @return string
  101. */
  102. public function getIcon()
  103. {
  104. return $this->icon;
  105. }
  106. /**
  107. * Get image relation ID
  108. *
  109. * @return int
  110. */
  111. public function getImageRelationId()
  112. {
  113. return $this->imageRelationId;
  114. }
  115. /**
  116. * Set Image Relation ID.
  117. *
  118. * @param int $rId
  119. */
  120. public function setImageRelationId($rId)
  121. {
  122. $this->imageRelationId = $rId;
  123. }
  124. /**
  125. * Get Object ID
  126. *
  127. * @deprecated 0.10.0
  128. *
  129. * @return int
  130. *
  131. * @codeCoverageIgnore
  132. */
  133. public function getObjectId()
  134. {
  135. return $this->relationId + 1325353440;
  136. }
  137. /**
  138. * Set Object ID
  139. *
  140. * @deprecated 0.10.0
  141. *
  142. * @param int $objId
  143. *
  144. * @codeCoverageIgnore
  145. */
  146. public function setObjectId($objId)
  147. {
  148. $this->relationId = $objId;
  149. }
  150. }