BaseDrawing.php 11 KB

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