Drawing.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. use ZipArchive;
  5. class Drawing extends BaseDrawing
  6. {
  7. const IMAGE_TYPES_CONVERTION_MAP = [
  8. IMAGETYPE_GIF => IMAGETYPE_PNG,
  9. IMAGETYPE_JPEG => IMAGETYPE_JPEG,
  10. IMAGETYPE_PNG => IMAGETYPE_PNG,
  11. IMAGETYPE_BMP => IMAGETYPE_PNG,
  12. ];
  13. /**
  14. * Path.
  15. *
  16. * @var string
  17. */
  18. private $path;
  19. /**
  20. * Whether or not we are dealing with a URL.
  21. *
  22. * @var bool
  23. */
  24. private $isUrl;
  25. /**
  26. * Create a new Drawing.
  27. */
  28. public function __construct()
  29. {
  30. // Initialise values
  31. $this->path = '';
  32. $this->isUrl = false;
  33. // Initialize parent
  34. parent::__construct();
  35. }
  36. /**
  37. * Get Filename.
  38. *
  39. * @return string
  40. */
  41. public function getFilename()
  42. {
  43. return basename($this->path);
  44. }
  45. /**
  46. * Get indexed filename (using image index).
  47. */
  48. public function getIndexedFilename(): string
  49. {
  50. return md5($this->path) . '.' . $this->getExtension();
  51. }
  52. /**
  53. * Get Extension.
  54. *
  55. * @return string
  56. */
  57. public function getExtension()
  58. {
  59. $exploded = explode('.', basename($this->path));
  60. return $exploded[count($exploded) - 1];
  61. }
  62. /**
  63. * Get full filepath to store drawing in zip archive.
  64. *
  65. * @return string
  66. */
  67. public function getMediaFilename()
  68. {
  69. if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
  70. throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
  71. }
  72. return sprintf('image%d%s', $this->getImageIndex(), $this->getImageFileExtensionForSave());
  73. }
  74. /**
  75. * Get Path.
  76. *
  77. * @return string
  78. */
  79. public function getPath()
  80. {
  81. return $this->path;
  82. }
  83. /**
  84. * Set Path.
  85. *
  86. * @param string $path File path
  87. * @param bool $verifyFile Verify file
  88. * @param ZipArchive $zip Zip archive instance
  89. *
  90. * @return $this
  91. */
  92. public function setPath($path, $verifyFile = true, $zip = null)
  93. {
  94. if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) {
  95. // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
  96. if (filter_var($path, FILTER_VALIDATE_URL)) {
  97. $this->path = $path;
  98. // Implicit that it is a URL, rather store info than running check above on value in other places.
  99. $this->isUrl = true;
  100. $imageContents = file_get_contents($path);
  101. $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
  102. if ($filePath) {
  103. file_put_contents($filePath, $imageContents);
  104. if (file_exists($filePath)) {
  105. $this->setSizesAndType($filePath);
  106. unlink($filePath);
  107. }
  108. }
  109. } elseif (file_exists($path)) {
  110. $this->path = $path;
  111. $this->setSizesAndType($path);
  112. } elseif ($zip instanceof ZipArchive) {
  113. $zipPath = explode('#', $path)[1];
  114. if ($zip->locateName($zipPath) !== false) {
  115. $this->path = $path;
  116. $this->setSizesAndType($path);
  117. }
  118. } else {
  119. throw new PhpSpreadsheetException("File $path not found!");
  120. }
  121. } else {
  122. $this->path = $path;
  123. }
  124. return $this;
  125. }
  126. /**
  127. * Get isURL.
  128. */
  129. public function getIsURL(): bool
  130. {
  131. return $this->isUrl;
  132. }
  133. /**
  134. * Set isURL.
  135. *
  136. * @return $this
  137. */
  138. public function setIsURL(bool $isUrl): self
  139. {
  140. $this->isUrl = $isUrl;
  141. return $this;
  142. }
  143. /**
  144. * Get hash code.
  145. *
  146. * @return string Hash code
  147. */
  148. public function getHashCode()
  149. {
  150. return md5(
  151. $this->path .
  152. parent::getHashCode() .
  153. __CLASS__
  154. );
  155. }
  156. /**
  157. * Get Image Type for Save.
  158. */
  159. public function getImageTypeForSave(): int
  160. {
  161. if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
  162. throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
  163. }
  164. return self::IMAGE_TYPES_CONVERTION_MAP[$this->type];
  165. }
  166. /**
  167. * Get Image file extention for Save.
  168. */
  169. public function getImageFileExtensionForSave(bool $includeDot = true): string
  170. {
  171. if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
  172. throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
  173. }
  174. $result = image_type_to_extension(self::IMAGE_TYPES_CONVERTION_MAP[$this->type], $includeDot);
  175. return "$result";
  176. }
  177. /**
  178. * Get Image mime type.
  179. */
  180. public function getImageMimeType(): string
  181. {
  182. if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
  183. throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
  184. }
  185. return image_type_to_mime_type(self::IMAGE_TYPES_CONVERTION_MAP[$this->type]);
  186. }
  187. }