MemoryDrawing.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Worksheet_MemoryDrawing extends PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  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. /* MIME types */
  15. const MIMETYPE_DEFAULT = 'image/png';
  16. const MIMETYPE_PNG = 'image/png';
  17. const MIMETYPE_GIF = 'image/gif';
  18. const MIMETYPE_JPEG = 'image/jpeg';
  19. /**
  20. * Image resource
  21. *
  22. * @var resource
  23. */
  24. private $imageResource;
  25. /**
  26. * Rendering function
  27. *
  28. * @var string
  29. */
  30. private $renderingFunction;
  31. /**
  32. * Mime type
  33. *
  34. * @var string
  35. */
  36. private $mimeType;
  37. /**
  38. * Unique name
  39. *
  40. * @var string
  41. */
  42. private $uniqueName;
  43. /**
  44. * Create a new PHPExcel_Worksheet_MemoryDrawing
  45. */
  46. public function __construct()
  47. {
  48. // Initialise values
  49. $this->imageResource = null;
  50. $this->renderingFunction = self::RENDERING_DEFAULT;
  51. $this->mimeType = self::MIMETYPE_DEFAULT;
  52. $this->uniqueName = md5(rand(0, 9999). time() . rand(0, 9999));
  53. // Initialize parent
  54. parent::__construct();
  55. }
  56. /**
  57. * Get image resource
  58. *
  59. * @return resource
  60. */
  61. public function getImageResource()
  62. {
  63. return $this->imageResource;
  64. }
  65. /**
  66. * Set image resource
  67. *
  68. * @param $value resource
  69. * @return PHPExcel_Worksheet_MemoryDrawing
  70. */
  71. public function setImageResource($value = null)
  72. {
  73. $this->imageResource = $value;
  74. if (!is_null($this->imageResource)) {
  75. // Get width/height
  76. $this->width = imagesx($this->imageResource);
  77. $this->height = imagesy($this->imageResource);
  78. }
  79. return $this;
  80. }
  81. /**
  82. * Get rendering function
  83. *
  84. * @return string
  85. */
  86. public function getRenderingFunction()
  87. {
  88. return $this->renderingFunction;
  89. }
  90. /**
  91. * Set rendering function
  92. *
  93. * @param string $value
  94. * @return PHPExcel_Worksheet_MemoryDrawing
  95. */
  96. public function setRenderingFunction($value = PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT)
  97. {
  98. $this->renderingFunction = $value;
  99. return $this;
  100. }
  101. /**
  102. * Get mime type
  103. *
  104. * @return string
  105. */
  106. public function getMimeType()
  107. {
  108. return $this->mimeType;
  109. }
  110. /**
  111. * Set mime type
  112. *
  113. * @param string $value
  114. * @return PHPExcel_Worksheet_MemoryDrawing
  115. */
  116. public function setMimeType($value = PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT)
  117. {
  118. $this->mimeType = $value;
  119. return $this;
  120. }
  121. /**
  122. * Get indexed filename (using image index)
  123. *
  124. * @return string
  125. */
  126. public function getIndexedFilename()
  127. {
  128. $extension = strtolower($this->getMimeType());
  129. $extension = explode('/', $extension);
  130. $extension = $extension[1];
  131. return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
  132. }
  133. /**
  134. * Get hash code
  135. *
  136. * @return string Hash code
  137. */
  138. public function getHashCode()
  139. {
  140. return md5(
  141. $this->renderingFunction .
  142. $this->mimeType .
  143. $this->uniqueName .
  144. parent::getHashCode() .
  145. __CLASS__
  146. );
  147. }
  148. /**
  149. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  150. */
  151. public function __clone()
  152. {
  153. $vars = get_object_vars($this);
  154. foreach ($vars as $key => $value) {
  155. if (is_object($value)) {
  156. $this->$key = clone $value;
  157. } else {
  158. $this->$key = $value;
  159. }
  160. }
  161. }
  162. }