PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
PHP | 436 lines | 161 code | 45 blank | 230 comment | 22 complexity | 4b81e596cc822102d7e5c34349452490 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
  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_BaseDrawing
  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_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->getDrawingCollection()->append($this);
  178. } else {
  179. if ($pOverrideOld) {
  180. // Remove drawing from old PHPExcel_Worksheet
  181. $iterator = $this->_worksheet->getDrawingCollection()->getIterator();
  182. while ($iterator->valid()) {
  183. if ($iterator->current()->getHashCode() == $this->getHashCode()) {
  184. $this->_worksheet->getDrawingCollection()->offsetUnset( $iterator->key() );
  185. $this->_worksheet = null;
  186. break;
  187. }
  188. }
  189. // Set new PHPExcel_Worksheet
  190. $this->setWorksheet($pValue);
  191. } else {
  192. throw new Exception("A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.");
  193. }
  194. }
  195. }
  196. /**
  197. * Get Coordinates
  198. *
  199. * @return string
  200. */
  201. public function getCoordinates() {
  202. return $this->_coordinates;
  203. }
  204. /**
  205. * Set Coordinates
  206. *
  207. * @param string $pValue
  208. */
  209. public function setCoordinates($pValue = 'A1') {
  210. $this->_coordinates = $pValue;
  211. }
  212. /**
  213. * Get OffsetX
  214. *
  215. * @return int
  216. */
  217. public function getOffsetX() {
  218. return $this->_offsetX;
  219. }
  220. /**
  221. * Set OffsetX
  222. *
  223. * @param int $pValue
  224. */
  225. public function setOffsetX($pValue = 0) {
  226. $this->_offsetX = $pValue;
  227. }
  228. /**
  229. * Get OffsetY
  230. *
  231. * @return int
  232. */
  233. public function getOffsetY() {
  234. return $this->_offsetY;
  235. }
  236. /**
  237. * Set OffsetY
  238. *
  239. * @param int $pValue
  240. */
  241. public function setOffsetY($pValue = 0) {
  242. $this->_offsetY = $pValue;
  243. }
  244. /**
  245. * Get Width
  246. *
  247. * @return int
  248. */
  249. public function getWidth() {
  250. return $this->_width;
  251. }
  252. /**
  253. * Set Width
  254. *
  255. * @param int $pValue
  256. */
  257. public function setWidth($pValue = 0) {
  258. // Resize proportional?
  259. if ($this->_resizeProportional && $pValue != 0) {
  260. $ratio = $this->_width / $this->_height;
  261. $this->_height = round($ratio * $pValue);
  262. }
  263. // Set width
  264. $this->_width = $pValue;
  265. }
  266. /**
  267. * Get Height
  268. *
  269. * @return int
  270. */
  271. public function getHeight() {
  272. return $this->_height;
  273. }
  274. /**
  275. * Set Height
  276. *
  277. * @param int $pValue
  278. */
  279. public function setHeight($pValue = 0) {
  280. // Resize proportional?
  281. if ($this->_resizeProportional && $pValue != 0) {
  282. $ratio = $this->_width / $this->_height;
  283. $this->_width = round($ratio * $pValue);
  284. }
  285. // Set height
  286. $this->_height = $pValue;
  287. }
  288. /**
  289. * Set width and height with proportional resize
  290. * @author Vincent@luo MSN:kele_100@hotmail.com
  291. * @param int $width
  292. * @param int $height
  293. * @example $objDrawing->setResizeProportional(true);
  294. * @example $objDrawing->setWidthAndHeight(160,120);
  295. */
  296. public function setWidthAndHeight($width = 0, $height = 0) {
  297. $xratio = $width / $this->_width;
  298. $yratio = $height / $this->_height;
  299. if ($this->_resizeProportional && !($width == 0 || $height == 0)) {
  300. if (($xratio * $this->_height) < $height) {
  301. $this->_height = ceil($xratio * $this->_height);
  302. $this->_width = $width;
  303. } else {
  304. $this->_width = ceil($yratio * $this->_width);
  305. $this->_height = $height;
  306. }
  307. }
  308. }
  309. /**
  310. * Get ResizeProportional
  311. *
  312. * @return boolean
  313. */
  314. public function getResizeProportional() {
  315. return $this->_resizeProportional;
  316. }
  317. /**
  318. * Set ResizeProportional
  319. *
  320. * @param boolean $pValue
  321. */
  322. public function setResizeProportional($pValue = true) {
  323. $this->_resizeProportional = $pValue;
  324. }
  325. /**
  326. * Get Rotation
  327. *
  328. * @return int
  329. */
  330. public function getRotation() {
  331. return $this->_rotation;
  332. }
  333. /**
  334. * Set Rotation
  335. *
  336. * @param int $pValue
  337. */
  338. public function setRotation($pValue = 0) {
  339. $this->_rotation = $pValue;
  340. }
  341. /**
  342. * Get Shadow
  343. *
  344. * @return PHPExcel_Worksheet_Drawing_Shadow
  345. */
  346. public function getShadow() {
  347. return $this->_shadow;
  348. }
  349. /**
  350. * Set Shadow
  351. *
  352. * @param PHPExcel_Worksheet_Drawing_Shadow $pValue
  353. * @throws Exception
  354. */
  355. public function setShadow(PHPExcel_Worksheet_Drawing_Shadow $pValue = null) {
  356. $this->_shadow = $pValue;
  357. }
  358. /**
  359. * Get hash code
  360. *
  361. * @return string Hash code
  362. */
  363. public function getHashCode() {
  364. return md5(
  365. $this->_name
  366. . $this->_description
  367. . $this->_worksheet->getHashCode()
  368. . $this->_coordinates
  369. . $this->_offsetX
  370. . $this->_offsetY
  371. . $this->_width
  372. . $this->_height
  373. . $this->_rotation
  374. . $this->_shadow->getHashCode()
  375. . __CLASS__
  376. );
  377. }
  378. /**
  379. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  380. */
  381. public function __clone() {
  382. $vars = get_object_vars($this);
  383. foreach ($vars as $key => $value) {
  384. if (is_object($value)) {
  385. $this->$key = clone $value;
  386. } else {
  387. $this->$key = $value;
  388. }
  389. }
  390. }
  391. }