image.gd.inc

  1. 7.x drupal-7.x/modules/system/image.gd.inc
  2. 6.x drupal-6.x/includes/image.gd.inc

GD2 toolkit for image manipulation within Drupal.

File

drupal-6.x/includes/image.gd.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * GD2 toolkit for image manipulation within Drupal.
  5. */
  6. /**
  7. * @ingroup image
  8. * @{
  9. */
  10. /**
  11. * Retrieve information about the toolkit.
  12. */
  13. function image_gd_info() {
  14. return array('name' => 'gd', 'title' => t('GD2 image manipulation toolkit'));
  15. }
  16. /**
  17. * Retrieve settings for the GD2 toolkit.
  18. */
  19. function image_gd_settings() {
  20. if (image_gd_check_settings()) {
  21. $form = array();
  22. $form['status'] = array(
  23. '#value' => t('The GD toolkit is installed and working properly.')
  24. );
  25. $form['image_jpeg_quality'] = array(
  26. '#type' => 'textfield',
  27. '#title' => t('JPEG quality'),
  28. '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
  29. '#size' => 10,
  30. '#maxlength' => 3,
  31. '#default_value' => variable_get('image_jpeg_quality', 75),
  32. '#field_suffix' => t('%'),
  33. );
  34. $form['#element_validate'] = array('image_gd_settings_validate');
  35. return $form;
  36. }
  37. else {
  38. form_set_error('image_toolkit', t('The GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see <a href="@url">PHP\'s image documentation</a>.', array('@url' => 'http://php.net/image')));
  39. return FALSE;
  40. }
  41. }
  42. /**
  43. * Validate the submitted GD settings.
  44. */
  45. function image_gd_settings_validate($form, &$form_state) {
  46. // Validate image quality range.
  47. $value = $form_state['values']['image_jpeg_quality'];
  48. if (!is_numeric($value) || $value < 0 || $value > 100) {
  49. form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.'));
  50. }
  51. }
  52. /**
  53. * Verify GD2 settings (that the right version is actually installed).
  54. *
  55. * @return
  56. * A boolean indicating if the GD toolkit is avaiable on this machine.
  57. */
  58. function image_gd_check_settings() {
  59. if ($check = get_extension_funcs('gd')) {
  60. if (in_array('imagegd2', $check)) {
  61. // GD2 support is available.
  62. return TRUE;
  63. }
  64. }
  65. return FALSE;
  66. }
  67. /**
  68. * Scale an image to the specified size using GD.
  69. */
  70. function image_gd_resize($source, $destination, $width, $height) {
  71. if (!file_exists($source)) {
  72. return FALSE;
  73. }
  74. $info = image_get_info($source);
  75. if (!$info) {
  76. return FALSE;
  77. }
  78. $im = image_gd_open($source, $info['extension']);
  79. if (!$im) {
  80. return FALSE;
  81. }
  82. $res = imagecreatetruecolor($width, $height);
  83. if ($info['extension'] == 'png') {
  84. $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
  85. imagealphablending($res, FALSE);
  86. imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
  87. imagealphablending($res, TRUE);
  88. imagesavealpha($res, TRUE);
  89. }
  90. elseif ($info['extension'] == 'gif') {
  91. // If we have a specific transparent color.
  92. $transparency_index = imagecolortransparent($im);
  93. if ($transparency_index >= 0) {
  94. // Get the original image's transparent color's RGB values.
  95. $transparent_color = imagecolorsforindex($im, $transparency_index);
  96. // Allocate the same color in the new image resource.
  97. $transparency_index = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  98. // Completely fill the background of the new image with allocated color.
  99. imagefill($res, 0, 0, $transparency_index);
  100. // Set the background color for new image to transparent.
  101. imagecolortransparent($res, $transparency_index);
  102. // Find number of colors in the images palette.
  103. $number_colors = imagecolorstotal($im);
  104. // Convert from true color to palette to fix transparency issues.
  105. imagetruecolortopalette($res, TRUE, $number_colors);
  106. }
  107. }
  108. imagecopyresampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
  109. $result = image_gd_close($res, $destination, $info['extension']);
  110. imagedestroy($res);
  111. imagedestroy($im);
  112. return $result;
  113. }
  114. /**
  115. * Rotate an image the given number of degrees.
  116. */
  117. function image_gd_rotate($source, $destination, $degrees, $background = 0x000000) {
  118. if (!function_exists('imageRotate')) {
  119. return FALSE;
  120. }
  121. $info = image_get_info($source);
  122. if (!$info) {
  123. return FALSE;
  124. }
  125. $im = image_gd_open($source, $info['extension']);
  126. if (!$im) {
  127. return FALSE;
  128. }
  129. $res = imageRotate($im, $degrees, $background);
  130. $result = image_gd_close($res, $destination, $info['extension']);
  131. return $result;
  132. }
  133. /**
  134. * Crop an image using the GD toolkit.
  135. */
  136. function image_gd_crop($source, $destination, $x, $y, $width, $height) {
  137. $info = image_get_info($source);
  138. if (!$info) {
  139. return FALSE;
  140. }
  141. $im = image_gd_open($source, $info['extension']);
  142. $res = imageCreateTrueColor($width, $height);
  143. imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
  144. $result = image_gd_close($res, $destination, $info['extension']);
  145. imageDestroy($res);
  146. imageDestroy($im);
  147. return $result;
  148. }
  149. /**
  150. * GD helper function to create an image resource from a file.
  151. *
  152. * @param $file
  153. * A string file path where the iamge should be saved.
  154. * @param $extension
  155. * A string containing one of the following extensions: gif, jpg, jpeg, png.
  156. * @return
  157. * An image resource, or FALSE on error.
  158. */
  159. function image_gd_open($file, $extension) {
  160. $extension = str_replace('jpg', 'jpeg', $extension);
  161. $open_func = 'imageCreateFrom'. $extension;
  162. if (!function_exists($open_func)) {
  163. return FALSE;
  164. }
  165. return $open_func($file);
  166. }
  167. /**
  168. * GD helper to write an image resource to a destination file.
  169. *
  170. * @param $res
  171. * An image resource created with image_gd_open().
  172. * @param $destination
  173. * A string file path where the iamge should be saved.
  174. * @param $extension
  175. * A string containing one of the following extensions: gif, jpg, jpeg, png.
  176. * @return
  177. * Boolean indicating success.
  178. */
  179. function image_gd_close($res, $destination, $extension) {
  180. $extension = str_replace('jpg', 'jpeg', $extension);
  181. $close_func = 'image'. $extension;
  182. if (!function_exists($close_func)) {
  183. return FALSE;
  184. }
  185. if ($extension == 'jpeg') {
  186. return $close_func($res, $destination, variable_get('image_jpeg_quality', 75));
  187. }
  188. else {
  189. return $close_func($res, $destination);
  190. }
  191. }
  192. /**
  193. * @} End of "ingroup image".
  194. */