Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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