Skip navigation
Help

image.inc

  1. drupal
    1. 6 drupal/includes/image.inc
    2. 7 drupal/includes/image.inc

API for manipulating images.

Functions & methods

NameDescription
image_cropCrop an image to the rectangle specified by the given rectangle.
image_desaturateConvert an image to grayscale.
image_get_available_toolkitsReturn a list of available toolkits.
image_get_infoGet details about an image.
image_get_toolkitRetrieve the name of the currently used toolkit.
image_loadLoad an image file and return an image object.
image_resizeResize an image to the given dimensions (ignoring aspect ratio).
image_rotateRotate an image by the given number of degrees.
image_saveClose the image and save the changes to a file.
image_scaleScales an image to the given width and height while maintaining aspect ratio.
image_scale_and_cropScales an image to the exact width and height given.
image_toolkit_invokeInvokes the given method using the currently selected toolkit.

File

drupal/includes/image.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * API for manipulating images.
  5. */
  6. /**
  7. * @defgroup image Image toolkits
  8. * @{
  9. * Functions for image file manipulations.
  10. *
  11. * Drupal's image toolkits provide an abstraction layer for common image file
  12. * manipulations like scaling, cropping, and rotating. The abstraction frees
  13. * module authors from the need to support multiple image libraries, and it
  14. * allows site administrators to choose the library that's best for them.
  15. *
  16. * PHP includes the GD library by default so a GD toolkit is installed with
  17. * Drupal. Other toolkits like ImageMagick are available from contrib modules.
  18. * GD works well for small images, but using it with larger files may cause PHP
  19. * to run out of memory. In contrast the ImageMagick library does not suffer
  20. * from this problem, but it requires the ISP to have installed additional
  21. * software.
  22. *
  23. * Image toolkits are discovered based on the associated module's
  24. * hook_image_toolkits. Additionally the image toolkit include file
  25. * must be identified in the files array in the module.info file. The
  26. * toolkit must then be enabled using the admin/config/media/image-toolkit
  27. * form.
  28. *
  29. * Only one toolkit may be selected at a time. If a module author wishes to call
  30. * a specific toolkit they can check that it is installed by calling
  31. * image_get_available_toolkits(), and then calling its functions directly.
  32. */
  33. /**
  34. * Return a list of available toolkits.
  35. *
  36. * @return
  37. * An array with the toolkit names as keys and the descriptions as values.
  38. */
  39. function image_get_available_toolkits() {
  40. // hook_image_toolkits returns an array of toolkit names.
  41. $toolkits = module_invoke_all('image_toolkits');
  42. $output = array();
  43. foreach ($toolkits as $name => $info) {
  44. // Only allow modules that aren't marked as unavailable.
  45. if ($info['available']) {
  46. $output[$name] = $info['title'];
  47. }
  48. }
  49. return $output;
  50. }
  51. /**
  52. * Retrieve the name of the currently used toolkit.
  53. *
  54. * @return
  55. * String containing the name of the selected toolkit, or FALSE on error.
  56. */
  57. function image_get_toolkit() {
  58. static $toolkit;
  59. if (!isset($toolkit)) {
  60. $toolkits = image_get_available_toolkits();
  61. $toolkit = variable_get('image_toolkit', 'gd');
  62. if (!isset($toolkits[$toolkit]) || !function_exists('image_' . $toolkit . '_load')) {
  63. // The selected toolkit isn't available so return the first one found. If
  64. // none are available this will return FALSE.
  65. reset($toolkits);
  66. $toolkit = key($toolkits);
  67. }
  68. }
  69. return $toolkit;
  70. }
  71. /**
  72. * Invokes the given method using the currently selected toolkit.
  73. *
  74. * @param $method
  75. * A string containing the method to invoke.
  76. * @param $image
  77. * An image object returned by image_load().
  78. * @param $params
  79. * An optional array of parameters to pass to the toolkit method.
  80. *
  81. * @return
  82. * Mixed values (typically Boolean indicating successful operation).
  83. */
  84. function image_toolkit_invoke($method, stdClass $image, array $params = array()) {
  85. $function = 'image_' . $image->toolkit . '_' . $method;
  86. if (function_exists($function)) {
  87. array_unshift($params, $image);
  88. return call_user_func_array($function, $params);
  89. }
  90. watchdog('image', 'The selected image handling toolkit %toolkit can not correctly process %function.', array('%toolkit' => $image->toolkit, '%function' => $function), WATCHDOG_ERROR);
  91. return FALSE;
  92. }
  93. /**
  94. * Get details about an image.
  95. *
  96. * Drupal supports GIF, JPG and PNG file formats when used with the GD
  97. * toolkit, and may support others, depending on which toolkits are
  98. * installed.
  99. *
  100. * @param $filepath
  101. * String specifying the path of the image file.
  102. * @param $toolkit
  103. * An optional image toolkit name to override the default.
  104. *
  105. * @return
  106. * FALSE, if the file could not be found or is not an image. Otherwise, a
  107. * keyed array containing information about the image:
  108. * - "width": Width, in pixels.
  109. * - "height": Height, in pixels.
  110. * - "extension": Commonly used file extension for the image.
  111. * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
  112. * - "file_size": File size in bytes.
  113. */
  114. function image_get_info($filepath, $toolkit = FALSE) {
  115. $details = FALSE;
  116. if (!is_file($filepath) && !is_uploaded_file($filepath)) {
  117. return $details;
  118. }
  119. if (!$toolkit) {
  120. $toolkit = image_get_toolkit();
  121. }
  122. if ($toolkit) {
  123. $image = new stdClass();
  124. $image->source = $filepath;
  125. $image->toolkit = $toolkit;
  126. $details = image_toolkit_invoke('get_info', $image);
  127. if (isset($details) && is_array($details)) {
  128. $details['file_size'] = filesize($filepath);
  129. }
  130. }
  131. return $details;
  132. }
  133. /**
  134. * Scales an image to the exact width and height given.
  135. *
  136. * This function achieves the target aspect ratio by cropping the original image
  137. * equally on both sides, or equally on the top and bottom. This function is
  138. * useful to create uniform sized avatars from larger images.
  139. *
  140. * The resulting image always has the exact target dimensions.
  141. *
  142. * @param $image
  143. * An image object returned by image_load().
  144. * @param $width
  145. * The target width, in pixels.
  146. * @param $height
  147. * The target height, in pixels.
  148. *
  149. * @return
  150. * TRUE or FALSE, based on success.
  151. *
  152. * @see image_load()
  153. * @see image_resize()
  154. * @see image_crop()
  155. */
  156. function image_scale_and_crop(stdClass $image, $width, $height) {
  157. $scale = max($width / $image->info['width'], $height / $image->info['height']);
  158. $x = ($image->info['width'] * $scale - $width) / 2;
  159. $y = ($image->info['height'] * $scale - $height) / 2;
  160. if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
  161. return image_crop($image, $x, $y, $width, $height);
  162. }
  163. return FALSE;
  164. }
  165. /**
  166. * Scales an image to the given width and height while maintaining aspect ratio.
  167. *
  168. * The resulting image can be smaller for one or both target dimensions.
  169. *
  170. * @param $image
  171. * An image object returned by image_load().
  172. * @param $width
  173. * The target width, in pixels. This value is omitted then the scaling will
  174. * based only on the height value.
  175. * @param $height
  176. * The target height, in pixels. This value is omitted then the scaling will
  177. * based only on the width value.
  178. * @param $upscale
  179. * Boolean indicating that files smaller than the dimensions will be scaled
  180. * up. This generally results in a low quality image.
  181. *
  182. * @return
  183. * TRUE or FALSE, based on success.
  184. *
  185. * @see image_load()
  186. * @see image_scale_and_crop()
  187. */
  188. function image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) {
  189. $aspect = $image->info['height'] / $image->info['width'];
  190. if ($upscale) {
  191. // Set width/height according to aspect ratio if either is empty.
  192. $width = !empty($width) ? $width : $height / $aspect;
  193. $height = !empty($height) ? $height : $width / $aspect;
  194. }
  195. else {
  196. // Set impossibly large values if the width and height aren't set.
  197. $width = !empty($width) ? $width : 9999999;
  198. $height = !empty($height) ? $height : 9999999;
  199. // Don't scale up.
  200. if (round($width) >= $image->info['width'] && round($height) >= $image->info['height']) {
  201. return TRUE;
  202. }
  203. }
  204. if ($aspect < $height / $width) {
  205. $height = $width * $aspect;
  206. }
  207. else {
  208. $width = $height / $aspect;
  209. }
  210. return image_resize($image, $width, $height);
  211. }
  212. /**
  213. * Resize an image to the given dimensions (ignoring aspect ratio).
  214. *
  215. * @param $image
  216. * An image object returned by image_load().
  217. * @param $width
  218. * The target width, in pixels.
  219. * @param $height
  220. * The target height, in pixels.
  221. *
  222. * @return
  223. * TRUE or FALSE, based on success.
  224. *
  225. * @see image_load()
  226. * @see image_gd_resize()
  227. */
  228. function image_resize(stdClass $image, $width, $height) {
  229. $width = (int) round($width);
  230. $height = (int) round($height);
  231. return image_toolkit_invoke('resize', $image, array($width, $height));
  232. }
  233. /**
  234. * Rotate an image by the given number of degrees.
  235. *
  236. * @param $image
  237. * An image object returned by image_load().
  238. * @param $degrees
  239. * The number of (clockwise) degrees to rotate the image.
  240. * @param $background
  241. * An hexadecimal integer specifying the background color to use for the
  242. * uncovered area of the image after the rotation. E.g. 0x000000 for black,
  243. * 0xff00ff for magenta, and 0xffffff for white. For images that support
  244. * transparency, this will default to transparent. Otherwise it will
  245. * be white.
  246. *
  247. * @return
  248. * TRUE or FALSE, based on success.
  249. *
  250. * @see image_load()
  251. * @see image_gd_rotate()
  252. */
  253. function image_rotate(stdClass $image, $degrees, $background = NULL) {
  254. return image_toolkit_invoke('rotate', $image, array($degrees, $background));
  255. }
  256. /**
  257. * Crop an image to the rectangle specified by the given rectangle.
  258. *
  259. * @param $image
  260. * An image object returned by image_load().
  261. * @param $x
  262. * The top left coordinate, in pixels, of the crop area (x axis value).
  263. * @param $y
  264. * The top left coordinate, in pixels, of the crop area (y axis value).
  265. * @param $width
  266. * The target width, in pixels.
  267. * @param $height
  268. * The target height, in pixels.
  269. *
  270. * @return
  271. * TRUE or FALSE, based on success.
  272. *
  273. * @see image_load()
  274. * @see image_scale_and_crop()
  275. * @see image_gd_crop()
  276. */
  277. function image_crop(stdClass $image, $x, $y, $width, $height) {
  278. $aspect = $image->info['height'] / $image->info['width'];
  279. if (empty($height)) $height = $width / $aspect;
  280. if (empty($width)) $width = $height * $aspect;
  281. $width = (int) round($width);
  282. $height = (int) round($height);
  283. return image_toolkit_invoke('crop', $image, array($x, $y, $width, $height));
  284. }
  285. /**
  286. * Convert an image to grayscale.
  287. *
  288. * @param $image
  289. * An image object returned by image_load().
  290. *
  291. * @return
  292. * TRUE or FALSE, based on success.
  293. *
  294. * @see image_load()
  295. * @see image_gd_desaturate()
  296. */
  297. function image_desaturate(stdClass $image) {
  298. return image_toolkit_invoke('desaturate', $image);
  299. }
  300. /**
  301. * Load an image file and return an image object.
  302. *
  303. * Any changes to the file are not saved until image_save() is called.
  304. *
  305. * @param $file
  306. * Path to an image file.
  307. * @param $toolkit
  308. * An optional, image toolkit name to override the default.
  309. *
  310. * @return
  311. * An image object or FALSE if there was a problem loading the file. The
  312. * image object has the following properties:
  313. * - 'source' - The original file path.
  314. * - 'info' - The array of information returned by image_get_info()
  315. * - 'toolkit' - The name of the image toolkit requested when the image was
  316. * loaded.
  317. * Image toolkits may add additional properties. The caller is advised not to
  318. * monkey about with them.
  319. *
  320. * @see image_save()
  321. * @see image_get_info()
  322. * @see image_get_available_toolkits()
  323. * @see image_gd_load()
  324. */
  325. function image_load($file, $toolkit = FALSE) {
  326. if (!$toolkit) {
  327. $toolkit = image_get_toolkit();
  328. }
  329. if ($toolkit) {
  330. $image = new stdClass();
  331. $image->source = $file;
  332. $image->info = image_get_info($file, $toolkit);
  333. if (isset($image->info) && is_array($image->info)) {
  334. $image->toolkit = $toolkit;
  335. if (image_toolkit_invoke('load', $image)) {
  336. return $image;
  337. }
  338. }
  339. }
  340. return FALSE;
  341. }
  342. /**
  343. * Close the image and save the changes to a file.
  344. *
  345. * @param $image
  346. * An image object returned by image_load(). The object's 'info' property
  347. * will be updated if the file is saved successfully.
  348. * @param $destination
  349. * Destination path where the image should be saved. If it is empty the
  350. * original image file will be overwritten.
  351. *
  352. * @return
  353. * TRUE or FALSE, based on success.
  354. *
  355. * @see image_load()
  356. * @see image_gd_save()
  357. */
  358. function image_save(stdClass $image, $destination = NULL) {
  359. if (empty($destination)) {
  360. $destination = $image->source;
  361. }
  362. if ($return = image_toolkit_invoke('save', $image, array($destination))) {
  363. // Clear the cached file size and refresh the image information.
  364. clearstatcache();
  365. $image->info = image_get_info($destination, $image->toolkit);
  366. if (drupal_chmod($destination)) {
  367. return $return;
  368. }
  369. }
  370. return FALSE;
  371. }
  372. /**
  373. * @} End of "defgroup image".
  374. */