ImageInterface.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Drupal\Core\Image;
  3. /**
  4. * Provides an interface for image objects.
  5. */
  6. interface ImageInterface {
  7. /**
  8. * Checks if the image is valid.
  9. *
  10. * @return bool
  11. * TRUE if the image object contains a valid image, FALSE otherwise.
  12. */
  13. public function isValid();
  14. /**
  15. * Returns the height of the image.
  16. *
  17. * @return int|null
  18. * The height of the image, or NULL if the image is invalid.
  19. */
  20. public function getHeight();
  21. /**
  22. * Returns the width of the image.
  23. *
  24. * @return int|null
  25. * The width of the image, or NULL if the image is invalid.
  26. */
  27. public function getWidth();
  28. /**
  29. * Returns the size of the image file.
  30. *
  31. * @return int|null
  32. * The size of the file in bytes, or NULL if the image is invalid.
  33. */
  34. public function getFileSize();
  35. /**
  36. * Returns the MIME type of the image file.
  37. *
  38. * @return string
  39. * The MIME type of the image file, or an empty string if the image is
  40. * invalid.
  41. */
  42. public function getMimeType();
  43. /**
  44. * Retrieves the source path of the image file.
  45. *
  46. * @return string
  47. * The source path of the image file. An empty string if the source is
  48. * not set.
  49. */
  50. public function getSource();
  51. /**
  52. * Returns the image toolkit used for this image file.
  53. *
  54. * @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
  55. * The image toolkit.
  56. */
  57. public function getToolkit();
  58. /**
  59. * Returns the ID of the image toolkit used for this image file.
  60. *
  61. * @return string
  62. * The ID of the image toolkit.
  63. */
  64. public function getToolkitId();
  65. /**
  66. * Applies a toolkit operation to the image.
  67. *
  68. * The operation is deferred to the active toolkit.
  69. *
  70. * @param string $operation
  71. * The operation to be performed against the image.
  72. * @param array $arguments
  73. * (optional) An associative array of arguments to be passed to the toolkit
  74. * operation; for instance,
  75. * @code
  76. * ['width' => 50, 'height' => 100, 'upscale' => TRUE]
  77. * @endcode
  78. * Defaults to an empty array.
  79. *
  80. * @return bool
  81. * TRUE on success, FALSE on failure.
  82. */
  83. public function apply($operation, array $arguments = []);
  84. /**
  85. * Closes the image and saves the changes to a file.
  86. *
  87. * @param string|null $destination
  88. * (optional) Destination path where the image should be saved. If it is empty
  89. * the original image file will be overwritten.
  90. *
  91. * @return bool
  92. * TRUE on success, FALSE on failure.
  93. *
  94. * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::save()
  95. */
  96. public function save($destination = NULL);
  97. /**
  98. * Prepares a new image, without loading it from a file.
  99. *
  100. * For a working example, see
  101. * \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew.
  102. *
  103. * @param int $width
  104. * The width of the new image, in pixels.
  105. * @param int $height
  106. * The height of the new image, in pixels.
  107. * @param string $extension
  108. * (optional) The extension of the image file (for instance, 'png', 'gif',
  109. * etc.). Allowed values depend on the implementation of the image toolkit.
  110. * Defaults to 'png'.
  111. * @param string $transparent_color
  112. * (optional) The hexadecimal string representing the color to be used
  113. * for transparency, needed for GIF images. Defaults to '#ffffff' (white).
  114. *
  115. * @return bool
  116. * TRUE on success, FALSE on failure.
  117. */
  118. public function createNew($width, $height, $extension = 'png', $transparent_color = '#ffffff');
  119. /**
  120. * Scales an image while maintaining aspect ratio.
  121. *
  122. * The resulting image can be smaller for one or both target dimensions.
  123. *
  124. * @param int|null $width
  125. * The target width, in pixels. If this value is null then the scaling will
  126. * be based only on the height value.
  127. * @param int|null $height
  128. * (optional) The target height, in pixels. If this value is null then the
  129. * scaling will be based only on the width value.
  130. * @param bool $upscale
  131. * (optional) Boolean indicating that files smaller than the dimensions will
  132. * be scaled up. This generally results in a low quality image.
  133. *
  134. * @return bool
  135. * TRUE on success, FALSE on failure.
  136. */
  137. public function scale($width, $height = NULL, $upscale = FALSE);
  138. /**
  139. * Scales an image to the exact width and height given.
  140. *
  141. * This function achieves the target aspect ratio by cropping the original
  142. * image equally on both sides, or equally on the top and bottom. This
  143. * function is useful to create uniform sized avatars from larger images.
  144. *
  145. * The resulting image always has the exact target dimensions.
  146. *
  147. * @param int $width
  148. * The target width, in pixels.
  149. * @param int $height
  150. * The target height, in pixels.
  151. *
  152. * @return bool
  153. * TRUE on success, FALSE on failure.
  154. */
  155. public function scaleAndCrop($width, $height);
  156. /**
  157. * Instructs the toolkit to save the image in the format specified by the
  158. * extension.
  159. *
  160. * @param string $extension
  161. * The extension to convert to (for instance, 'jpeg' or 'png'). Allowed
  162. * values depend on the current image toolkit.
  163. *
  164. * @return bool
  165. * TRUE on success, FALSE on failure.
  166. *
  167. * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::getSupportedExtensions()
  168. */
  169. public function convert($extension);
  170. /**
  171. * Crops an image to a rectangle specified by the given dimensions.
  172. *
  173. * @param int $x
  174. * The top left coordinate, in pixels, of the crop area (x axis value).
  175. * @param int $y
  176. * The top left coordinate, in pixels, of the crop area (y axis value).
  177. * @param int $width
  178. * The target width, in pixels.
  179. * @param int $height
  180. * The target height, in pixels.
  181. *
  182. * @return bool
  183. * TRUE on success, FALSE on failure.
  184. */
  185. public function crop($x, $y, $width, $height = NULL);
  186. /**
  187. * Resizes an image to the given dimensions (ignoring aspect ratio).
  188. *
  189. * @param int $width
  190. * The target width, in pixels.
  191. * @param int $height
  192. * The target height, in pixels.
  193. *
  194. * @return bool
  195. * TRUE on success, FALSE on failure.
  196. */
  197. public function resize($width, $height);
  198. /**
  199. * Converts an image to grayscale.
  200. *
  201. * @return bool
  202. * TRUE on success, FALSE on failure.
  203. */
  204. public function desaturate();
  205. /**
  206. * Rotates an image by the given number of degrees.
  207. *
  208. * @param float $degrees
  209. * The number of (clockwise) degrees to rotate the image.
  210. * @param string|null $background
  211. * (optional) An hexadecimal integer specifying the background color to use
  212. * for the uncovered area of the image after the rotation; for example,
  213. * 0x000000 for black, 0xff00ff for magenta, and 0xffffff for white. When
  214. * NULL (the default) is specified, for images that support transparency,
  215. * this will default to transparent; otherwise, it will default to white.
  216. *
  217. * @return bool
  218. * TRUE on success, FALSE on failure.
  219. */
  220. public function rotate($degrees, $background = NULL);
  221. }