/libs_frame/PhpOffice/PhpSpreadsheet/Worksheet/BaseDrawing.php

https://github.com/a07061625/swooleyaf · PHP · 535 lines · 222 code · 61 blank · 252 comment · 28 complexity · 28411b0b38360b9d5e7c65adc72fe1a8 MD5 · raw file

  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
  4. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  5. use PhpOffice\PhpSpreadsheet\IComparable;
  6. class BaseDrawing implements IComparable
  7. {
  8. /**
  9. * Name.
  10. *
  11. * @var string
  12. */
  13. protected $name;
  14. /**
  15. * Description.
  16. *
  17. * @var string
  18. */
  19. protected $description;
  20. /**
  21. * Worksheet.
  22. *
  23. * @var Worksheet
  24. */
  25. protected $worksheet;
  26. /**
  27. * Coordinates.
  28. *
  29. * @var string
  30. */
  31. protected $coordinates;
  32. /**
  33. * Offset X.
  34. *
  35. * @var int
  36. */
  37. protected $offsetX;
  38. /**
  39. * Offset Y.
  40. *
  41. * @var int
  42. */
  43. protected $offsetY;
  44. /**
  45. * Width.
  46. *
  47. * @var int
  48. */
  49. protected $width;
  50. /**
  51. * Height.
  52. *
  53. * @var int
  54. */
  55. protected $height;
  56. /**
  57. * Proportional resize.
  58. *
  59. * @var bool
  60. */
  61. protected $resizeProportional;
  62. /**
  63. * Rotation.
  64. *
  65. * @var int
  66. */
  67. protected $rotation;
  68. /**
  69. * Shadow.
  70. *
  71. * @var Drawing\Shadow
  72. */
  73. protected $shadow;
  74. /**
  75. * Image counter.
  76. *
  77. * @var int
  78. */
  79. private static $imageCounter = 0;
  80. /**
  81. * Image index.
  82. *
  83. * @var int
  84. */
  85. private $imageIndex = 0;
  86. /**
  87. * Image hyperlink.
  88. *
  89. * @var null|Hyperlink
  90. */
  91. private $hyperlink;
  92. /**
  93. * Create a new BaseDrawing.
  94. */
  95. public function __construct()
  96. {
  97. // Initialise values
  98. $this->name = '';
  99. $this->description = '';
  100. $this->worksheet = null;
  101. $this->coordinates = 'A1';
  102. $this->offsetX = 0;
  103. $this->offsetY = 0;
  104. $this->width = 0;
  105. $this->height = 0;
  106. $this->resizeProportional = true;
  107. $this->rotation = 0;
  108. $this->shadow = new Drawing\Shadow();
  109. // Set image index
  110. ++self::$imageCounter;
  111. $this->imageIndex = self::$imageCounter;
  112. }
  113. /**
  114. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  115. */
  116. public function __clone()
  117. {
  118. $vars = get_object_vars($this);
  119. foreach ($vars as $key => $value) {
  120. if ($key == 'worksheet') {
  121. $this->worksheet = null;
  122. } elseif (is_object($value)) {
  123. $this->$key = clone $value;
  124. } else {
  125. $this->$key = $value;
  126. }
  127. }
  128. }
  129. /**
  130. * Get image index.
  131. *
  132. * @return int
  133. */
  134. public function getImageIndex()
  135. {
  136. return $this->imageIndex;
  137. }
  138. /**
  139. * Get Name.
  140. *
  141. * @return string
  142. */
  143. public function getName()
  144. {
  145. return $this->name;
  146. }
  147. /**
  148. * Set Name.
  149. *
  150. * @param string $pValue
  151. *
  152. * @return $this
  153. */
  154. public function setName($pValue)
  155. {
  156. $this->name = $pValue;
  157. return $this;
  158. }
  159. /**
  160. * Get Description.
  161. *
  162. * @return string
  163. */
  164. public function getDescription()
  165. {
  166. return $this->description;
  167. }
  168. /**
  169. * Set Description.
  170. *
  171. * @param string $description
  172. *
  173. * @return $this
  174. */
  175. public function setDescription($description)
  176. {
  177. $this->description = $description;
  178. return $this;
  179. }
  180. /**
  181. * Get Worksheet.
  182. *
  183. * @return Worksheet
  184. */
  185. public function getWorksheet()
  186. {
  187. return $this->worksheet;
  188. }
  189. /**
  190. * Set Worksheet.
  191. *
  192. * @param Worksheet $pValue
  193. * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
  194. *
  195. * @throws PhpSpreadsheetException
  196. *
  197. * @return $this
  198. */
  199. public function setWorksheet(Worksheet $pValue = null, $pOverrideOld = false)
  200. {
  201. if ($this->worksheet === null) {
  202. // Add drawing to \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  203. $this->worksheet = $pValue;
  204. $this->worksheet->getCell($this->coordinates);
  205. $this->worksheet->getDrawingCollection()->append($this);
  206. } else {
  207. if ($pOverrideOld) {
  208. // Remove drawing from old \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  209. $iterator = $this->worksheet->getDrawingCollection()->getIterator();
  210. while ($iterator->valid()) {
  211. if ($iterator->current()->getHashCode() == $this->getHashCode()) {
  212. $this->worksheet->getDrawingCollection()->offsetUnset($iterator->key());
  213. $this->worksheet = null;
  214. break;
  215. }
  216. }
  217. // Set new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  218. $this->setWorksheet($pValue);
  219. } else {
  220. throw new PhpSpreadsheetException('A Worksheet has already been assigned. Drawings can only exist on one \\PhpOffice\\PhpSpreadsheet\\Worksheet.');
  221. }
  222. }
  223. return $this;
  224. }
  225. /**
  226. * Get Coordinates.
  227. *
  228. * @return string
  229. */
  230. public function getCoordinates()
  231. {
  232. return $this->coordinates;
  233. }
  234. /**
  235. * Set Coordinates.
  236. *
  237. * @param string $pValue eg: 'A1'
  238. *
  239. * @return $this
  240. */
  241. public function setCoordinates($pValue)
  242. {
  243. $this->coordinates = $pValue;
  244. return $this;
  245. }
  246. /**
  247. * Get OffsetX.
  248. *
  249. * @return int
  250. */
  251. public function getOffsetX()
  252. {
  253. return $this->offsetX;
  254. }
  255. /**
  256. * Set OffsetX.
  257. *
  258. * @param int $pValue
  259. *
  260. * @return $this
  261. */
  262. public function setOffsetX($pValue)
  263. {
  264. $this->offsetX = $pValue;
  265. return $this;
  266. }
  267. /**
  268. * Get OffsetY.
  269. *
  270. * @return int
  271. */
  272. public function getOffsetY()
  273. {
  274. return $this->offsetY;
  275. }
  276. /**
  277. * Set OffsetY.
  278. *
  279. * @param int $pValue
  280. *
  281. * @return $this
  282. */
  283. public function setOffsetY($pValue)
  284. {
  285. $this->offsetY = $pValue;
  286. return $this;
  287. }
  288. /**
  289. * Get Width.
  290. *
  291. * @return int
  292. */
  293. public function getWidth()
  294. {
  295. return $this->width;
  296. }
  297. /**
  298. * Set Width.
  299. *
  300. * @param int $pValue
  301. *
  302. * @return $this
  303. */
  304. public function setWidth($pValue)
  305. {
  306. // Resize proportional?
  307. if ($this->resizeProportional && $pValue != 0) {
  308. $ratio = $this->height / ($this->width != 0 ? $this->width : 1);
  309. $this->height = round($ratio * $pValue);
  310. }
  311. // Set width
  312. $this->width = $pValue;
  313. return $this;
  314. }
  315. /**
  316. * Get Height.
  317. *
  318. * @return int
  319. */
  320. public function getHeight()
  321. {
  322. return $this->height;
  323. }
  324. /**
  325. * Set Height.
  326. *
  327. * @param int $pValue
  328. *
  329. * @return $this
  330. */
  331. public function setHeight($pValue)
  332. {
  333. // Resize proportional?
  334. if ($this->resizeProportional && $pValue != 0) {
  335. $ratio = $this->width / ($this->height != 0 ? $this->height : 1);
  336. $this->width = round($ratio * $pValue);
  337. }
  338. // Set height
  339. $this->height = $pValue;
  340. return $this;
  341. }
  342. /**
  343. * Set width and height with proportional resize.
  344. *
  345. * Example:
  346. * <code>
  347. * $objDrawing->setResizeProportional(true);
  348. * $objDrawing->setWidthAndHeight(160,120);
  349. * </code>
  350. *
  351. * @author Vincent@luo MSN:kele_100@hotmail.com
  352. *
  353. * @param int $width
  354. * @param int $height
  355. *
  356. * @return $this
  357. */
  358. public function setWidthAndHeight($width, $height)
  359. {
  360. $xratio = $width / ($this->width != 0 ? $this->width : 1);
  361. $yratio = $height / ($this->height != 0 ? $this->height : 1);
  362. if ($this->resizeProportional && !($width == 0 || $height == 0)) {
  363. if (($xratio * $this->height) < $height) {
  364. $this->height = ceil($xratio * $this->height);
  365. $this->width = $width;
  366. } else {
  367. $this->width = ceil($yratio * $this->width);
  368. $this->height = $height;
  369. }
  370. } else {
  371. $this->width = $width;
  372. $this->height = $height;
  373. }
  374. return $this;
  375. }
  376. /**
  377. * Get ResizeProportional.
  378. *
  379. * @return bool
  380. */
  381. public function getResizeProportional()
  382. {
  383. return $this->resizeProportional;
  384. }
  385. /**
  386. * Set ResizeProportional.
  387. *
  388. * @param bool $pValue
  389. *
  390. * @return $this
  391. */
  392. public function setResizeProportional($pValue)
  393. {
  394. $this->resizeProportional = $pValue;
  395. return $this;
  396. }
  397. /**
  398. * Get Rotation.
  399. *
  400. * @return int
  401. */
  402. public function getRotation()
  403. {
  404. return $this->rotation;
  405. }
  406. /**
  407. * Set Rotation.
  408. *
  409. * @param int $pValue
  410. *
  411. * @return $this
  412. */
  413. public function setRotation($pValue)
  414. {
  415. $this->rotation = $pValue;
  416. return $this;
  417. }
  418. /**
  419. * Get Shadow.
  420. *
  421. * @return Drawing\Shadow
  422. */
  423. public function getShadow()
  424. {
  425. return $this->shadow;
  426. }
  427. /**
  428. * Set Shadow.
  429. *
  430. * @param Drawing\Shadow $pValue
  431. *
  432. * @return $this
  433. */
  434. public function setShadow(Drawing\Shadow $pValue = null)
  435. {
  436. $this->shadow = $pValue;
  437. return $this;
  438. }
  439. /**
  440. * Get hash code.
  441. *
  442. * @return string Hash code
  443. */
  444. public function getHashCode()
  445. {
  446. return md5(
  447. $this->name .
  448. $this->description .
  449. $this->worksheet->getHashCode() .
  450. $this->coordinates .
  451. $this->offsetX .
  452. $this->offsetY .
  453. $this->width .
  454. $this->height .
  455. $this->rotation .
  456. $this->shadow->getHashCode() .
  457. __CLASS__
  458. );
  459. }
  460. /**
  461. * @param null|Hyperlink $pHyperlink
  462. */
  463. public function setHyperlink(Hyperlink $pHyperlink = null)
  464. {
  465. $this->hyperlink = $pHyperlink;
  466. }
  467. /**
  468. * @return null|Hyperlink
  469. */
  470. public function getHyperlink()
  471. {
  472. return $this->hyperlink;
  473. }
  474. }