MemoryDrawing.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Worksheet_MemoryDrawing
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Worksheet
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Worksheet_MemoryDrawing extends PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  35. {
  36. /* Rendering functions */
  37. const RENDERING_DEFAULT = 'imagepng';
  38. const RENDERING_PNG = 'imagepng';
  39. const RENDERING_GIF = 'imagegif';
  40. const RENDERING_JPEG = 'imagejpeg';
  41. /* MIME types */
  42. const MIMETYPE_DEFAULT = 'image/png';
  43. const MIMETYPE_PNG = 'image/png';
  44. const MIMETYPE_GIF = 'image/gif';
  45. const MIMETYPE_JPEG = 'image/jpeg';
  46. /**
  47. * Image resource
  48. *
  49. * @var resource
  50. */
  51. private $_imageResource;
  52. /**
  53. * Rendering function
  54. *
  55. * @var string
  56. */
  57. private $_renderingFunction;
  58. /**
  59. * Mime type
  60. *
  61. * @var string
  62. */
  63. private $_mimeType;
  64. /**
  65. * Unique name
  66. *
  67. * @var string
  68. */
  69. private $_uniqueName;
  70. /**
  71. * Create a new PHPExcel_Worksheet_MemoryDrawing
  72. */
  73. public function __construct()
  74. {
  75. // Initialise values
  76. $this->_imageResource = null;
  77. $this->_renderingFunction = self::RENDERING_DEFAULT;
  78. $this->_mimeType = self::MIMETYPE_DEFAULT;
  79. $this->_uniqueName = md5(rand(0, 9999). time() . rand(0, 9999));
  80. // Initialize parent
  81. parent::__construct();
  82. }
  83. /**
  84. * Get image resource
  85. *
  86. * @return resource
  87. */
  88. public function getImageResource() {
  89. return $this->_imageResource;
  90. }
  91. /**
  92. * Set image resource
  93. *
  94. * @param $value resource
  95. * @return PHPExcel_Worksheet_MemoryDrawing
  96. */
  97. public function setImageResource($value = null) {
  98. $this->_imageResource = $value;
  99. if (!is_null($this->_imageResource)) {
  100. // Get width/height
  101. $this->_width = imagesx($this->_imageResource);
  102. $this->_height = imagesy($this->_imageResource);
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Get rendering function
  108. *
  109. * @return string
  110. */
  111. public function getRenderingFunction() {
  112. return $this->_renderingFunction;
  113. }
  114. /**
  115. * Set rendering function
  116. *
  117. * @param string $value
  118. * @return PHPExcel_Worksheet_MemoryDrawing
  119. */
  120. public function setRenderingFunction($value = PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT) {
  121. $this->_renderingFunction = $value;
  122. return $this;
  123. }
  124. /**
  125. * Get mime type
  126. *
  127. * @return string
  128. */
  129. public function getMimeType() {
  130. return $this->_mimeType;
  131. }
  132. /**
  133. * Set mime type
  134. *
  135. * @param string $value
  136. * @return PHPExcel_Worksheet_MemoryDrawing
  137. */
  138. public function setMimeType($value = PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT) {
  139. $this->_mimeType = $value;
  140. return $this;
  141. }
  142. /**
  143. * Get indexed filename (using image index)
  144. *
  145. * @return string
  146. */
  147. public function getIndexedFilename() {
  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. return md5(
  160. $this->_renderingFunction
  161. . $this->_mimeType
  162. . $this->_uniqueName
  163. . parent::getHashCode()
  164. . __CLASS__
  165. );
  166. }
  167. /**
  168. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  169. */
  170. public function __clone() {
  171. $vars = get_object_vars($this);
  172. foreach ($vars as $key => $value) {
  173. if (is_object($value)) {
  174. $this->$key = clone $value;
  175. } else {
  176. $this->$key = $value;
  177. }
  178. }
  179. }
  180. }