MemoryDrawing.php 3.3 KB

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