4 * PHPExcel_Worksheet_BaseDrawing
6 * Copyright (c) 2006 - 2015 PHPExcel
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @package PHPExcel_Worksheet
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
28 class PHPExcel_Worksheet_BaseDrawing
implements PHPExcel_IComparable
35 private static $imageCounter = 0;
42 private $imageIndex = 0;
56 protected $description;
61 * @var PHPExcel_Worksheet
70 protected $coordinates;
101 * Proportional resize
105 protected $resizeProportional;
117 * @var PHPExcel_Worksheet_Drawing_Shadow
122 * Create a new PHPExcel_Worksheet_BaseDrawing
124 public function __construct()
128 $this->description
= '';
129 $this->worksheet
= null;
130 $this->coordinates
= 'A1';
135 $this->resizeProportional
= true;
137 $this->shadow
= new PHPExcel_Worksheet_Drawing_Shadow();
140 self
::$imageCounter++
;
141 $this->imageIndex
= self
::$imageCounter;
149 public function getImageIndex()
151 return $this->imageIndex
;
159 public function getName()
167 * @param string $pValue
168 * @return PHPExcel_Worksheet_BaseDrawing
170 public function setName($pValue = '')
172 $this->name
= $pValue;
181 public function getDescription()
183 return $this->description
;
189 * @param string $pValue
190 * @return PHPExcel_Worksheet_BaseDrawing
192 public function setDescription($pValue = '')
194 $this->description
= $pValue;
201 * @return PHPExcel_Worksheet
203 public function getWorksheet()
205 return $this->worksheet
;
211 * @param PHPExcel_Worksheet $pValue
212 * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
213 * @throws PHPExcel_Exception
214 * @return PHPExcel_Worksheet_BaseDrawing
216 public function setWorksheet(PHPExcel_Worksheet
$pValue = null, $pOverrideOld = false)
218 if (is_null($this->worksheet
)) {
219 // Add drawing to PHPExcel_Worksheet
220 $this->worksheet
= $pValue;
221 $this->worksheet
->getCell($this->coordinates
);
222 $this->worksheet
->getDrawingCollection()->append($this);
225 // Remove drawing from old PHPExcel_Worksheet
226 $iterator = $this->worksheet
->getDrawingCollection()->getIterator();
228 while ($iterator->valid()) {
229 if ($iterator->current()->getHashCode() == $this->getHashCode()) {
230 $this->worksheet
->getDrawingCollection()->offsetUnset($iterator->key());
231 $this->worksheet
= null;
236 // Set new PHPExcel_Worksheet
237 $this->setWorksheet($pValue);
239 throw new PHPExcel_Exception("A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.");
250 public function getCoordinates()
252 return $this->coordinates
;
258 * @param string $pValue
259 * @return PHPExcel_Worksheet_BaseDrawing
261 public function setCoordinates($pValue = 'A1')
263 $this->coordinates
= $pValue;
272 public function getOffsetX()
274 return $this->offsetX
;
281 * @return PHPExcel_Worksheet_BaseDrawing
283 public function setOffsetX($pValue = 0)
285 $this->offsetX
= $pValue;
294 public function getOffsetY()
296 return $this->offsetY
;
303 * @return PHPExcel_Worksheet_BaseDrawing
305 public function setOffsetY($pValue = 0)
307 $this->offsetY
= $pValue;
316 public function getWidth()
325 * @return PHPExcel_Worksheet_BaseDrawing
327 public function setWidth($pValue = 0)
329 // Resize proportional?
330 if ($this->resizeProportional
&& $pValue != 0) {
331 $ratio = $this->height
/ ($this->width
!= 0 ?
$this->width
: 1);
332 $this->height
= round($ratio * $pValue);
336 $this->width
= $pValue;
346 public function getHeight()
348 return $this->height
;
355 * @return PHPExcel_Worksheet_BaseDrawing
357 public function setHeight($pValue = 0)
359 // Resize proportional?
360 if ($this->resizeProportional
&& $pValue != 0) {
361 $ratio = $this->width
/ ($this->height
!= 0 ?
$this->height
: 1);
362 $this->width
= round($ratio * $pValue);
366 $this->height
= $pValue;
372 * Set width and height with proportional resize
375 * $objDrawing->setResizeProportional(true);
376 * $objDrawing->setWidthAndHeight(160,120);
379 * @author Vincent@luo MSN:kele_100@hotmail.com
382 * @return PHPExcel_Worksheet_BaseDrawing
384 public function setWidthAndHeight($width = 0, $height = 0)
386 $xratio = $width / ($this->width
!= 0 ?
$this->width
: 1);
387 $yratio = $height / ($this->height
!= 0 ?
$this->height
: 1);
388 if ($this->resizeProportional
&& !($width == 0 ||
$height == 0)) {
389 if (($xratio * $this->height
) < $height) {
390 $this->height
= ceil($xratio * $this->height
);
391 $this->width
= $width;
393 $this->width
= ceil($yratio * $this->width
);
394 $this->height
= $height;
397 $this->width
= $width;
398 $this->height
= $height;
405 * Get ResizeProportional
409 public function getResizeProportional()
411 return $this->resizeProportional
;
415 * Set ResizeProportional
417 * @param boolean $pValue
418 * @return PHPExcel_Worksheet_BaseDrawing
420 public function setResizeProportional($pValue = true)
422 $this->resizeProportional
= $pValue;
431 public function getRotation()
433 return $this->rotation
;
440 * @return PHPExcel_Worksheet_BaseDrawing
442 public function setRotation($pValue = 0)
444 $this->rotation
= $pValue;
451 * @return PHPExcel_Worksheet_Drawing_Shadow
453 public function getShadow()
455 return $this->shadow
;
461 * @param PHPExcel_Worksheet_Drawing_Shadow $pValue
462 * @throws PHPExcel_Exception
463 * @return PHPExcel_Worksheet_BaseDrawing
465 public function setShadow(PHPExcel_Worksheet_Drawing_Shadow
$pValue = null)
467 $this->shadow
= $pValue;
474 * @return string Hash code
476 public function getHashCode()
481 $this->worksheet
->getHashCode() .
488 $this->shadow
->getHashCode() .
494 * Implement PHP __clone to create a deep clone, not just a shallow copy.
496 public function __clone()
498 $vars = get_object_vars($this);
499 foreach ($vars as $key => $value) {
500 if (is_object($value)) {
501 $this->$key = clone $value;
503 $this->$key = $value;