BaseDrawing.php 11 KB

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