BaseDrawing.php 10 KB

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