image.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. * Gets 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. * Gets 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. * Gets 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 on success, FALSE on failure.
  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 image dimensions while maintaining aspect ratio.
  167. *
  168. * The resulting dimensions can be smaller for one or both target dimensions.
  169. *
  170. * @param $dimensions
  171. * Dimensions to be modified - an array with components width and height, in
  172. * pixels.
  173. * @param $width
  174. * The target width, in pixels. If this value is NULL then the scaling will be
  175. * based only on the height value.
  176. * @param $height
  177. * The target height, in pixels. If this value is NULL then the scaling will
  178. * be based only on the width value.
  179. * @param $upscale
  180. * Boolean indicating that images smaller than the target dimensions will be
  181. * scaled up. This generally results in a low quality image.
  182. *
  183. * @return
  184. * TRUE if $dimensions was modified, FALSE otherwise.
  185. *
  186. * @see image_scale()
  187. */
  188. function image_dimensions_scale(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) {
  189. $aspect = $dimensions['height'] / $dimensions['width'];
  190. // Calculate one of the dimensions from the other target dimension,
  191. // ensuring the same aspect ratio as the source dimensions. If one of the
  192. // target dimensions is missing, that is the one that is calculated. If both
  193. // are specified then the dimension calculated is the one that would not be
  194. // calculated to be bigger than its target.
  195. if (($width && !$height) || ($width && $height && $aspect < $height / $width)) {
  196. $height = (int) round($width * $aspect);
  197. }
  198. else {
  199. $width = (int) round($height / $aspect);
  200. }
  201. // Don't upscale if the option isn't enabled.
  202. if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) {
  203. return FALSE;
  204. }
  205. $dimensions['width'] = $width;
  206. $dimensions['height'] = $height;
  207. return TRUE;
  208. }
  209. /**
  210. * Scales an image while maintaining aspect ratio.
  211. *
  212. * The resulting image can be smaller for one or both target dimensions.
  213. *
  214. * @param $image
  215. * An image object returned by image_load().
  216. * @param $width
  217. * The target width, in pixels. If this value is NULL then the scaling will
  218. * be based only on the height value.
  219. * @param $height
  220. * The target height, in pixels. If this value is NULL then the scaling will
  221. * be based only on the width value.
  222. * @param $upscale
  223. * Boolean indicating that files smaller than the dimensions will be scaled
  224. * up. This generally results in a low quality image.
  225. *
  226. * @return
  227. * TRUE on success, FALSE on failure.
  228. *
  229. * @see image_dimensions_scale()
  230. * @see image_load()
  231. * @see image_scale_and_crop()
  232. */
  233. function image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) {
  234. $dimensions = $image->info;
  235. // Scale the dimensions - if they don't change then just return success.
  236. if (!image_dimensions_scale($dimensions, $width, $height, $upscale)) {
  237. return TRUE;
  238. }
  239. return image_resize($image, $dimensions['width'], $dimensions['height']);
  240. }
  241. /**
  242. * Resizes an image to the given dimensions (ignoring aspect ratio).
  243. *
  244. * @param $image
  245. * An image object returned by image_load().
  246. * @param $width
  247. * The target width, in pixels.
  248. * @param $height
  249. * The target height, in pixels.
  250. *
  251. * @return
  252. * TRUE on success, FALSE on failure.
  253. *
  254. * @see image_load()
  255. * @see image_gd_resize()
  256. */
  257. function image_resize(stdClass $image, $width, $height) {
  258. $width = (int) round($width);
  259. $height = (int) round($height);
  260. return image_toolkit_invoke('resize', $image, array($width, $height));
  261. }
  262. /**
  263. * Rotates an image by the given number of degrees.
  264. *
  265. * @param $image
  266. * An image object returned by image_load().
  267. * @param $degrees
  268. * The number of (clockwise) degrees to rotate the image.
  269. * @param $background
  270. * An hexadecimal integer specifying the background color to use for the
  271. * uncovered area of the image after the rotation. E.g. 0x000000 for black,
  272. * 0xff00ff for magenta, and 0xffffff for white. For images that support
  273. * transparency, this will default to transparent. Otherwise it will
  274. * be white.
  275. *
  276. * @return
  277. * TRUE on success, FALSE on failure.
  278. *
  279. * @see image_load()
  280. * @see image_gd_rotate()
  281. */
  282. function image_rotate(stdClass $image, $degrees, $background = NULL) {
  283. return image_toolkit_invoke('rotate', $image, array($degrees, $background));
  284. }
  285. /**
  286. * Crops an image to a rectangle specified by the given dimensions.
  287. *
  288. * @param $image
  289. * An image object returned by image_load().
  290. * @param $x
  291. * The top left coordinate, in pixels, of the crop area (x axis value).
  292. * @param $y
  293. * The top left coordinate, in pixels, of the crop area (y axis value).
  294. * @param $width
  295. * The target width, in pixels.
  296. * @param $height
  297. * The target height, in pixels.
  298. *
  299. * @return
  300. * TRUE on success, FALSE on failure.
  301. *
  302. * @see image_load()
  303. * @see image_scale_and_crop()
  304. * @see image_gd_crop()
  305. */
  306. function image_crop(stdClass $image, $x, $y, $width, $height) {
  307. $aspect = $image->info['height'] / $image->info['width'];
  308. if (empty($height)) $height = $width / $aspect;
  309. if (empty($width)) $width = $height * $aspect;
  310. $width = (int) round($width);
  311. $height = (int) round($height);
  312. return image_toolkit_invoke('crop', $image, array($x, $y, $width, $height));
  313. }
  314. /**
  315. * Converts an image to grayscale.
  316. *
  317. * @param $image
  318. * An image object returned by image_load().
  319. *
  320. * @return
  321. * TRUE on success, FALSE on failure.
  322. *
  323. * @see image_load()
  324. * @see image_gd_desaturate()
  325. */
  326. function image_desaturate(stdClass $image) {
  327. return image_toolkit_invoke('desaturate', $image);
  328. }
  329. /**
  330. * Loads an image file and returns an image object.
  331. *
  332. * Any changes to the file are not saved until image_save() is called.
  333. *
  334. * @param $file
  335. * Path to an image file.
  336. * @param $toolkit
  337. * An optional, image toolkit name to override the default.
  338. *
  339. * @return
  340. * An image object or FALSE if there was a problem loading the file. The
  341. * image object has the following properties:
  342. * - 'source' - The original file path.
  343. * - 'info' - The array of information returned by image_get_info()
  344. * - 'toolkit' - The name of the image toolkit requested when the image was
  345. * loaded.
  346. * Image toolkits may add additional properties. The caller is advised not to
  347. * monkey about with them.
  348. *
  349. * @see image_save()
  350. * @see image_get_info()
  351. * @see image_get_available_toolkits()
  352. * @see image_gd_load()
  353. */
  354. function image_load($file, $toolkit = FALSE) {
  355. if (!$toolkit) {
  356. $toolkit = image_get_toolkit();
  357. }
  358. if ($toolkit) {
  359. $image = new stdClass();
  360. $image->source = $file;
  361. $image->info = image_get_info($file, $toolkit);
  362. if (isset($image->info) && is_array($image->info)) {
  363. $image->toolkit = $toolkit;
  364. if (image_toolkit_invoke('load', $image)) {
  365. return $image;
  366. }
  367. }
  368. }
  369. return FALSE;
  370. }
  371. /**
  372. * Closes the image and saves the changes to a file.
  373. *
  374. * @param $image
  375. * An image object returned by image_load(). The object's 'info' property
  376. * will be updated if the file is saved successfully.
  377. * @param $destination
  378. * Destination path where the image should be saved. If it is empty the
  379. * original image file will be overwritten.
  380. *
  381. * @return
  382. * TRUE on success, FALSE on failure.
  383. *
  384. * @see image_load()
  385. * @see image_gd_save()
  386. */
  387. function image_save(stdClass $image, $destination = NULL) {
  388. if (empty($destination)) {
  389. $destination = $image->source;
  390. }
  391. if ($return = image_toolkit_invoke('save', $image, array($destination))) {
  392. // Clear the cached file size and refresh the image information.
  393. clearstatcache();
  394. $image->info = image_get_info($destination, $image->toolkit);
  395. if (drupal_chmod($destination)) {
  396. return $return;
  397. }
  398. }
  399. return FALSE;
  400. }
  401. /**
  402. * @} End of "defgroup image".
  403. */