BaseDrawing.php 9.7 KB

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