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