MemoryDrawing.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * PHPExcel_Worksheet_MemoryDrawing
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Worksheet
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Worksheet_MemoryDrawing extends PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  28. {
  29. /* Rendering functions */
  30. const RENDERING_DEFAULT = 'imagepng';
  31. const RENDERING_PNG = 'imagepng';
  32. const RENDERING_GIF = 'imagegif';
  33. const RENDERING_JPEG = 'imagejpeg';
  34. /* MIME types */
  35. const MIMETYPE_DEFAULT = 'image/png';
  36. const MIMETYPE_PNG = 'image/png';
  37. const MIMETYPE_GIF = 'image/gif';
  38. const MIMETYPE_JPEG = 'image/jpeg';
  39. /**
  40. * Image resource
  41. *
  42. * @var resource
  43. */
  44. private $imageResource;
  45. /**
  46. * Rendering function
  47. *
  48. * @var string
  49. */
  50. private $renderingFunction;
  51. /**
  52. * Mime type
  53. *
  54. * @var string
  55. */
  56. private $mimeType;
  57. /**
  58. * Unique name
  59. *
  60. * @var string
  61. */
  62. private $uniqueName;
  63. /**
  64. * Create a new PHPExcel_Worksheet_MemoryDrawing
  65. */
  66. public function __construct()
  67. {
  68. // Initialise values
  69. $this->imageResource = null;
  70. $this->renderingFunction = self::RENDERING_DEFAULT;
  71. $this->mimeType = self::MIMETYPE_DEFAULT;
  72. $this->uniqueName = md5(rand(0, 9999). time() . rand(0, 9999));
  73. // Initialize parent
  74. parent::__construct();
  75. }
  76. /**
  77. * Get image resource
  78. *
  79. * @return resource
  80. */
  81. public function getImageResource()
  82. {
  83. return $this->imageResource;
  84. }
  85. /**
  86. * Set image resource
  87. *
  88. * @param $value resource
  89. * @return PHPExcel_Worksheet_MemoryDrawing
  90. */
  91. public function setImageResource($value = null)
  92. {
  93. $this->imageResource = $value;
  94. if (!is_null($this->imageResource)) {
  95. // Get width/height
  96. $this->width = imagesx($this->imageResource);
  97. $this->height = imagesy($this->imageResource);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Get rendering function
  103. *
  104. * @return string
  105. */
  106. public function getRenderingFunction()
  107. {
  108. return $this->renderingFunction;
  109. }
  110. /**
  111. * Set rendering function
  112. *
  113. * @param string $value
  114. * @return PHPExcel_Worksheet_MemoryDrawing
  115. */
  116. public function setRenderingFunction($value = PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT)
  117. {
  118. $this->renderingFunction = $value;
  119. return $this;
  120. }
  121. /**
  122. * Get mime type
  123. *
  124. * @return string
  125. */
  126. public function getMimeType()
  127. {
  128. return $this->mimeType;
  129. }
  130. /**
  131. * Set mime type
  132. *
  133. * @param string $value
  134. * @return PHPExcel_Worksheet_MemoryDrawing
  135. */
  136. public function setMimeType($value = PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT)
  137. {
  138. $this->mimeType = $value;
  139. return $this;
  140. }
  141. /**
  142. * Get indexed filename (using image index)
  143. *
  144. * @return string
  145. */
  146. public function getIndexedFilename()
  147. {
  148. $extension = strtolower($this->getMimeType());
  149. $extension = explode('/', $extension);
  150. $extension = $extension[1];
  151. return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
  152. }
  153. /**
  154. * Get hash code
  155. *
  156. * @return string Hash code
  157. */
  158. public function getHashCode()
  159. {
  160. return md5(
  161. $this->renderingFunction .
  162. $this->mimeType .
  163. $this->uniqueName .
  164. parent::getHashCode() .
  165. __CLASS__
  166. );
  167. }
  168. /**
  169. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  170. */
  171. public function __clone()
  172. {
  173. $vars = get_object_vars($this);
  174. foreach ($vars as $key => $value) {
  175. if (is_object($value)) {
  176. $this->$key = clone $value;
  177. } else {
  178. $this->$key = $value;
  179. }
  180. }
  181. }
  182. }