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 GdImage;
   6  use PhpOffice\PhpSpreadsheet\Exception;
   7  
   8  class MemoryDrawing extends BaseDrawing
   9  {
  10      // Rendering functions
  11      const RENDERING_DEFAULT = 'imagepng';
  12      const RENDERING_PNG = 'imagepng';
  13      const RENDERING_GIF = 'imagegif';
  14      const RENDERING_JPEG = 'imagejpeg';
  15  
  16      // MIME types
  17      const MIMETYPE_DEFAULT = 'image/png';
  18      const MIMETYPE_PNG = 'image/png';
  19      const MIMETYPE_GIF = 'image/gif';
  20      const MIMETYPE_JPEG = 'image/jpeg';
  21  
  22      /**
  23       * Image resource.
  24       *
  25       * @var null|GdImage|resource
  26       */
  27      private $imageResource;
  28  
  29      /**
  30       * Rendering function.
  31       *
  32       * @var string
  33       */
  34      private $renderingFunction;
  35  
  36      /**
  37       * Mime type.
  38       *
  39       * @var string
  40       */
  41      private $mimeType;
  42  
  43      /**
  44       * Unique name.
  45       *
  46       * @var string
  47       */
  48      private $uniqueName;
  49  
  50      /** @var null|resource */
  51      private $alwaysNull;
  52  
  53      /**
  54       * Create a new MemoryDrawing.
  55       */
  56      public function __construct()
  57      {
  58          // Initialise values
  59          $this->renderingFunction = self::RENDERING_DEFAULT;
  60          $this->mimeType = self::MIMETYPE_DEFAULT;
  61          $this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999));
  62          $this->alwaysNull = null;
  63  
  64          // Initialize parent
  65          parent::__construct();
  66      }
  67  
  68      public function __destruct()
  69      {
  70          if ($this->imageResource) {
  71              $rslt = @imagedestroy($this->imageResource);
  72              // "Fix" for Scrutinizer
  73              $this->imageResource = $rslt ? null : $this->alwaysNull;
  74          }
  75      }
  76  
  77      public function __clone()
  78      {
  79          parent::__clone();
  80          $this->cloneResource();
  81      }
  82  
  83      private function cloneResource(): void
  84      {
  85          if (!$this->imageResource) {
  86              return;
  87          }
  88  
  89          $width = (int) imagesx($this->imageResource);
  90          $height = (int) imagesy($this->imageResource);
  91  
  92          if (imageistruecolor($this->imageResource)) {
  93              $clone = imagecreatetruecolor($width, $height);
  94              if (!$clone) {
  95                  throw new Exception('Could not clone image resource');
  96              }
  97  
  98              imagealphablending($clone, false);
  99              imagesavealpha($clone, true);
 100          } else {
 101              $clone = imagecreate($width, $height);
 102              if (!$clone) {
 103                  throw new Exception('Could not clone image resource');
 104              }
 105  
 106              // If the image has transparency...
 107              $transparent = imagecolortransparent($this->imageResource);
 108              if ($transparent >= 0) {
 109                  $rgb = imagecolorsforindex($this->imageResource, $transparent);
 110                  if (empty($rgb)) {
 111                      throw new Exception('Could not get image colors');
 112                  }
 113  
 114                  imagesavealpha($clone, true);
 115                  $color = imagecolorallocatealpha($clone, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);
 116                  if ($color === false) {
 117                      throw new Exception('Could not get image alpha color');
 118                  }
 119  
 120                  imagefill($clone, 0, 0, $color);
 121              }
 122          }
 123  
 124          //Create the Clone!!
 125          imagecopy($clone, $this->imageResource, 0, 0, 0, 0, $width, $height);
 126  
 127          $this->imageResource = $clone;
 128      }
 129  
 130      /**
 131       * Get image resource.
 132       *
 133       * @return null|GdImage|resource
 134       */
 135      public function getImageResource()
 136      {
 137          return $this->imageResource;
 138      }
 139  
 140      /**
 141       * Set image resource.
 142       *
 143       * @param GdImage|resource $value
 144       *
 145       * @return $this
 146       */
 147      public function setImageResource($value)
 148      {
 149          $this->imageResource = $value;
 150  
 151          if ($this->imageResource !== null) {
 152              // Get width/height
 153              $this->width = (int) imagesx($this->imageResource);
 154              $this->height = (int) imagesy($this->imageResource);
 155          }
 156  
 157          return $this;
 158      }
 159  
 160      /**
 161       * Get rendering function.
 162       *
 163       * @return string
 164       */
 165      public function getRenderingFunction()
 166      {
 167          return $this->renderingFunction;
 168      }
 169  
 170      /**
 171       * Set rendering function.
 172       *
 173       * @param string $value see self::RENDERING_*
 174       *
 175       * @return $this
 176       */
 177      public function setRenderingFunction($value)
 178      {
 179          $this->renderingFunction = $value;
 180  
 181          return $this;
 182      }
 183  
 184      /**
 185       * Get mime type.
 186       *
 187       * @return string
 188       */
 189      public function getMimeType()
 190      {
 191          return $this->mimeType;
 192      }
 193  
 194      /**
 195       * Set mime type.
 196       *
 197       * @param string $value see self::MIMETYPE_*
 198       *
 199       * @return $this
 200       */
 201      public function setMimeType($value)
 202      {
 203          $this->mimeType = $value;
 204  
 205          return $this;
 206      }
 207  
 208      /**
 209       * Get indexed filename (using image index).
 210       */
 211      public function getIndexedFilename(): string
 212      {
 213          $extension = strtolower($this->getMimeType());
 214          $extension = explode('/', $extension);
 215          $extension = $extension[1];
 216  
 217          return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
 218      }
 219  
 220      /**
 221       * Get hash code.
 222       *
 223       * @return string Hash code
 224       */
 225      public function getHashCode()
 226      {
 227          return md5(
 228              $this->renderingFunction .
 229              $this->mimeType .
 230              $this->uniqueName .
 231              parent::getHashCode() .
 232              __CLASS__
 233          );
 234      }
 235  }