Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   6  use ZipArchive;
   7  
   8  class Drawing extends BaseDrawing
   9  {
  10      const IMAGE_TYPES_CONVERTION_MAP = [
  11          IMAGETYPE_GIF => IMAGETYPE_PNG,
  12          IMAGETYPE_JPEG => IMAGETYPE_JPEG,
  13          IMAGETYPE_PNG => IMAGETYPE_PNG,
  14          IMAGETYPE_BMP => IMAGETYPE_PNG,
  15      ];
  16  
  17      /**
  18       * Path.
  19       *
  20       * @var string
  21       */
  22      private $path;
  23  
  24      /**
  25       * Whether or not we are dealing with a URL.
  26       *
  27       * @var bool
  28       */
  29      private $isUrl;
  30  
  31      /**
  32       * Create a new Drawing.
  33       */
  34      public function __construct()
  35      {
  36          // Initialise values
  37          $this->path = '';
  38          $this->isUrl = false;
  39  
  40          // Initialize parent
  41          parent::__construct();
  42      }
  43  
  44      /**
  45       * Get Filename.
  46       *
  47       * @return string
  48       */
  49      public function getFilename()
  50      {
  51          return basename($this->path);
  52      }
  53  
  54      /**
  55       * Get indexed filename (using image index).
  56       */
  57      public function getIndexedFilename(): string
  58      {
  59          return md5($this->path) . '.' . $this->getExtension();
  60      }
  61  
  62      /**
  63       * Get Extension.
  64       *
  65       * @return string
  66       */
  67      public function getExtension()
  68      {
  69          $exploded = explode('.', basename($this->path));
  70  
  71          return $exploded[count($exploded) - 1];
  72      }
  73  
  74      /**
  75       * Get full filepath to store drawing in zip archive.
  76       *
  77       * @return string
  78       */
  79      public function getMediaFilename()
  80      {
  81          if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
  82              throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
  83          }
  84  
  85          return sprintf('image%d%s', $this->getImageIndex(), $this->getImageFileExtensionForSave());
  86      }
  87  
  88      /**
  89       * Get Path.
  90       *
  91       * @return string
  92       */
  93      public function getPath()
  94      {
  95          return $this->path;
  96      }
  97  
  98      /**
  99       * Set Path.
 100       *
 101       * @param string $path File path
 102       * @param bool $verifyFile Verify file
 103       * @param ZipArchive $zip Zip archive instance
 104       *
 105       * @return $this
 106       */
 107      public function setPath($path, $verifyFile = true, $zip = null)
 108      {
 109          if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) {
 110              // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
 111              if (filter_var($path, FILTER_VALIDATE_URL)) {
 112                  $this->path = $path;
 113                  // Implicit that it is a URL, rather store info than running check above on value in other places.
 114                  $this->isUrl = true;
 115                  $imageContents = file_get_contents($path);
 116                  $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
 117                  if ($filePath) {
 118                      file_put_contents($filePath, $imageContents);
 119                      if (file_exists($filePath)) {
 120                          $this->setSizesAndType($filePath);
 121                          unlink($filePath);
 122                      }
 123                  }
 124              } elseif (file_exists($path)) {
 125                  $this->path = $path;
 126                  $this->setSizesAndType($path);
 127              } elseif ($zip instanceof ZipArchive) {
 128                  $zipPath = explode('#', $path)[1];
 129                  if ($zip->locateName($zipPath) !== false) {
 130                      $this->path = $path;
 131                      $this->setSizesAndType($path);
 132                  }
 133              } else {
 134                  throw new PhpSpreadsheetException("File $path not found!");
 135              }
 136          } else {
 137              $this->path = $path;
 138          }
 139  
 140          return $this;
 141      }
 142  
 143      /**
 144       * Get isURL.
 145       */
 146      public function getIsURL(): bool
 147      {
 148          return $this->isUrl;
 149      }
 150  
 151      /**
 152       * Set isURL.
 153       *
 154       * @return $this
 155       */
 156      public function setIsURL(bool $isUrl): self
 157      {
 158          $this->isUrl = $isUrl;
 159  
 160          return $this;
 161      }
 162  
 163      /**
 164       * Get hash code.
 165       *
 166       * @return string Hash code
 167       */
 168      public function getHashCode()
 169      {
 170          return md5(
 171              $this->path .
 172              parent::getHashCode() .
 173              __CLASS__
 174          );
 175      }
 176  
 177      /**
 178       * Get Image Type for Save.
 179       */
 180      public function getImageTypeForSave(): int
 181      {
 182          if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
 183              throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
 184          }
 185  
 186          return self::IMAGE_TYPES_CONVERTION_MAP[$this->type];
 187      }
 188  
 189      /**
 190       * Get Image file extention for Save.
 191       */
 192      public function getImageFileExtensionForSave(bool $includeDot = true): string
 193      {
 194          if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
 195              throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
 196          }
 197  
 198          $result = image_type_to_extension(self::IMAGE_TYPES_CONVERTION_MAP[$this->type], $includeDot);
 199  
 200          return is_string($result) ? $result : '';
 201      }
 202  
 203      /**
 204       * Get Image mime type.
 205       */
 206      public function getImageMimeType(): string
 207      {
 208          if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
 209              throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
 210          }
 211  
 212          return image_type_to_mime_type(self::IMAGE_TYPES_CONVERTION_MAP[$this->type]);
 213      }
 214  }