See Release Notes
Long Term Support Release
<?php namespace PhpOffice\PhpSpreadsheet\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\Hyperlink; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\IComparable; class BaseDrawing implements IComparable {> const EDIT_AS_ABSOLUTE = 'absolute'; /** > const EDIT_AS_ONECELL = 'oneCell'; * Image counter. > const EDIT_AS_TWOCELL = 'twoCell'; * > private const VALID_EDIT_AS = [ * @var int > self::EDIT_AS_ABSOLUTE, */ > self::EDIT_AS_ONECELL, private static $imageCounter = 0; > self::EDIT_AS_TWOCELL, > ]; /** > * Image index. > /** * > * The editAs attribute, used only with two cell anchor. * @var int > * */ > * @var string private $imageIndex = 0; > */ > protected $editAs = ''; /** >* Name. * * @var string */< protected $name;> protected $name = '';/** * Description. * * @var string */< protected $description;> protected $description = '';/** * Worksheet. *< * @var Worksheet> * @var null|Worksheet*/ protected $worksheet; /** * Coordinates. * * @var string */< protected $coordinates;> protected $coordinates = 'A1';/** * Offset X. * * @var int */< protected $offsetX;> protected $offsetX = 0;/** * Offset Y. * * @var int */< protected $offsetY;> protected $offsetY = 0; > > /** > * Coordinates2. > * > * @var string > */ > protected $coordinates2 = ''; > > /** > * Offset X2. > * > * @var int > */ > protected $offsetX2 = 0; > > /** > * Offset Y2. > * > * @var int > */ > protected $offsetY2 = 0;/** * Width. * * @var int */< protected $width;> protected $width = 0;/** * Height. * * @var int */< protected $height;> protected $height = 0; > > /** > * Pixel width of image. See $width for the size the Drawing will be in the sheet. > * > * @var int > */ > protected $imageWidth = 0; > > /** > * Pixel width of image. See $height for the size the Drawing will be in the sheet. > * > * @var int > */ > protected $imageHeight = 0;/** * Proportional resize. * * @var bool */< protected $resizeProportional;> protected $resizeProportional = true;/** * Rotation. * * @var int */< protected $rotation;> protected $rotation = 0;/** * Shadow. * * @var Drawing\Shadow */ protected $shadow; /** * Image hyperlink. * * @var null|Hyperlink */ private $hyperlink; /**> * Image type. * Create a new BaseDrawing. > * */ > * @var int public function __construct() > */ { > protected $type = IMAGETYPE_UNKNOWN; // Initialise values > $this->name = ''; > /**< $this->name = ''; < $this->description = ''; < $this->worksheet = null; < $this->coordinates = 'A1'; < $this->offsetX = 0; < $this->offsetY = 0; < $this->width = 0; < $this->height = 0; < $this->resizeProportional = true; < $this->rotation = 0; < $this->shadow = new Drawing\Shadow();> $this->setShadow();// Set image index ++self::$imageCounter; $this->imageIndex = self::$imageCounter; }< /** < * Get image index. < * < * @return int < */ < public function getImageIndex()> public function getImageIndex(): int{ return $this->imageIndex; }< /** < * Get Name. < * < * @return string < */ < public function getName()> public function getName(): string{ return $this->name; }< /** < * Set Name. < * < * @param string $pValue < * < * @return BaseDrawing < */ < public function setName($pValue)> public function setName(string $name): self{< $this->name = $pValue;> $this->name = $name;return $this; }< /** < * Get Description. < * < * @return string < */ < public function getDescription()> public function getDescription(): string{ return $this->description; }< /** < * Set Description. < * < * @param string $description < * < * @return BaseDrawing < */ < public function setDescription($description)> public function setDescription(string $description): self{ $this->description = $description; return $this; }< /** < * Get Worksheet. < * < * @return Worksheet < */ < public function getWorksheet()> public function getWorksheet(): ?Worksheet{ return $this->worksheet; } /** * Set Worksheet. *< * @param Worksheet $pValue < * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet? < * < * @throws PhpSpreadsheetException < * < * @return BaseDrawing> * @param bool $overrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?*/< public function setWorksheet(Worksheet $pValue = null, $pOverrideOld = false)> public function setWorksheet(?Worksheet $worksheet = null, bool $overrideOld = false): self{ if ($this->worksheet === null) { // Add drawing to \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet< $this->worksheet = $pValue;> if ($worksheet !== null) { > $this->worksheet = $worksheet;$this->worksheet->getCell($this->coordinates); $this->worksheet->getDrawingCollection()->append($this);> }} else {< if ($pOverrideOld) {> if ($overrideOld) {// Remove drawing from old \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $iterator = $this->worksheet->getDrawingCollection()->getIterator(); while ($iterator->valid()) {< if ($iterator->current()->getHashCode() == $this->getHashCode()) { < $this->worksheet->getDrawingCollection()->offsetUnset($iterator->key());> if ($iterator->current()->getHashCode() === $this->getHashCode()) { > $this->worksheet->getDrawingCollection()->offsetUnset(/** @scrutinizer ignore-type */ $iterator->key());$this->worksheet = null; break; } } // Set new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet< $this->setWorksheet($pValue);> $this->setWorksheet($worksheet);} else { throw new PhpSpreadsheetException('A Worksheet has already been assigned. Drawings can only exist on one \\PhpOffice\\PhpSpreadsheet\\Worksheet.'); } } return $this; }< /** < * Get Coordinates. < * < * @return string < */ < public function getCoordinates()> public function getCoordinates(): string{ return $this->coordinates; }< /** < * Set Coordinates. < * < * @param string $pValue eg: 'A1' < * < * @return BaseDrawing < */ < public function setCoordinates($pValue)> public function setCoordinates(string $coordinates): self{< $this->coordinates = $pValue;> $this->coordinates = $coordinates;return $this; }< /** < * Get OffsetX. < * < * @return int < */ < public function getOffsetX()> public function getOffsetX(): int{ return $this->offsetX; }< /** < * Set OffsetX. < * < * @param int $pValue < * < * @return BaseDrawing < */ < public function setOffsetX($pValue)> public function setOffsetX(int $offsetX): self{< $this->offsetX = $pValue;> $this->offsetX = $offsetX;return $this; }< /** < * Get OffsetY. < * < * @return int < */ < public function getOffsetY()> public function getOffsetY(): int{ return $this->offsetY; }< /** < * Set OffsetY. < * < * @param int $pValue < * < * @return BaseDrawing < */ < public function setOffsetY($pValue)> public function setOffsetY(int $offsetY): self{< $this->offsetY = $pValue;> $this->offsetY = $offsetY;return $this; }< /** < * Get Width. < * < * @return int < */ < public function getWidth()> public function getCoordinates2(): string > { > return $this->coordinates2; > } > > public function setCoordinates2(string $coordinates2): self > { > $this->coordinates2 = $coordinates2; > > return $this; > } > > public function getOffsetX2(): int > { > return $this->offsetX2; > } > > public function setOffsetX2(int $offsetX2): self > { > $this->offsetX2 = $offsetX2; > > return $this; > } > > public function getOffsetY2(): int > { > return $this->offsetY2; > } > > public function setOffsetY2(int $offsetY2): self > { > $this->offsetY2 = $offsetY2; > > return $this; > } > > public function getWidth(): int{ return $this->width; }< /** < * Set Width. < * < * @param int $pValue < * < * @return BaseDrawing < */ < public function setWidth($pValue)> public function setWidth(int $width): self{ // Resize proportional?< if ($this->resizeProportional && $pValue != 0) {> if ($this->resizeProportional && $width != 0) {$ratio = $this->height / ($this->width != 0 ? $this->width : 1);< $this->height = round($ratio * $pValue);> $this->height = (int) round($ratio * $width);} // Set width< $this->width = $pValue;> $this->width = $width;return $this; }< /** < * Get Height. < * < * @return int < */ < public function getHeight()> public function getHeight(): int{ return $this->height; }< /** < * Set Height. < * < * @param int $pValue < * < * @return BaseDrawing < */ < public function setHeight($pValue)> public function setHeight(int $height): self{ // Resize proportional?< if ($this->resizeProportional && $pValue != 0) {> if ($this->resizeProportional && $height != 0) {$ratio = $this->width / ($this->height != 0 ? $this->height : 1);< $this->width = round($ratio * $pValue);> $this->width = (int) round($ratio * $height);} // Set height< $this->height = $pValue;> $this->height = $height;return $this; } /** * Set width and height with proportional resize. * * Example: * <code> * $objDrawing->setResizeProportional(true); * $objDrawing->setWidthAndHeight(160,120); * </code> * * @author Vincent@luo MSN:kele_100@hotmail.com< * < * @param int $width < * @param int $height < * < * @return BaseDrawing*/< public function setWidthAndHeight($width, $height)> public function setWidthAndHeight(int $width, int $height): self{ $xratio = $width / ($this->width != 0 ? $this->width : 1); $yratio = $height / ($this->height != 0 ? $this->height : 1); if ($this->resizeProportional && !($width == 0 || $height == 0)) { if (($xratio * $this->height) < $height) {< $this->height = ceil($xratio * $this->height);> $this->height = (int) ceil($xratio * $this->height);$this->width = $width; } else {< $this->width = ceil($yratio * $this->width);> $this->width = (int) ceil($yratio * $this->width);$this->height = $height; } } else { $this->width = $width; $this->height = $height; } return $this; }< /** < * Get ResizeProportional. < * < * @return bool < */ < public function getResizeProportional()> public function getResizeProportional(): bool{ return $this->resizeProportional; }< /** < * Set ResizeProportional. < * < * @param bool $pValue < * < * @return BaseDrawing < */ < public function setResizeProportional($pValue)> public function setResizeProportional(bool $resizeProportional): self{< $this->resizeProportional = $pValue;> $this->resizeProportional = $resizeProportional;return $this; }< /** < * Get Rotation. < * < * @return int < */ < public function getRotation()> public function getRotation(): int{ return $this->rotation; }< /** < * Set Rotation. < * < * @param int $pValue < * < * @return BaseDrawing < */ < public function setRotation($pValue)> public function setRotation(int $rotation): self{< $this->rotation = $pValue;> $this->rotation = $rotation;return $this; }< /** < * Get Shadow. < * < * @return Drawing\Shadow < */ < public function getShadow()> public function getShadow(): Drawing\Shadow{ return $this->shadow; }< /** < * Set Shadow. < * < * @param Drawing\Shadow $pValue < * < * @return BaseDrawing < */ < public function setShadow(Drawing\Shadow $pValue = null)> public function setShadow(?Drawing\Shadow $shadow = null): self{< $this->shadow = $pValue;> $this->shadow = $shadow ?? new Drawing\Shadow();return $this; } /** * Get hash code. * * @return string Hash code */ public function getHashCode() { return md5( $this->name . $this->description .< $this->worksheet->getHashCode() .> (($this->worksheet === null) ? '' : $this->worksheet->getHashCode()) .$this->coordinates . $this->offsetX . $this->offsetY .> $this->coordinates2 . $this->width . > $this->offsetX2 . $this->height . > $this->offsetY2 .$this->rotation . $this->shadow->getHashCode() . __CLASS__ ); } /** * Implement PHP __clone to create a deep clone, not just a shallow copy. */ public function __clone() { $vars = get_object_vars($this); foreach ($vars as $key => $value) { if ($key == 'worksheet') { $this->worksheet = null; } elseif (is_object($value)) { $this->$key = clone $value; } else { $this->$key = $value; } } }> public function setHyperlink(?Hyperlink $hyperlink = null): void /** > { * @param null|Hyperlink $pHyperlink > $this->hyperlink = $hyperlink; */ > } public function setHyperlink(Hyperlink $pHyperlink = null) > { > public function getHyperlink(): ?Hyperlink $this->hyperlink = $pHyperlink; > { } > return $this->hyperlink; > } /** >< * @param null|Hyperlink $pHyperlink> * Set Fact Sizes and Type of Image.< public function setHyperlink(Hyperlink $pHyperlink = null)> protected function setSizesAndType(string $path): void< $this->hyperlink = $pHyperlink;> if ($this->imageWidth === 0 && $this->imageHeight === 0 && $this->type === IMAGETYPE_UNKNOWN) { > $imageData = getimagesize($path); > > if (!empty($imageData)) { > $this->imageWidth = $imageData[0]; > $this->imageHeight = $imageData[1]; > $this->type = $imageData[2]; > } > } > if ($this->width === 0 && $this->height === 0) { > $this->width = $this->imageWidth; > $this->height = $this->imageHeight; > }< * @return null|Hyperlink> * Get Image Type.< public function getHyperlink()> public function getType(): int< return $this->hyperlink;> return $this->type; > } > > public function getImageWidth(): int > { > return $this->imageWidth; > } > > public function getImageHeight(): int > { > return $this->imageHeight; > } > > public function getEditAs(): string > { > return $this->editAs; > } > > public function setEditAs(string $editAs): self > { > $this->editAs = $editAs; > > return $this; > } > > public function validEditAs(): bool > { > return in_array($this->editAs, self::VALID_EDIT_AS, true);}