image.inc

  1. drupal
    1. 4.6 includes/image.inc
    2. 4.7 includes/image.inc
    3. 5 includes/image.inc
    4. 6 includes/image.inc
    5. 7 includes/image.inc
    6. 8 core/includes/image.inc

Functions & methods

NameDescription
image_cropCrop an image to the rectangle specified by the given rectangle.
image_gd_check_settingsVerify GD2 settings (that the right version is actually installed).
image_gd_closeGD helper to write an image resource to a destination file.
image_gd_cropCrop an image using the GD toolkit.
image_gd_openGD helper function to create an image resource from a file.
image_gd_resizeScale an image to the specified size using GD.
image_gd_rotateRotate an image the given number of degrees.
image_gd_settingsRetrieve settings for the GD2 toolkit (not used).
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_resizeResize an image to the given dimensions (ignoring aspect ratio).
image_rotateRotate an image by the given number of degrees.
image_scaleScales an image to the given width and height while maintaining aspect ratio.
image_toolkit_invokeInvokes the given method using the currently selected toolkit.
image_toolkit_validateValidates toolkit selection.

File

includes/image.inc
View source
  1. <?php
  2. /**
  3. * Return a list of available toolkits.
  4. *
  5. * @return An array of toolkit name => descriptive title.
  6. */
  7. function image_get_available_toolkits() {
  8. $toolkits = file_scan_directory('includes', 'image\..*\.inc$');
  9. $output = array();
  10. foreach ($toolkits as $file => $toolkit) {
  11. include_once "./$file";
  12. $function = str_replace('.', '_', $toolkit->name) .'_info';
  13. if (function_exists($function)) {
  14. $info = $function();
  15. $output[$info['name']] = $info['title'];
  16. }
  17. }
  18. $output['gd'] = t('Built-in GD2 toolkit');
  19. return $output;
  20. }
  21. /**
  22. * Retrieve the name of the currently used toolkit.
  23. *
  24. * @return String containing the name of the toolkit.
  25. */
  26. function image_get_toolkit() {
  27. static $toolkit;
  28. if (!$toolkit) {
  29. $toolkit = variable_get('image_toolkit', 'gd');
  30. $toolkit_file = './includes/image.'.$toolkit.'.inc';
  31. if ($toolkit != 'gd' && file_exists($toolkit_file)) {
  32. include_once $toolkit_file;
  33. }
  34. elseif (!image_gd_check_settings()) {
  35. $toolkit = false;
  36. }
  37. }
  38. return $toolkit;
  39. }
  40. /**
  41. * Validates toolkit selection.
  42. */
  43. function image_toolkit_validate($formelement, $fieldname) {
  44. if ($formelement['#value'] == 'gd' && image_gd_check_settings() == false) {
  45. form_set_error('image_toolkit', t("The built-in GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see %url.", array('%url' => '<a href="http://php.net/image">http://php.net/image</a>')));
  46. }
  47. }
  48. /**
  49. * Invokes the given method using the currently selected toolkit.
  50. *
  51. * @param $method A string containing the method to invoke.
  52. * @param $params An optional array of parameters to pass to the toolkit method.
  53. *
  54. * @return Mixed values (typically Boolean for successful operation).
  55. */
  56. function image_toolkit_invoke($method, $params = array()) {
  57. if ($toolkit = image_get_toolkit()) {
  58. $function = 'image_'. $toolkit .'_'. $method;
  59. if (function_exists($function)) {
  60. return call_user_func_array($function, $params);
  61. }
  62. else {
  63. watchdog('php', t("The selected image handling toolkit '%toolkit' can not correctly process '%function'.", array('%toolkit' => "<em>$toolkit</em>", '%function' => "<em>$function</em>")), WATCHDOG_ERROR);
  64. return false;
  65. }
  66. }
  67. else {
  68. if ($method == 'settings') {
  69. return image_gd_settings();
  70. }
  71. }
  72. }
  73. /**
  74. * Get details about an image.
  75. *
  76. * @return array containing information about the image
  77. * 'width': image's width in pixels
  78. * 'height': image's height in pixels
  79. * 'extension': commonly used extension for the image
  80. * 'mime_type': image's MIME type ('image/jpeg', 'image/gif', etc.)
  81. * 'file_size': image's physical size (in bytes)
  82. */
  83. function image_get_info($file) {
  84. if (!is_file($file)) {
  85. return false;
  86. }
  87. $details = false;
  88. $data = @getimagesize($file);
  89. $file_size = @filesize($file);
  90. if (isset($data) && is_array($data)) {
  91. $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
  92. $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
  93. $details = array('width' => $data[0],
  94. 'height' => $data[1],
  95. 'extension' => $extension,
  96. 'file_size' => $file_size,
  97. 'mime_type' => $data['mime']);
  98. }
  99. return $details;
  100. }
  101. /**
  102. * Scales an image to the given width and height while maintaining aspect
  103. * ratio.
  104. *
  105. * @param $source The filepath of the source image
  106. * @param $destination The file path of the destination image
  107. * @param $width The target width
  108. * @param $height The target height
  109. *
  110. * @return True or false, based on success
  111. */
  112. function image_scale($source, $destination, $width, $height) {
  113. $info = image_get_info($source);
  114. // don't scale up
  115. if ($width >= $info['width'] && $height >= $info['height']) {
  116. return false;
  117. }
  118. $aspect = $info['height'] / $info['width'];
  119. if ($aspect < $height / $width) {
  120. $width = (int)min($width, $info['width']);
  121. $height = (int)round($width * $aspect);
  122. }
  123. else {
  124. $height = (int)min($height, $info['height']);
  125. $width = (int)round($height / $aspect);
  126. }
  127. return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
  128. }
  129. /**
  130. * Resize an image to the given dimensions (ignoring aspect ratio).
  131. *
  132. * @param $source The filepath of the source image.
  133. * @param $destination The file path of the destination image.
  134. * @param $width The target width.
  135. * @param $height The target height.
  136. */
  137. function image_resize($source, $destination, $width, $height) {
  138. return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
  139. }
  140. /**
  141. * Rotate an image by the given number of degrees.
  142. *
  143. * @param $source The filepath of the source image
  144. * @param $destination The file path of the destination image
  145. * @param $degrees The number of (clockwise) degrees to rotate the image
  146. */
  147. function image_rotate($source, $destination, $degrees) {
  148. return image_toolkit_invoke('rotate', array($source, $destination, $degrees));
  149. }
  150. /**
  151. * Crop an image to the rectangle specified by the given rectangle.
  152. *
  153. * @param $source The filepath of the source image
  154. * @param $destination The file path of the destination image
  155. * @param $x The top left co-ordinate of the crop area (x axis value)
  156. * @param $y The top left co-ordinate of the crop area (y axis value)
  157. * @param $width The target width
  158. * @param $height The target height
  159. */
  160. function image_crop($source, $destination, $x, $y, $width, $height) {
  161. return image_toolkit_invoke('crop', array($source, $destination, $x, $y, $width, $height));
  162. }
  163. /**
  164. * GD2 toolkit functions
  165. * With the minimal requirements of PHP 4.3 for Drupal, we use the built-in version of GD.
  166. */
  167. /**
  168. * Retrieve settings for the GD2 toolkit (not used).
  169. */
  170. function image_gd_settings() {
  171. if (image_gd_check_settings()) {
  172. return array('#type' => 'item', '#value' => t('The built-in GD2 toolkit is installed and working properly.'));
  173. }
  174. else {
  175. return array('#type' => 'item', '#value' => t('The built-in GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see %url.', array('%url' => l('http://php.net/image', 'http://php.net/image'))));
  176. }
  177. }
  178. /**
  179. * Verify GD2 settings (that the right version is actually installed).
  180. *
  181. * @return boolean
  182. */
  183. function image_gd_check_settings() {
  184. if ($check = get_extension_funcs('gd')) {
  185. if (in_array('imagegd2', $check)) {
  186. // GD2 support is available.
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Scale an image to the specified size using GD.
  194. */
  195. function image_gd_resize($source, $destination, $width, $height) {
  196. if (!file_exists($source)) {
  197. return false;
  198. }
  199. $info = image_get_info($source);
  200. if (!$info) {
  201. return false;
  202. }
  203. $im = image_gd_open($source, $info['extension']);
  204. if (!$im) {
  205. return false;
  206. }
  207. $res = imageCreateTrueColor($width, $height);
  208. if ($info['extension'] == 'png') {
  209. $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
  210. imagealphablending($res, FALSE);
  211. imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
  212. imagealphablending($res, TRUE);
  213. imagesavealpha($res, TRUE);
  214. }
  215. imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
  216. $result = image_gd_close($res, $destination, $info['extension']);
  217. imageDestroy($res);
  218. imageDestroy($im);
  219. return $result;
  220. }
  221. /**
  222. * Rotate an image the given number of degrees.
  223. */
  224. function image_gd_rotate($source, $destination, $degrees, $bg_color = 0) {
  225. if (!function_exists('imageRotate')) {
  226. return false;
  227. }
  228. $info = image_get_info($source);
  229. if (!$info) {
  230. return false;
  231. }
  232. $im = image_gd_open($source, $info['extension']);
  233. if (!$im) {
  234. return false;
  235. }
  236. $res = imageRotate($im, $degrees, $bg_color);
  237. $result = image_gd_close($res, $destination, $info['extension']);
  238. return $result;
  239. }
  240. /**
  241. * Crop an image using the GD toolkit.
  242. */
  243. function image_gd_crop($source, $destination, $x, $y, $width, $height) {
  244. $info = image_get_info($source);
  245. if (!$info) {
  246. return false;
  247. }
  248. $im = image_gd_open($source, $info['extension']);
  249. $res = imageCreateTrueColor($width, $height);
  250. imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
  251. $result = image_gd_close($res, $destination, $info['extension']);
  252. imageDestroy($res);
  253. imageDestroy($im);
  254. return $result;
  255. }
  256. /**
  257. * GD helper function to create an image resource from a file.
  258. */
  259. function image_gd_open($file, $extension) {
  260. $extension = str_replace('jpg', 'jpeg', $extension);
  261. $open_func = 'imageCreateFrom'. $extension;
  262. if (!function_exists($open_func)) {
  263. return false;
  264. }
  265. return $open_func($file);
  266. }
  267. /**
  268. * GD helper to write an image resource to a destination file.
  269. */
  270. function image_gd_close($res, $destination, $extension) {
  271. $extension = str_replace('jpg', 'jpeg', $extension);
  272. $close_func = 'image'. $extension;
  273. if (!function_exists($close_func)) {
  274. return false;
  275. }
  276. return $close_func($res, $destination);
  277. }
Login or register to post comments