BaseDrawing.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Worksheet_BaseDrawing
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Worksheet
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  35. {
  36. /**
  37. * Image counter
  38. *
  39. * @var int
  40. */
  41. private static $_imageCounter = 0;
  42. /**
  43. * Image index
  44. *
  45. * @var int
  46. */
  47. private $_imageIndex = 0;
  48. /**
  49. * Name
  50. *
  51. * @var string
  52. */
  53. protected $_name;
  54. /**
  55. * Description
  56. *
  57. * @var string
  58. */
  59. protected $_description;
  60. /**
  61. * Worksheet
  62. *
  63. * @var PHPExcel_Worksheet
  64. */
  65. protected $_worksheet;
  66. /**
  67. * Coordinates
  68. *
  69. * @var string
  70. */
  71. protected $_coordinates;
  72. /**
  73. * Offset X
  74. *
  75. * @var int
  76. */
  77. protected $_offsetX;
  78. /**
  79. * Offset Y
  80. *
  81. * @var int
  82. */
  83. protected $_offsetY;
  84. /**
  85. * Width
  86. *
  87. * @var int
  88. */
  89. protected $_width;
  90. /**
  91. * Height
  92. *
  93. * @var int
  94. */
  95. protected $_height;
  96. /**
  97. * Proportional resize
  98. *
  99. * @var boolean
  100. */
  101. protected $_resizeProportional;
  102. /**
  103. * Rotation
  104. *
  105. * @var int
  106. */
  107. protected $_rotation;
  108. /**
  109. * Shadow
  110. *
  111. * @var PHPExcel_Worksheet_Drawing_Shadow
  112. */
  113. protected $_shadow;
  114. /**
  115. * Create a new PHPExcel_Worksheet_BaseDrawing
  116. */
  117. public function __construct()
  118. {
  119. // Initialise values
  120. $this->_name = '';
  121. $this->_description = '';
  122. $this->_worksheet = null;
  123. $this->_coordinates = 'A1';
  124. $this->_offsetX = 0;
  125. $this->_offsetY = 0;
  126. $this->_width = 0;
  127. $this->_height = 0;
  128. $this->_resizeProportional = true;
  129. $this->_rotation = 0;
  130. $this->_shadow = new PHPExcel_Worksheet_Drawing_Shadow();
  131. // Set image index
  132. self::$_imageCounter++;
  133. $this->_imageIndex = self::$_imageCounter;
  134. }
  135. /**
  136. * Get image index
  137. *
  138. * @return int
  139. */
  140. public function getImageIndex() {
  141. return $this->_imageIndex;
  142. }
  143. /**
  144. * Get Name
  145. *
  146. * @return string
  147. */
  148. public function getName() {
  149. return $this->_name;
  150. }
  151. /**
  152. * Set Name
  153. *
  154. * @param string $pValue
  155. * @return PHPExcel_Worksheet_BaseDrawing
  156. */
  157. public function setName($pValue = '') {
  158. $this->_name = $pValue;
  159. return $this;
  160. }
  161. /**
  162. * Get Description
  163. *
  164. * @return string
  165. */
  166. public function getDescription() {
  167. return $this->_description;
  168. }
  169. /**
  170. * Set Description
  171. *
  172. * @param string $pValue
  173. * @return PHPExcel_Worksheet_BaseDrawing
  174. */
  175. public function setDescription($pValue = '') {
  176. $this->_description = $pValue;
  177. return $this;
  178. }
  179. /**
  180. * Get Worksheet
  181. *
  182. * @return PHPExcel_Worksheet
  183. */
  184. public function getWorksheet() {
  185. return $this->_worksheet;
  186. }
  187. /**
  188. * Set Worksheet
  189. *
  190. * @param PHPExcel_Worksheet $pValue
  191. * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
  192. * @throws PHPExcel_Exception
  193. * @return PHPExcel_Worksheet_BaseDrawing
  194. */
  195. public function setWorksheet(PHPExcel_Worksheet $pValue = null, $pOverrideOld = false) {
  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. return $this->_coordinates;
  227. }
  228. /**
  229. * Set Coordinates
  230. *
  231. * @param string $pValue
  232. * @return PHPExcel_Worksheet_BaseDrawing
  233. */
  234. public function setCoordinates($pValue = 'A1') {
  235. $this->_coordinates = $pValue;
  236. return $this;
  237. }
  238. /**
  239. * Get OffsetX
  240. *
  241. * @return int
  242. */
  243. public function getOffsetX() {
  244. return $this->_offsetX;
  245. }
  246. /**
  247. * Set OffsetX
  248. *
  249. * @param int $pValue
  250. * @return PHPExcel_Worksheet_BaseDrawing
  251. */
  252. public function setOffsetX($pValue = 0) {
  253. $this->_offsetX = $pValue;
  254. return $this;
  255. }
  256. /**
  257. * Get OffsetY
  258. *
  259. * @return int
  260. */
  261. public function getOffsetY() {
  262. return $this->_offsetY;
  263. }
  264. /**
  265. * Set OffsetY
  266. *
  267. * @param int $pValue
  268. * @return PHPExcel_Worksheet_BaseDrawing
  269. */
  270. public function setOffsetY($pValue = 0) {
  271. $this->_offsetY = $pValue;
  272. return $this;
  273. }
  274. /**
  275. * Get Width
  276. *
  277. * @return int
  278. */
  279. public function getWidth() {
  280. return $this->_width;
  281. }
  282. /**
  283. * Set Width
  284. *
  285. * @param int $pValue
  286. * @return PHPExcel_Worksheet_BaseDrawing
  287. */
  288. public function setWidth($pValue = 0) {
  289. // Resize proportional?
  290. if ($this->_resizeProportional && $pValue != 0) {
  291. $ratio = $this->_height / $this->_width;
  292. $this->_height = round($ratio * $pValue);
  293. }
  294. // Set width
  295. $this->_width = $pValue;
  296. return $this;
  297. }
  298. /**
  299. * Get Height
  300. *
  301. * @return int
  302. */
  303. public function getHeight() {
  304. return $this->_height;
  305. }
  306. /**
  307. * Set Height
  308. *
  309. * @param int $pValue
  310. * @return PHPExcel_Worksheet_BaseDrawing
  311. */
  312. public function setHeight($pValue = 0) {
  313. // Resize proportional?
  314. if ($this->_resizeProportional && $pValue != 0) {
  315. $ratio = $this->_width / $this->_height;
  316. $this->_width = round($ratio * $pValue);
  317. }
  318. // Set height
  319. $this->_height = $pValue;
  320. return $this;
  321. }
  322. /**
  323. * Set width and height with proportional resize
  324. * Example:
  325. * <code>
  326. * $objDrawing->setResizeProportional(true);
  327. * $objDrawing->setWidthAndHeight(160,120);
  328. * </code>
  329. *
  330. * @author Vincent@luo MSN:kele_100@hotmail.com
  331. * @param int $width
  332. * @param int $height
  333. * @return PHPExcel_Worksheet_BaseDrawing
  334. */
  335. public function setWidthAndHeight($width = 0, $height = 0) {
  336. $xratio = $width / $this->_width;
  337. $yratio = $height / $this->_height;
  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. }
  347. return $this;
  348. }
  349. /**
  350. * Get ResizeProportional
  351. *
  352. * @return boolean
  353. */
  354. public function getResizeProportional() {
  355. return $this->_resizeProportional;
  356. }
  357. /**
  358. * Set ResizeProportional
  359. *
  360. * @param boolean $pValue
  361. * @return PHPExcel_Worksheet_BaseDrawing
  362. */
  363. public function setResizeProportional($pValue = true) {
  364. $this->_resizeProportional = $pValue;
  365. return $this;
  366. }
  367. /**
  368. * Get Rotation
  369. *
  370. * @return int
  371. */
  372. public function getRotation() {
  373. return $this->_rotation;
  374. }
  375. /**
  376. * Set Rotation
  377. *
  378. * @param int $pValue
  379. * @return PHPExcel_Worksheet_BaseDrawing
  380. */
  381. public function setRotation($pValue = 0) {
  382. $this->_rotation = $pValue;
  383. return $this;
  384. }
  385. /**
  386. * Get Shadow
  387. *
  388. * @return PHPExcel_Worksheet_Drawing_Shadow
  389. */
  390. public function getShadow() {
  391. return $this->_shadow;
  392. }
  393. /**
  394. * Set Shadow
  395. *
  396. * @param PHPExcel_Worksheet_Drawing_Shadow $pValue
  397. * @throws PHPExcel_Exception
  398. * @return PHPExcel_Worksheet_BaseDrawing
  399. */
  400. public function setShadow(PHPExcel_Worksheet_Drawing_Shadow $pValue = null) {
  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. return md5(
  411. $this->_name
  412. . $this->_description
  413. . $this->_worksheet->getHashCode()
  414. . $this->_coordinates
  415. . $this->_offsetX
  416. . $this->_offsetY
  417. . $this->_width
  418. . $this->_height
  419. . $this->_rotation
  420. . $this->_shadow->getHashCode()
  421. . __CLASS__
  422. );
  423. }
  424. /**
  425. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  426. */
  427. public function __clone() {
  428. $vars = get_object_vars($this);
  429. foreach ($vars as $key => $value) {
  430. if (is_object($value)) {
  431. $this->$key = clone $value;
  432. } else {
  433. $this->$key = $value;
  434. }
  435. }
  436. }
  437. }