| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Worksheet; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Hyperlink; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\IComparable; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Drawing\Shadow; |
| 9 |
use SimpleXMLElement; |
| 10 |
|
| 11 |
class BaseDrawing implements IComparable |
| 12 |
{ |
| 13 |
const EDIT_AS_ABSOLUTE = 'absolute'; |
| 14 |
const EDIT_AS_ONECELL = 'oneCell'; |
| 15 |
const EDIT_AS_TWOCELL = 'twoCell'; |
| 16 |
private const VALID_EDIT_AS = [ |
| 17 |
self::EDIT_AS_ABSOLUTE, |
| 18 |
self::EDIT_AS_ONECELL, |
| 19 |
self::EDIT_AS_TWOCELL, |
| 20 |
]; |
| 21 |
|
| 22 |
/** |
| 23 |
* The editAs attribute, used only with two cell anchor. |
| 24 |
*/ |
| 25 |
protected string $editAs = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Image counter. |
| 29 |
*/ |
| 30 |
private static int $imageCounter = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Image index. |
| 34 |
*/ |
| 35 |
private int $imageIndex; |
| 36 |
|
| 37 |
/** |
| 38 |
* Name. |
| 39 |
*/ |
| 40 |
protected string $name = ''; |
| 41 |
|
| 42 |
/** |
| 43 |
* Description. |
| 44 |
*/ |
| 45 |
protected string $description = ''; |
| 46 |
|
| 47 |
/** |
| 48 |
* Worksheet. |
| 49 |
*/ |
| 50 |
protected ?Worksheet $worksheet = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* Coordinates. |
| 54 |
*/ |
| 55 |
protected string $coordinates = 'A1'; |
| 56 |
|
| 57 |
/** |
| 58 |
* Offset X. |
| 59 |
*/ |
| 60 |
protected int $offsetX = 0; |
| 61 |
|
| 62 |
/** |
| 63 |
* Offset Y. |
| 64 |
*/ |
| 65 |
protected int $offsetY = 0; |
| 66 |
|
| 67 |
/** |
| 68 |
* Coordinates2. |
| 69 |
*/ |
| 70 |
protected string $coordinates2 = ''; |
| 71 |
|
| 72 |
/** |
| 73 |
* Offset X2. |
| 74 |
*/ |
| 75 |
protected int $offsetX2 = 0; |
| 76 |
|
| 77 |
/** |
| 78 |
* Offset Y2. |
| 79 |
*/ |
| 80 |
protected int $offsetY2 = 0; |
| 81 |
|
| 82 |
/** |
| 83 |
* Width. |
| 84 |
*/ |
| 85 |
protected int $width = 0; |
| 86 |
|
| 87 |
/** |
| 88 |
* Height. |
| 89 |
*/ |
| 90 |
protected int $height = 0; |
| 91 |
|
| 92 |
/** |
| 93 |
* Pixel width of image. See $width for the size the Drawing will be in the sheet. |
| 94 |
*/ |
| 95 |
protected int $imageWidth = 0; |
| 96 |
|
| 97 |
/** |
| 98 |
* Pixel width of image. See $height for the size the Drawing will be in the sheet. |
| 99 |
*/ |
| 100 |
protected int $imageHeight = 0; |
| 101 |
|
| 102 |
/** |
| 103 |
* Proportional resize. |
| 104 |
*/ |
| 105 |
protected bool $resizeProportional = true; |
| 106 |
|
| 107 |
/** |
| 108 |
* Rotation. |
| 109 |
*/ |
| 110 |
protected int $rotation = 0; |
| 111 |
|
| 112 |
protected bool $flipVertical = false; |
| 113 |
|
| 114 |
protected bool $flipHorizontal = false; |
| 115 |
|
| 116 |
/** |
| 117 |
* Shadow. |
| 118 |
*/ |
| 119 |
protected Shadow $shadow; |
| 120 |
|
| 121 |
/** |
| 122 |
* Image hyperlink. |
| 123 |
*/ |
| 124 |
private ?Hyperlink $hyperlink = null; |
| 125 |
|
| 126 |
/** |
| 127 |
* Image type. |
| 128 |
*/ |
| 129 |
protected int $type = IMAGETYPE_UNKNOWN; |
| 130 |
|
| 131 |
/** @var null|SimpleXMLElement|string[] */ |
| 132 |
protected $srcRect = []; |
| 133 |
|
| 134 |
/** |
| 135 |
* Percentage multiplied by 100,000, e.g. 40% = 40,000. |
| 136 |
* Opacity=x is the same as transparency=100000-x. |
| 137 |
*/ |
| 138 |
protected ?int $opacity = null; |
| 139 |
|
| 140 |
protected bool $inCell = false; |
| 141 |
|
| 142 |
protected int $index = 0; |
| 143 |
|
| 144 |
/** |
| 145 |
* Create a new BaseDrawing. |
| 146 |
*/ |
| 147 |
public function __construct() |
| 148 |
{ |
| 149 |
// Initialise values |
| 150 |
$this->setShadow(); |
| 151 |
|
| 152 |
// Set image index |
| 153 |
++self::$imageCounter; |
| 154 |
$this->imageIndex = self::$imageCounter; |
| 155 |
} |
| 156 |
|
| 157 |
public function __destruct() |
| 158 |
{ |
| 159 |
$this->worksheet = null; |
| 160 |
} |
| 161 |
|
| 162 |
public function getImageIndex(): int |
| 163 |
{ |
| 164 |
return $this->imageIndex; |
| 165 |
} |
| 166 |
|
| 167 |
public function getName(): string |
| 168 |
{ |
| 169 |
return $this->name; |
| 170 |
} |
| 171 |
|
| 172 |
public function setName(string $name): self |
| 173 |
{ |
| 174 |
$this->name = $name; |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
public function getDescription(): string |
| 180 |
{ |
| 181 |
return $this->description; |
| 182 |
} |
| 183 |
|
| 184 |
public function setDescription(string $description): self |
| 185 |
{ |
| 186 |
$this->description = $description; |
| 187 |
|
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
public function getWorksheet(): ?Worksheet |
| 192 |
{ |
| 193 |
return $this->worksheet; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Set Worksheet. |
| 198 |
* |
| 199 |
* @param bool $overrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet? |
| 200 |
*/ |
| 201 |
public function setWorksheet(?Worksheet $worksheet = null, bool $overrideOld = false): self |
| 202 |
{ |
| 203 |
if ($this->worksheet === null) { |
| 204 |
// Add drawing to Worksheet |
| 205 |
if ($worksheet !== null) { |
| 206 |
$this->worksheet = $worksheet; |
| 207 |
if (!($this instanceof Drawing && $this->getPath() === '')) { |
| 208 |
$this->worksheet->getCell($this->coordinates); |
| 209 |
} |
| 210 |
if ($this->inCell) { |
| 211 |
$this->worksheet->getInCellDrawingCollection() |
| 212 |
->append($this); |
| 213 |
} else { |
| 214 |
$this->worksheet->getDrawingCollection() |
| 215 |
->append($this); |
| 216 |
} |
| 217 |
} |
| 218 |
} else { |
| 219 |
if ($overrideOld) { |
| 220 |
// Remove drawing from old Worksheet |
| 221 |
$collections = [ |
| 222 |
$this->worksheet->getDrawingCollection(), |
| 223 |
$this->worksheet->getInCellDrawingCollection(), |
| 224 |
]; |
| 225 |
|
| 226 |
foreach ($collections as $collection) { |
| 227 |
foreach ($collection as $key => $drawing) { |
| 228 |
if ($drawing->getHashCode() === $this->getHashCode()) { |
| 229 |
$collection->offsetUnset($key); |
| 230 |
$this->worksheet = null; |
| 231 |
|
| 232 |
break 2; // break both loops |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Set new Worksheet |
| 238 |
$this->setWorksheet($worksheet); |
| 239 |
} else { |
| 240 |
throw new PhpSpreadsheetException('A Worksheet has already been assigned. Drawings can only exist on one Worksheet.'); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return $this; |
| 245 |
} |
| 246 |
|
| 247 |
public function getCoordinates(): string |
| 248 |
{ |
| 249 |
return $this->coordinates; |
| 250 |
} |
| 251 |
|
| 252 |
public function setCoordinates(string $coordinates): self |
| 253 |
{ |
| 254 |
$this->coordinates = $coordinates; |
| 255 |
if ($this->worksheet !== null) { |
| 256 |
if (!($this instanceof Drawing && $this->getPath() === '')) { |
| 257 |
$this->worksheet->getCell($this->coordinates); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return $this; |
| 262 |
} |
| 263 |
|
| 264 |
public function getOffsetX(): int |
| 265 |
{ |
| 266 |
return $this->offsetX; |
| 267 |
} |
| 268 |
|
| 269 |
public function setOffsetX(int $offsetX): self |
| 270 |
{ |
| 271 |
$this->offsetX = $offsetX; |
| 272 |
|
| 273 |
return $this; |
| 274 |
} |
| 275 |
|
| 276 |
public function getOffsetY(): int |
| 277 |
{ |
| 278 |
return $this->offsetY; |
| 279 |
} |
| 280 |
|
| 281 |
public function setOffsetY(int $offsetY): self |
| 282 |
{ |
| 283 |
$this->offsetY = $offsetY; |
| 284 |
|
| 285 |
return $this; |
| 286 |
} |
| 287 |
|
| 288 |
public function getCoordinates2(): string |
| 289 |
{ |
| 290 |
return $this->coordinates2; |
| 291 |
} |
| 292 |
|
| 293 |
public function setCoordinates2(string $coordinates2): self |
| 294 |
{ |
| 295 |
$this->coordinates2 = $coordinates2; |
| 296 |
|
| 297 |
return $this; |
| 298 |
} |
| 299 |
|
| 300 |
public function getOffsetX2(): int |
| 301 |
{ |
| 302 |
return $this->offsetX2; |
| 303 |
} |
| 304 |
|
| 305 |
public function setOffsetX2(int $offsetX2): self |
| 306 |
{ |
| 307 |
$this->offsetX2 = $offsetX2; |
| 308 |
|
| 309 |
return $this; |
| 310 |
} |
| 311 |
|
| 312 |
public function getOffsetY2(): int |
| 313 |
{ |
| 314 |
return $this->offsetY2; |
| 315 |
} |
| 316 |
|
| 317 |
public function setOffsetY2(int $offsetY2): self |
| 318 |
{ |
| 319 |
$this->offsetY2 = $offsetY2; |
| 320 |
|
| 321 |
return $this; |
| 322 |
} |
| 323 |
|
| 324 |
public function getWidth(): int |
| 325 |
{ |
| 326 |
return $this->width; |
| 327 |
} |
| 328 |
|
| 329 |
public function setWidth(int $width): self |
| 330 |
{ |
| 331 |
// Resize proportional? |
| 332 |
if ($this->resizeProportional && $width != 0) { |
| 333 |
$ratio = $this->height / ($this->width != 0 ? $this->width : 1); |
| 334 |
$this->height = (int) round($ratio * $width); |
| 335 |
} |
| 336 |
|
| 337 |
// Set width |
| 338 |
$this->width = $width; |
| 339 |
|
| 340 |
return $this; |
| 341 |
} |
| 342 |
|
| 343 |
public function getHeight(): int |
| 344 |
{ |
| 345 |
return $this->height; |
| 346 |
} |
| 347 |
|
| 348 |
public function setHeight(int $height): self |
| 349 |
{ |
| 350 |
// Resize proportional? |
| 351 |
if ($this->resizeProportional && $height != 0) { |
| 352 |
$ratio = $this->width / ($this->height != 0 ? $this->height : 1); |
| 353 |
$this->width = (int) round($ratio * $height); |
| 354 |
} |
| 355 |
|
| 356 |
// Set height |
| 357 |
$this->height = $height; |
| 358 |
|
| 359 |
return $this; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Set width and height with proportional resize. |
| 364 |
* |
| 365 |
* Example: |
| 366 |
* <code> |
| 367 |
* $objDrawing->setResizeProportional(true); |
| 368 |
* $objDrawing->setWidthAndHeight(160,120); |
| 369 |
* </code> |
| 370 |
* |
| 371 |
* @author Vincent@luo MSN:[email protected] |
| 372 |
*/ |
| 373 |
public function setWidthAndHeight(int $width, int $height): self |
| 374 |
{ |
| 375 |
if ($this->width === 0 || $this->height === 0 || $width === 0 || $height === 0 || !$this->resizeProportional) { |
| 376 |
$this->width = $width; |
| 377 |
$this->height = $height; |
| 378 |
} else { |
| 379 |
$xratio = $width / $this->width; |
| 380 |
$yratio = $height / $this->height; |
| 381 |
if (($xratio * $this->height) < $height) { |
| 382 |
$this->height = (int) ceil($xratio * $this->height); |
| 383 |
$this->width = $width; |
| 384 |
} else { |
| 385 |
$this->width = (int) ceil($yratio * $this->width); |
| 386 |
$this->height = $height; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return $this; |
| 391 |
} |
| 392 |
|
| 393 |
public function getResizeProportional(): bool |
| 394 |
{ |
| 395 |
return $this->resizeProportional; |
| 396 |
} |
| 397 |
|
| 398 |
public function setResizeProportional(bool $resizeProportional): self |
| 399 |
{ |
| 400 |
$this->resizeProportional = $resizeProportional; |
| 401 |
|
| 402 |
return $this; |
| 403 |
} |
| 404 |
|
| 405 |
public function getRotation(): int |
| 406 |
{ |
| 407 |
return $this->rotation; |
| 408 |
} |
| 409 |
|
| 410 |
public function setRotation(int $rotation): self |
| 411 |
{ |
| 412 |
$this->rotation = $rotation; |
| 413 |
|
| 414 |
return $this; |
| 415 |
} |
| 416 |
|
| 417 |
public function getShadow(): Shadow |
| 418 |
{ |
| 419 |
return $this->shadow; |
| 420 |
} |
| 421 |
|
| 422 |
public function setShadow(?Shadow $shadow = null): self |
| 423 |
{ |
| 424 |
$this->shadow = $shadow ?? new Shadow(); |
| 425 |
|
| 426 |
return $this; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Get hash code. |
| 431 |
* |
| 432 |
* @return string Hash code |
| 433 |
*/ |
| 434 |
public function getHashCode(): string |
| 435 |
{ |
| 436 |
return md5( |
| 437 |
$this->name |
| 438 |
. $this->description |
| 439 |
. (($this->worksheet === null) ? '' : (string) spl_object_id($this->worksheet)) |
| 440 |
. $this->coordinates |
| 441 |
. $this->offsetX |
| 442 |
. $this->offsetY |
| 443 |
. $this->coordinates2 |
| 444 |
. $this->offsetX2 |
| 445 |
. $this->offsetY2 |
| 446 |
. $this->width |
| 447 |
. $this->height |
| 448 |
. $this->rotation |
| 449 |
. $this->shadow->getHashCode() |
| 450 |
. __CLASS__ |
| 451 |
); |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
| 456 |
*/ |
| 457 |
public function __clone() |
| 458 |
{ |
| 459 |
$vars = get_object_vars($this); |
| 460 |
foreach ($vars as $key => $value) { |
| 461 |
if ($key == 'worksheet') { |
| 462 |
$this->worksheet = null; |
| 463 |
} elseif (is_object($value)) { |
| 464 |
$this->$key = clone $value; |
| 465 |
} else { |
| 466 |
$this->$key = $value; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
public function setHyperlink(?Hyperlink $hyperlink = null): void |
| 472 |
{ |
| 473 |
$this->hyperlink = $hyperlink; |
| 474 |
} |
| 475 |
|
| 476 |
public function getHyperlink(): ?Hyperlink |
| 477 |
{ |
| 478 |
return $this->hyperlink; |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Set Fact Sizes and Type of Image. |
| 483 |
*/ |
| 484 |
protected function setSizesAndType(string $path): void |
| 485 |
{ |
| 486 |
if ($this->imageWidth === 0 && $this->imageHeight === 0 && $this->type === IMAGETYPE_UNKNOWN) { |
| 487 |
$imageData = getimagesize($path); |
| 488 |
|
| 489 |
if (!empty($imageData)) { |
| 490 |
$this->imageWidth = $imageData[0]; |
| 491 |
$this->imageHeight = $imageData[1]; |
| 492 |
$this->type = $imageData[2]; |
| 493 |
} |
| 494 |
} |
| 495 |
if ($this->width === 0 && $this->height === 0) { |
| 496 |
$this->width = $this->imageWidth; |
| 497 |
$this->height = $this->imageHeight; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Get Image Type. |
| 503 |
*/ |
| 504 |
public function getType(): int |
| 505 |
{ |
| 506 |
return $this->type; |
| 507 |
} |
| 508 |
|
| 509 |
public function getImageWidth(): int |
| 510 |
{ |
| 511 |
return $this->imageWidth; |
| 512 |
} |
| 513 |
|
| 514 |
public function getImageHeight(): int |
| 515 |
{ |
| 516 |
return $this->imageHeight; |
| 517 |
} |
| 518 |
|
| 519 |
public function getEditAs(): string |
| 520 |
{ |
| 521 |
return $this->editAs; |
| 522 |
} |
| 523 |
|
| 524 |
public function setEditAs(string $editAs): self |
| 525 |
{ |
| 526 |
$this->editAs = $editAs; |
| 527 |
|
| 528 |
return $this; |
| 529 |
} |
| 530 |
|
| 531 |
public function validEditAs(): bool |
| 532 |
{ |
| 533 |
return in_array($this->editAs, self::VALID_EDIT_AS, true); |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* @return null|SimpleXMLElement|string[] |
| 538 |
*/ |
| 539 |
public function getSrcRect() |
| 540 |
{ |
| 541 |
return $this->srcRect; |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* @param null|SimpleXMLElement|string[] $srcRect |
| 546 |
*/ |
| 547 |
public function setSrcRect($srcRect): self |
| 548 |
{ |
| 549 |
$this->srcRect = $srcRect; |
| 550 |
|
| 551 |
return $this; |
| 552 |
} |
| 553 |
|
| 554 |
public function setFlipHorizontal(bool $flipHorizontal): self |
| 555 |
{ |
| 556 |
$this->flipHorizontal = $flipHorizontal; |
| 557 |
|
| 558 |
return $this; |
| 559 |
} |
| 560 |
|
| 561 |
public function getFlipHorizontal(): bool |
| 562 |
{ |
| 563 |
return $this->flipHorizontal; |
| 564 |
} |
| 565 |
|
| 566 |
public function setFlipVertical(bool $flipVertical): self |
| 567 |
{ |
| 568 |
$this->flipVertical = $flipVertical; |
| 569 |
|
| 570 |
return $this; |
| 571 |
} |
| 572 |
|
| 573 |
public function getFlipVertical(): bool |
| 574 |
{ |
| 575 |
return $this->flipVertical; |
| 576 |
} |
| 577 |
|
| 578 |
public function setOpacity(?int $opacity): self |
| 579 |
{ |
| 580 |
$this->opacity = $opacity; |
| 581 |
|
| 582 |
return $this; |
| 583 |
} |
| 584 |
|
| 585 |
public function getOpacity(): ?int |
| 586 |
{ |
| 587 |
return $this->opacity; |
| 588 |
} |
| 589 |
|
| 590 |
public function setInCell(bool $inCell): self |
| 591 |
{ |
| 592 |
$this->inCell = $inCell; |
| 593 |
|
| 594 |
return $this; |
| 595 |
} |
| 596 |
|
| 597 |
public function isInCell(): ?bool |
| 598 |
{ |
| 599 |
return $this->inCell; |
| 600 |
} |
| 601 |
|
| 602 |
public function setIndex(int $index): self |
| 603 |
{ |
| 604 |
$this->index = $index; |
| 605 |
|
| 606 |
return $this; |
| 607 |
} |
| 608 |
|
| 609 |
public function getIndex(): int |
| 610 |
{ |
| 611 |
return $this->index; |
| 612 |
} |
| 613 |
} |
| 614 |
|