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 GdImage;
   6  
   7  class MemoryDrawing extends BaseDrawing
   8  {
   9      // Rendering functions
  10      const RENDERING_DEFAULT = 'imagepng';
  11      const RENDERING_PNG = 'imagepng';
  12      const RENDERING_GIF = 'imagegif';
  13      const RENDERING_JPEG = 'imagejpeg';
  14  
  15      // MIME types
  16      const MIMETYPE_DEFAULT = 'image/png';
  17      const MIMETYPE_PNG = 'image/png';
  18      const MIMETYPE_GIF = 'image/gif';
  19      const MIMETYPE_JPEG = 'image/jpeg';
  20  
  21      /**
  22       * Image resource.
  23       *
  24       * @var GdImage|resource
  25       */
  26      private $imageResource;
  27  
  28      /**
  29       * Rendering function.
  30       *
  31       * @var string
  32       */
  33      private $renderingFunction;
  34  
  35      /**
  36       * Mime type.
  37       *
  38       * @var string
  39       */
  40      private $mimeType;
  41  
  42      /**
  43       * Unique name.
  44       *
  45       * @var string
  46       */
  47      private $uniqueName;
  48  
  49      /**
  50       * Create a new MemoryDrawing.
  51       */
  52      public function __construct()
  53      {
  54          // Initialise values
  55          $this->imageResource = null;
  56          $this->renderingFunction = self::RENDERING_DEFAULT;
  57          $this->mimeType = self::MIMETYPE_DEFAULT;
  58          $this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999));
  59  
  60          // Initialize parent
  61          parent::__construct();
  62      }
  63  
  64      /**
  65       * Get image resource.
  66       *
  67       * @return GdImage|resource
  68       */
  69      public function getImageResource()
  70      {
  71          return $this->imageResource;
  72      }
  73  
  74      /**
  75       * Set image resource.
  76       *
  77       * @param GdImage|resource $value
  78       *
  79       * @return $this
  80       */
  81      public function setImageResource($value)
  82      {
  83          $this->imageResource = $value;
  84  
  85          if ($this->imageResource !== null) {
  86              // Get width/height
  87              $this->width = imagesx($this->imageResource);
  88              $this->height = imagesy($this->imageResource);
  89          }
  90  
  91          return $this;
  92      }
  93  
  94      /**
  95       * Get rendering function.
  96       *
  97       * @return string
  98       */
  99      public function getRenderingFunction()
 100      {
 101          return $this->renderingFunction;
 102      }
 103  
 104      /**
 105       * Set rendering function.
 106       *
 107       * @param string $value see self::RENDERING_*
 108       *
 109       * @return $this
 110       */
 111      public function setRenderingFunction($value)
 112      {
 113          $this->renderingFunction = $value;
 114  
 115          return $this;
 116      }
 117  
 118      /**
 119       * Get mime type.
 120       *
 121       * @return string
 122       */
 123      public function getMimeType()
 124      {
 125          return $this->mimeType;
 126      }
 127  
 128      /**
 129       * Set mime type.
 130       *
 131       * @param string $value see self::MIMETYPE_*
 132       *
 133       * @return $this
 134       */
 135      public function setMimeType($value)
 136      {
 137          $this->mimeType = $value;
 138  
 139          return $this;
 140      }
 141  
 142      /**
 143       * Get indexed filename (using image index).
 144       *
 145       * @return string
 146       */
 147      public function getIndexedFilename()
 148      {
 149          $extension = strtolower($this->getMimeType());
 150          $extension = explode('/', $extension);
 151          $extension = $extension[1];
 152  
 153          return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
 154      }
 155  
 156      /**
 157       * Get hash code.
 158       *
 159       * @return string Hash code
 160       */
 161      public function getHashCode()
 162      {
 163          return md5(
 164              $this->renderingFunction .
 165              $this->mimeType .
 166              $this->uniqueName .
 167              parent::getHashCode() .
 168              __CLASS__
 169          );
 170      }
 171  }