PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.3.5/Classes/PHPExcel/Worksheet/Drawing.php

#
PHP | 482 lines | 182 code | 50 blank | 250 comment | 27 complexity | 3766b158a5ddfe20fcd747060495019e MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel, Maarten Balliauw
  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 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /** PHPExcel_Worksheet */
  30. require_once 'PHPExcel/Worksheet.php';
  31. /** PHPExcel_Worksheet_Drawing_Shadow */
  32. require_once 'PHPExcel/Worksheet/Drawing/Shadow.php';
  33. /**
  34. * PHPExcel_Worksheet_Drawing
  35. *
  36. * @category PHPExcel
  37. * @package PHPExcel_Worksheet
  38. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  39. */
  40. class PHPExcel_Worksheet_Drawing implements PHPExcel_IComparable
  41. {
  42. /**
  43. * Name
  44. *
  45. * @var string
  46. */
  47. private $_name;
  48. /**
  49. * Description
  50. *
  51. * @var string
  52. */
  53. private $_description;
  54. /**
  55. * Path
  56. *
  57. * @var string
  58. */
  59. private $_path;
  60. /**
  61. * Worksheet
  62. *
  63. * @var PHPExcel_Worksheet
  64. */
  65. private $_worksheet;
  66. /**
  67. * Coordinates
  68. *
  69. * @var string
  70. */
  71. private $_coordinates;
  72. /**
  73. * Offset X
  74. *
  75. * @var int
  76. */
  77. private $_offsetX;
  78. /**
  79. * Offset Y
  80. *
  81. * @var int
  82. */
  83. private $_offsetY;
  84. /**
  85. * Width
  86. *
  87. * @var int
  88. */
  89. private $_width;
  90. /**
  91. * Height
  92. *
  93. * @var int
  94. */
  95. private $_height;
  96. /**
  97. * Proportional resize
  98. *
  99. * @var boolean
  100. */
  101. private $_resizeProportional;
  102. /**
  103. * Rotation
  104. *
  105. * @var int
  106. */
  107. private $_rotation;
  108. /**
  109. * Shadow
  110. *
  111. * @var PHPExcel_Worksheet_Drawing_Shadow
  112. */
  113. private $_shadow;
  114. /**
  115. * Create a new PHPExcel_Worksheet_Drawing
  116. */
  117. public function __construct()
  118. {
  119. // Initialise values
  120. $this->_name = '';
  121. $this->_description = '';
  122. $this->_path = '';
  123. $this->_worksheet = null;
  124. $this->_coordinates = 'A1';
  125. $this->_offsetX = 0;
  126. $this->_offsetY = 0;
  127. $this->_width = 0;
  128. $this->_height = 0;
  129. $this->_resizeProportional = true;
  130. $this->_rotation = 0;
  131. $this->_shadow = new PHPExcel_Worksheet_Drawing_Shadow();
  132. }
  133. /**
  134. * Get Name
  135. *
  136. * @return string
  137. */
  138. public function getName() {
  139. return $this->_name;
  140. }
  141. /**
  142. * Set Name
  143. *
  144. * @param string $pValue
  145. */
  146. public function setName($pValue = '') {
  147. $this->_name = $pValue;
  148. }
  149. /**
  150. * Get Filename
  151. *
  152. * @return string
  153. */
  154. public function getFilename() {
  155. return basename($this->_path);
  156. }
  157. /**
  158. * Get Extension
  159. *
  160. * @return string
  161. */
  162. public function getExtension() {
  163. return end(explode(".", basename($this->_path)));
  164. }
  165. /**
  166. * Get Description
  167. *
  168. * @return string
  169. */
  170. public function getDescription() {
  171. return $this->_description;
  172. }
  173. /**
  174. * Set Description
  175. *
  176. * @param string $pValue
  177. */
  178. public function setDescription($pValue = '') {
  179. $this->_description = $pValue;
  180. }
  181. /**
  182. * Get Path
  183. *
  184. * @return string
  185. */
  186. public function getPath() {
  187. return $this->_path;
  188. }
  189. /**
  190. * Set Path
  191. *
  192. * @param string $pValue File path
  193. * @param boolean $pVerifyFile Verify file
  194. * @throws Exception
  195. */
  196. public function setPath($pValue = '', $pVerifyFile = true) {
  197. if ($pVerifyFile) {
  198. if (file_exists($pValue)) {
  199. $this->_path = $pValue;
  200. if ($this->_width == 0 && $this->_height == 0) {
  201. // Get width/height
  202. list($this->_width, $this->_height) = getimagesize($pValue);
  203. }
  204. } else {
  205. throw new Exception("File $pValue not found!");
  206. }
  207. } else {
  208. $this->_path = $pValue;
  209. }
  210. }
  211. /**
  212. * Get Worksheet
  213. *
  214. * @return PHPExcel_Worksheet
  215. */
  216. public function getWorksheet() {
  217. return $this->_worksheet;
  218. }
  219. /**
  220. * Set Worksheet
  221. *
  222. * @param PHPExcel_Worksheet $pValue
  223. * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
  224. * @throws Exception
  225. */
  226. public function setWorksheet($pValue = null, $pOverrideOld = false) {
  227. if ($pValue instanceof PHPExcel_Worksheet) {
  228. if (is_null($this->_worksheet)) {
  229. // Add drawing to PHPExcel_Worksheet
  230. $this->_worksheet = $pValue;
  231. $this->_worksheet->getDrawingCollection()->append($this);
  232. } else {
  233. if ($pOverrideOld) {
  234. // Remove drawing from old PHPExcel_Worksheet
  235. $iterator = $this->_worksheet->getDrawingCollection()->getIterator();
  236. while ($iterator->valid()) {
  237. if ($iterator->current()->getHashCode() == $this->getHashCode()) {
  238. $this->_worksheet->getDrawingCollection()->offsetUnset( $iterator->key() );
  239. $this->_worksheet = null;
  240. break;
  241. }
  242. }
  243. // Set new PHPExcel_Worksheet
  244. $this->setWorksheet($pValue);
  245. } else {
  246. throw new Exception("A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.");
  247. }
  248. }
  249. } else {
  250. throw new Exception("Invalid PHPExcel_Worksheet passed.");
  251. }
  252. }
  253. /**
  254. * Get Coordinates
  255. *
  256. * @return string
  257. */
  258. public function getCoordinates() {
  259. return $this->_coordinates;
  260. }
  261. /**
  262. * Set Coordinates
  263. *
  264. * @param string $pValue
  265. */
  266. public function setCoordinates($pValue = 'A1') {
  267. $this->_coordinates = $pValue;
  268. }
  269. /**
  270. * Get OffsetX
  271. *
  272. * @return int
  273. */
  274. public function getOffsetX() {
  275. return $this->_offsetX;
  276. }
  277. /**
  278. * Set OffsetX
  279. *
  280. * @param int $pValue
  281. */
  282. public function setOffsetX($pValue = 0) {
  283. $this->_offsetX = $pValue;
  284. }
  285. /**
  286. * Get OffsetY
  287. *
  288. * @return int
  289. */
  290. public function getOffsetY() {
  291. return $this->_offsetY;
  292. }
  293. /**
  294. * Set OffsetY
  295. *
  296. * @param int $pValue
  297. */
  298. public function setOffsetY($pValue = 0) {
  299. $this->_offsetY = $pValue;
  300. }
  301. /**
  302. * Get Width
  303. *
  304. * @return int
  305. */
  306. public function getWidth() {
  307. return $this->_width;
  308. }
  309. /**
  310. * Set Width
  311. *
  312. * @param int $pValue
  313. */
  314. public function setWidth($pValue = 0) {
  315. // Resize proportional?
  316. if ($this->_resizeProportional && $pValue != 0) {
  317. $ratio = $this->_width / $this->_height;
  318. $this->_height = round($ratio * $pValue);
  319. }
  320. // Set width
  321. $this->_width = $pValue;
  322. }
  323. /**
  324. * Get Height
  325. *
  326. * @return int
  327. */
  328. public function getHeight() {
  329. return $this->_height;
  330. }
  331. /**
  332. * Set Height
  333. *
  334. * @param int $pValue
  335. */
  336. public function setHeight($pValue = 0) {
  337. // Resize proportional?
  338. if ($this->_resizeProportional && $pValue != 0) {
  339. $ratio = $this->_width / $this->_height;
  340. $this->_width = round($ratio * $pValue);
  341. }
  342. // Set height
  343. $this->_height = $pValue;
  344. }
  345. /**
  346. * Get ResizeProportional
  347. *
  348. * @return boolean
  349. */
  350. public function getResizeProportional() {
  351. return $this->_resizeProportional;
  352. }
  353. /**
  354. * Set ResizeProportional
  355. *
  356. * @param boolean $pValue
  357. */
  358. public function setResizeProportional($pValue = true) {
  359. $this->_resizeProportional = $pValue;
  360. }
  361. /**
  362. * Get Rotation
  363. *
  364. * @return int
  365. */
  366. public function getRotation() {
  367. return $this->_rotation;
  368. }
  369. /**
  370. * Set Rotation
  371. *
  372. * @param int $pValue
  373. */
  374. public function setRotation($pValue = 0) {
  375. $this->_rotation = $pValue;
  376. }
  377. /**
  378. * Get Shadow
  379. *
  380. * @return PHPExcel_Worksheet_Drawing_Shadow
  381. */
  382. public function getShadow() {
  383. return $this->_shadow;
  384. }
  385. /**
  386. * Set Shadow
  387. *
  388. * @param PHPExcel_Worksheet_Drawing_Shadow $pValue
  389. * @throws Exception
  390. */
  391. public function setShadow($pValue = null) {
  392. if ($pValue instanceof PHPExcel_Worksheet_Drawing_Shadow) {
  393. $this->_shadow = $pValue;
  394. } else {
  395. throw new Exception("Invalid PHPExcel_Worksheet_Drawing_Shadow passed.");
  396. }
  397. }
  398. /**
  399. * Get hash code
  400. *
  401. * @return string Hash code
  402. */
  403. public function getHashCode() {
  404. return md5(
  405. $this->_name
  406. . $this->_description
  407. . $this->_path
  408. . $this->_worksheet->getHashCode()
  409. . $this->_coordinates
  410. . $this->_offsetX
  411. . $this->_offsetY
  412. . $this->_width
  413. . $this->_height
  414. . $this->_rotation
  415. . $this->_shadow->getHashCode()
  416. . __CLASS__
  417. );
  418. }
  419. /**
  420. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  421. */
  422. public function __clone() {
  423. $vars = get_object_vars($this);
  424. foreach ($vars as $key => $value) {
  425. if (is_object($value)) {
  426. $this->$key = clone $value;
  427. } else {
  428. $this->$key = $value;
  429. }
  430. }
  431. }
  432. }