Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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