PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.6.1/Classes/PHPExcel/Worksheet/BaseDrawing.php

#
PHP | 437 lines | 162 code | 45 blank | 230 comment | 22 complexity | db96341605b5dd742ed0d1ce128168fd 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 - 2008 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 - 2008 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. /** 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_BaseDrawing
  35. *
  36. * @category PHPExcel
  37. * @package PHPExcel_Worksheet
  38. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  39. */
  40. class PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  41. {
  42. /**
  43. * Name
  44. *
  45. * @var string
  46. */
  47. protected $_name;
  48. /**
  49. * Description
  50. *
  51. * @var string
  52. */
  53. protected $_description;
  54. /**
  55. * Worksheet
  56. *
  57. * @var PHPExcel_Worksheet
  58. */
  59. protected $_worksheet;
  60. /**
  61. * Coordinates
  62. *
  63. * @var string
  64. */
  65. protected $_coordinates;
  66. /**
  67. * Offset X
  68. *
  69. * @var int
  70. */
  71. protected $_offsetX;
  72. /**
  73. * Offset Y
  74. *
  75. * @var int
  76. */
  77. protected $_offsetY;
  78. /**
  79. * Width
  80. *
  81. * @var int
  82. */
  83. protected $_width;
  84. /**
  85. * Height
  86. *
  87. * @var int
  88. */
  89. protected $_height;
  90. /**
  91. * Proportional resize
  92. *
  93. * @var boolean
  94. */
  95. protected $_resizeProportional;
  96. /**
  97. * Rotation
  98. *
  99. * @var int
  100. */
  101. protected $_rotation;
  102. /**
  103. * Shadow
  104. *
  105. * @var PHPExcel_Worksheet_Drawing_Shadow
  106. */
  107. protected $_shadow;
  108. /**
  109. * Create a new PHPExcel_Worksheet_BaseDrawing
  110. */
  111. public function __construct()
  112. {
  113. // Initialise values
  114. $this->_name = '';
  115. $this->_description = '';
  116. $this->_worksheet = null;
  117. $this->_coordinates = 'A1';
  118. $this->_offsetX = 0;
  119. $this->_offsetY = 0;
  120. $this->_width = 0;
  121. $this->_height = 0;
  122. $this->_resizeProportional = true;
  123. $this->_rotation = 0;
  124. $this->_shadow = new PHPExcel_Worksheet_Drawing_Shadow();
  125. }
  126. /**
  127. * Get Name
  128. *
  129. * @return string
  130. */
  131. public function getName() {
  132. return $this->_name;
  133. }
  134. /**
  135. * Set Name
  136. *
  137. * @param string $pValue
  138. */
  139. public function setName($pValue = '') {
  140. $this->_name = $pValue;
  141. }
  142. /**
  143. * Get Description
  144. *
  145. * @return string
  146. */
  147. public function getDescription() {
  148. return $this->_description;
  149. }
  150. /**
  151. * Set Description
  152. *
  153. * @param string $pValue
  154. */
  155. public function setDescription($pValue = '') {
  156. $this->_description = $pValue;
  157. }
  158. /**
  159. * Get Worksheet
  160. *
  161. * @return PHPExcel_Worksheet
  162. */
  163. public function getWorksheet() {
  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 Exception
  172. */
  173. public function setWorksheet(PHPExcel_Worksheet $pValue = null, $pOverrideOld = false) {
  174. if (is_null($this->_worksheet)) {
  175. // Add drawing to PHPExcel_Worksheet
  176. $this->_worksheet = $pValue;
  177. $this->_worksheet->getCell($this->_coordinates);
  178. $this->_worksheet->getDrawingCollection()->append($this);
  179. } else {
  180. if ($pOverrideOld) {
  181. // Remove drawing from old PHPExcel_Worksheet
  182. $iterator = $this->_worksheet->getDrawingCollection()->getIterator();
  183. while ($iterator->valid()) {
  184. if ($iterator->current()->getHashCode() == $this->getHashCode()) {
  185. $this->_worksheet->getDrawingCollection()->offsetUnset( $iterator->key() );
  186. $this->_worksheet = null;
  187. break;
  188. }
  189. }
  190. // Set new PHPExcel_Worksheet
  191. $this->setWorksheet($pValue);
  192. } else {
  193. throw new Exception("A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.");
  194. }
  195. }
  196. }
  197. /**
  198. * Get Coordinates
  199. *
  200. * @return string
  201. */
  202. public function getCoordinates() {
  203. return $this->_coordinates;
  204. }
  205. /**
  206. * Set Coordinates
  207. *
  208. * @param string $pValue
  209. */
  210. public function setCoordinates($pValue = 'A1') {
  211. $this->_coordinates = $pValue;
  212. }
  213. /**
  214. * Get OffsetX
  215. *
  216. * @return int
  217. */
  218. public function getOffsetX() {
  219. return $this->_offsetX;
  220. }
  221. /**
  222. * Set OffsetX
  223. *
  224. * @param int $pValue
  225. */
  226. public function setOffsetX($pValue = 0) {
  227. $this->_offsetX = $pValue;
  228. }
  229. /**
  230. * Get OffsetY
  231. *
  232. * @return int
  233. */
  234. public function getOffsetY() {
  235. return $this->_offsetY;
  236. }
  237. /**
  238. * Set OffsetY
  239. *
  240. * @param int $pValue
  241. */
  242. public function setOffsetY($pValue = 0) {
  243. $this->_offsetY = $pValue;
  244. }
  245. /**
  246. * Get Width
  247. *
  248. * @return int
  249. */
  250. public function getWidth() {
  251. return $this->_width;
  252. }
  253. /**
  254. * Set Width
  255. *
  256. * @param int $pValue
  257. */
  258. public function setWidth($pValue = 0) {
  259. // Resize proportional?
  260. if ($this->_resizeProportional && $pValue != 0) {
  261. $ratio = $this->_width / $this->_height;
  262. $this->_height = round($ratio * $pValue);
  263. }
  264. // Set width
  265. $this->_width = $pValue;
  266. }
  267. /**
  268. * Get Height
  269. *
  270. * @return int
  271. */
  272. public function getHeight() {
  273. return $this->_height;
  274. }
  275. /**
  276. * Set Height
  277. *
  278. * @param int $pValue
  279. */
  280. public function setHeight($pValue = 0) {
  281. // Resize proportional?
  282. if ($this->_resizeProportional && $pValue != 0) {
  283. $ratio = $this->_width / $this->_height;
  284. $this->_width = round($ratio * $pValue);
  285. }
  286. // Set height
  287. $this->_height = $pValue;
  288. }
  289. /**
  290. * Set width and height with proportional resize
  291. * @author Vincent@luo MSN:kele_100@hotmail.com
  292. * @param int $width
  293. * @param int $height
  294. * @example $objDrawing->setResizeProportional(true);
  295. * @example $objDrawing->setWidthAndHeight(160,120);
  296. */
  297. public function setWidthAndHeight($width = 0, $height = 0) {
  298. $xratio = $width / $this->_width;
  299. $yratio = $height / $this->_height;
  300. if ($this->_resizeProportional && !($width == 0 || $height == 0)) {
  301. if (($xratio * $this->_height) < $height) {
  302. $this->_height = ceil($xratio * $this->_height);
  303. $this->_width = $width;
  304. } else {
  305. $this->_width = ceil($yratio * $this->_width);
  306. $this->_height = $height;
  307. }
  308. }
  309. }
  310. /**
  311. * Get ResizeProportional
  312. *
  313. * @return boolean
  314. */
  315. public function getResizeProportional() {
  316. return $this->_resizeProportional;
  317. }
  318. /**
  319. * Set ResizeProportional
  320. *
  321. * @param boolean $pValue
  322. */
  323. public function setResizeProportional($pValue = true) {
  324. $this->_resizeProportional = $pValue;
  325. }
  326. /**
  327. * Get Rotation
  328. *
  329. * @return int
  330. */
  331. public function getRotation() {
  332. return $this->_rotation;
  333. }
  334. /**
  335. * Set Rotation
  336. *
  337. * @param int $pValue
  338. */
  339. public function setRotation($pValue = 0) {
  340. $this->_rotation = $pValue;
  341. }
  342. /**
  343. * Get Shadow
  344. *
  345. * @return PHPExcel_Worksheet_Drawing_Shadow
  346. */
  347. public function getShadow() {
  348. return $this->_shadow;
  349. }
  350. /**
  351. * Set Shadow
  352. *
  353. * @param PHPExcel_Worksheet_Drawing_Shadow $pValue
  354. * @throws Exception
  355. */
  356. public function setShadow(PHPExcel_Worksheet_Drawing_Shadow $pValue = null) {
  357. $this->_shadow = $pValue;
  358. }
  359. /**
  360. * Get hash code
  361. *
  362. * @return string Hash code
  363. */
  364. public function getHashCode() {
  365. return md5(
  366. $this->_name
  367. . $this->_description
  368. . $this->_worksheet->getHashCode()
  369. . $this->_coordinates
  370. . $this->_offsetX
  371. . $this->_offsetY
  372. . $this->_width
  373. . $this->_height
  374. . $this->_rotation
  375. . $this->_shadow->getHashCode()
  376. . __CLASS__
  377. );
  378. }
  379. /**
  380. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  381. */
  382. public function __clone() {
  383. $vars = get_object_vars($this);
  384. foreach ($vars as $key => $value) {
  385. if (is_object($value)) {
  386. $this->$key = clone $value;
  387. } else {
  388. $this->$key = $value;
  389. }
  390. }
  391. }
  392. }