IImage.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCP;
  9. use GdImage;
  10. /**
  11. * Class for basic image manipulation
  12. * @since 8.1.0
  13. */
  14. interface IImage {
  15. /**
  16. * Determine whether the object contains an image resource.
  17. *
  18. * @since 8.1.0
  19. */
  20. public function valid(): bool;
  21. /**
  22. * Returns the MIME type of the image or null if no image is loaded.
  23. *
  24. * @since 8.1.0
  25. */
  26. public function mimeType(): ?string;
  27. /**
  28. * Returns the width of the image or -1 if no image is loaded.
  29. *
  30. * @since 8.1.0
  31. */
  32. public function width(): int;
  33. /**
  34. * Returns the height of the image or -1 if no image is loaded.
  35. *
  36. * @since 8.1.0
  37. */
  38. public function height(): int;
  39. /**
  40. * Returns the width when the image orientation is top-left.
  41. *
  42. * @since 8.1.0
  43. */
  44. public function widthTopLeft(): int;
  45. /**
  46. * Returns the height when the image orientation is top-left.
  47. *
  48. * @since 8.1.0
  49. */
  50. public function heightTopLeft(): int;
  51. /**
  52. * Outputs the image.
  53. *
  54. * @since 8.1.0
  55. */
  56. public function show(?string $mimeType = null): bool;
  57. /**
  58. * Saves the image.
  59. *
  60. * @param string $filePath
  61. * @param string $mimeType
  62. * @since 8.1.0
  63. */
  64. public function save(?string $filePath = null, ?string $mimeType = null): bool;
  65. /**
  66. * @return false|resource|\GdImage Returns the image resource if any
  67. * @since 8.1.0
  68. */
  69. public function resource();
  70. /**
  71. * @return string Returns the mimetype of the data. Returns null
  72. * if the data is not valid.
  73. * @since 13.0.0
  74. */
  75. public function dataMimeType(): ?string;
  76. /**
  77. * @return string Returns the raw image data.
  78. * @since 8.1.0
  79. */
  80. public function data(): ?string;
  81. /**
  82. * (I'm open for suggestions on better method name ;)
  83. * Get the orientation based on EXIF data.
  84. *
  85. * @return int The orientation or -1 if no EXIF data is available.
  86. * @since 8.1.0
  87. */
  88. public function getOrientation(): int;
  89. /**
  90. * (I'm open for suggestions on better method name ;)
  91. * Fixes orientation based on EXIF data.
  92. *
  93. * @since 8.1.0
  94. */
  95. public function fixOrientation(): bool;
  96. /**
  97. * Resizes the image preserving ratio.
  98. *
  99. * @param integer $maxSize The maximum size of either the width or height.
  100. * @since 8.1.0
  101. */
  102. public function resize(int $maxSize): bool;
  103. /**
  104. * @param int $width
  105. * @param int $height
  106. * @return bool
  107. * @since 8.1.0
  108. */
  109. public function preciseResize(int $width, int $height): bool;
  110. /**
  111. * Crops the image to the middle square. If the image is already square it just returns.
  112. *
  113. * @param int $size maximum size for the result (optional)
  114. * @return bool for success or failure
  115. * @since 8.1.0
  116. */
  117. public function centerCrop(int $size = 0): bool;
  118. /**
  119. * Crops the image from point $x$y with dimension $wx$h.
  120. *
  121. * @param int $x Horizontal position
  122. * @param int $y Vertical position
  123. * @param int $w Width
  124. * @param int $h Height
  125. * @return bool for success or failure
  126. * @since 8.1.0
  127. */
  128. public function crop(int $x, int $y, int $w, int $h): bool;
  129. /**
  130. * Resizes the image to fit within a boundary while preserving ratio.
  131. *
  132. * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up
  133. *
  134. * @param int $maxWidth
  135. * @param int $maxHeight
  136. * @since 8.1.0
  137. */
  138. public function fitIn(int $maxWidth, int $maxHeight): bool;
  139. /**
  140. * Shrinks the image to fit within a boundary while preserving ratio.
  141. *
  142. * @param int $maxWidth
  143. * @param int $maxHeight
  144. * @since 8.1.0
  145. */
  146. public function scaleDownToFit(int $maxWidth, int $maxHeight): bool;
  147. /**
  148. * create a copy of this image
  149. *
  150. * @return IImage
  151. * @since 19.0.0
  152. */
  153. public function copy(): IImage;
  154. /**
  155. * create a new cropped copy of this image
  156. *
  157. * @param int $x Horizontal position
  158. * @param int $y Vertical position
  159. * @param int $w Width
  160. * @param int $h Height
  161. * @return IImage
  162. * @since 19.0.0
  163. */
  164. public function cropCopy(int $x, int $y, int $w, int $h): IImage;
  165. /**
  166. * create a new resized copy of this image
  167. *
  168. * @param int $width
  169. * @param int $height
  170. * @return IImage
  171. * @since 19.0.0
  172. */
  173. public function preciseResizeCopy(int $width, int $height): IImage;
  174. /**
  175. * Resizes the image preserving ratio, returning a new copy
  176. *
  177. * @param int $maxSize The maximum size of either the width or height.
  178. * @return IImage
  179. * @since 19.0.0
  180. */
  181. public function resizeCopy(int $maxSize): IImage;
  182. /**
  183. * Loads an image from a string of data.
  184. *
  185. * @param string $str A string of image data as read from a file.
  186. *
  187. * @since 31.0.0
  188. */
  189. public function loadFromData(string $str): GdImage|false;
  190. /**
  191. * Reads the EXIF data for an image.
  192. *
  193. * @param string $data EXIF data
  194. *
  195. * @since 31.0.0
  196. */
  197. public function readExif(string $data): void;
  198. }