image.gd.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * @file
  4. * GD2 toolkit for image manipulation within Drupal.
  5. */
  6. /**
  7. * @addtogroup image
  8. * @{
  9. */
  10. /**
  11. * Retrieve settings for the GD2 toolkit.
  12. */
  13. function image_gd_settings() {
  14. if (image_gd_check_settings()) {
  15. $form['status'] = array(
  16. '#markup' => t('The GD toolkit is installed and working properly.')
  17. );
  18. $form['image_jpeg_quality'] = array(
  19. '#type' => 'textfield',
  20. '#title' => t('JPEG quality'),
  21. '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
  22. '#size' => 10,
  23. '#maxlength' => 3,
  24. '#default_value' => variable_get('image_jpeg_quality', 75),
  25. '#field_suffix' => t('%'),
  26. );
  27. $form['#element_validate'] = array('image_gd_settings_validate');
  28. return $form;
  29. }
  30. else {
  31. 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')));
  32. return FALSE;
  33. }
  34. }
  35. /**
  36. * Validate the submitted GD settings.
  37. */
  38. function image_gd_settings_validate($form, &$form_state) {
  39. // Validate image quality range.
  40. $value = $form_state['values']['image_jpeg_quality'];
  41. if (!is_numeric($value) || $value < 0 || $value > 100) {
  42. form_set_error('image_jpeg_quality', t('JPEG quality must be a number between 0 and 100.'));
  43. }
  44. }
  45. /**
  46. * Verify GD2 settings (that the right version is actually installed).
  47. *
  48. * @return
  49. * A boolean indicating if the GD toolkit is available on this machine.
  50. */
  51. function image_gd_check_settings() {
  52. if ($check = get_extension_funcs('gd')) {
  53. if (in_array('imagegd2', $check)) {
  54. // GD2 support is available.
  55. return TRUE;
  56. }
  57. }
  58. return FALSE;
  59. }
  60. /**
  61. * Scale an image to the specified size using GD.
  62. *
  63. * @param $image
  64. * An image object. The $image->resource, $image->info['width'], and
  65. * $image->info['height'] values will be modified by this call.
  66. * @param $width
  67. * The new width of the resized image, in pixels.
  68. * @param $height
  69. * The new height of the resized image, in pixels.
  70. * @return
  71. * TRUE or FALSE, based on success.
  72. *
  73. * @see image_resize()
  74. */
  75. function image_gd_resize(stdClass $image, $width, $height) {
  76. $res = image_gd_create_tmp($image, $width, $height);
  77. if (!imagecopyresampled($res, $image->resource, 0, 0, 0, 0, $width, $height, $image->info['width'], $image->info['height'])) {
  78. return FALSE;
  79. }
  80. imagedestroy($image->resource);
  81. // Update image object.
  82. $image->resource = $res;
  83. $image->info['width'] = $width;
  84. $image->info['height'] = $height;
  85. return TRUE;
  86. }
  87. /**
  88. * Rotate an image the given number of degrees.
  89. *
  90. * @param $image
  91. * An image object. The $image->resource, $image->info['width'], and
  92. * $image->info['height'] values will be modified by this call.
  93. * @param $degrees
  94. * The number of (clockwise) degrees to rotate the image.
  95. * @param $background
  96. * An hexadecimal integer specifying the background color to use for the
  97. * uncovered area of the image after the rotation. E.g. 0x000000 for black,
  98. * 0xff00ff for magenta, and 0xffffff for white. For images that support
  99. * transparency, this will default to transparent. Otherwise it will
  100. * be white.
  101. * @return
  102. * TRUE or FALSE, based on success.
  103. *
  104. * @see image_rotate()
  105. */
  106. function image_gd_rotate(stdClass $image, $degrees, $background = NULL) {
  107. // PHP installations using non-bundled GD do not have imagerotate.
  108. if (!function_exists('imagerotate')) {
  109. watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->source));
  110. return FALSE;
  111. }
  112. $width = $image->info['width'];
  113. $height = $image->info['height'];
  114. // Convert the hexadecimal background value to a color index value.
  115. if (isset($background)) {
  116. $rgb = array();
  117. for ($i = 16; $i >= 0; $i -= 8) {
  118. $rgb[] = (($background >> $i) & 0xFF);
  119. }
  120. $background = imagecolorallocatealpha($image->resource, $rgb[0], $rgb[1], $rgb[2], 0);
  121. }
  122. // Set the background color as transparent if $background is NULL.
  123. else {
  124. // Get the current transparent color.
  125. $background = imagecolortransparent($image->resource);
  126. // If no transparent colors, use white.
  127. if ($background == 0) {
  128. $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0);
  129. }
  130. }
  131. // Images are assigned a new color palette when rotating, removing any
  132. // transparency flags. For GIF images, keep a record of the transparent color.
  133. if ($image->info['extension'] == 'gif') {
  134. $transparent_index = imagecolortransparent($image->resource);
  135. if ($transparent_index != 0) {
  136. $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index);
  137. }
  138. }
  139. $image->resource = imagerotate($image->resource, 360 - $degrees, $background);
  140. // GIFs need to reassign the transparent color after performing the rotate.
  141. if (isset($transparent_gif_color)) {
  142. $background = imagecolorexactalpha($image->resource, $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']);
  143. imagecolortransparent($image->resource, $background);
  144. }
  145. $image->info['width'] = imagesx($image->resource);
  146. $image->info['height'] = imagesy($image->resource);
  147. return TRUE;
  148. }
  149. /**
  150. * Crop an image using the GD toolkit.
  151. *
  152. * @param $image
  153. * An image object. The $image->resource, $image->info['width'], and
  154. * $image->info['height'] values will be modified by this call.
  155. * @param $x
  156. * The starting x offset at which to start the crop, in pixels.
  157. * @param $y
  158. * The starting y offset at which to start the crop, in pixels.
  159. * @param $width
  160. * The width of the cropped area, in pixels.
  161. * @param $height
  162. * The height of the cropped area, in pixels.
  163. * @return
  164. * TRUE or FALSE, based on success.
  165. *
  166. * @see image_crop()
  167. */
  168. function image_gd_crop(stdClass $image, $x, $y, $width, $height) {
  169. $res = image_gd_create_tmp($image, $width, $height);
  170. if (!imagecopyresampled($res, $image->resource, 0, 0, $x, $y, $width, $height, $width, $height)) {
  171. return FALSE;
  172. }
  173. // Destroy the original image and return the modified image.
  174. imagedestroy($image->resource);
  175. $image->resource = $res;
  176. $image->info['width'] = $width;
  177. $image->info['height'] = $height;
  178. return TRUE;
  179. }
  180. /**
  181. * Convert an image resource to grayscale.
  182. *
  183. * Note that transparent GIFs loose transparency when desaturated.
  184. *
  185. * @param $image
  186. * An image object. The $image->resource value will be modified by this call.
  187. * @return
  188. * TRUE or FALSE, based on success.
  189. *
  190. * @see image_desaturate()
  191. */
  192. function image_gd_desaturate(stdClass $image) {
  193. // PHP installations using non-bundled GD do not have imagefilter.
  194. if (!function_exists('imagefilter')) {
  195. watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->source));
  196. return FALSE;
  197. }
  198. return imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
  199. }
  200. /**
  201. * GD helper function to create an image resource from a file.
  202. *
  203. * @param $image
  204. * An image object. The $image->resource value will populated by this call.
  205. * @return
  206. * TRUE or FALSE, based on success.
  207. *
  208. * @see image_load()
  209. */
  210. function image_gd_load(stdClass $image) {
  211. $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
  212. $function = 'imagecreatefrom' . $extension;
  213. return (function_exists($function) && $image->resource = $function($image->source));
  214. }
  215. /**
  216. * GD helper to write an image resource to a destination file.
  217. *
  218. * @param $image
  219. * An image object.
  220. * @param $destination
  221. * A string file URI or path where the image should be saved.
  222. * @return
  223. * TRUE or FALSE, based on success.
  224. *
  225. * @see image_save()
  226. */
  227. function image_gd_save(stdClass $image, $destination) {
  228. $scheme = file_uri_scheme($destination);
  229. // Work around lack of stream wrapper support in imagejpeg() and imagepng().
  230. if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
  231. // If destination is not local, save image to temporary local file.
  232. $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
  233. if (!isset($local_wrappers[$scheme])) {
  234. $permanent_destination = $destination;
  235. $destination = drupal_tempnam('temporary://', 'gd_');
  236. }
  237. // Convert stream wrapper URI to normal path.
  238. $destination = drupal_realpath($destination);
  239. }
  240. $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
  241. $function = 'image' . $extension;
  242. if (!function_exists($function)) {
  243. return FALSE;
  244. }
  245. if ($extension == 'jpeg') {
  246. $success = $function($image->resource, $destination, variable_get('image_jpeg_quality', 75));
  247. }
  248. else {
  249. // Always save PNG images with full transparency.
  250. if ($extension == 'png') {
  251. imagealphablending($image->resource, FALSE);
  252. imagesavealpha($image->resource, TRUE);
  253. }
  254. $success = $function($image->resource, $destination);
  255. }
  256. // Move temporary local file to remote destination.
  257. if (isset($permanent_destination) && $success) {
  258. return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
  259. }
  260. return $success;
  261. }
  262. /**
  263. * Create a truecolor image preserving transparency from a provided image.
  264. *
  265. * @param $image
  266. * An image object.
  267. * @param $width
  268. * The new width of the new image, in pixels.
  269. * @param $height
  270. * The new height of the new image, in pixels.
  271. * @return
  272. * A GD image handle.
  273. */
  274. function image_gd_create_tmp(stdClass $image, $width, $height) {
  275. $res = imagecreatetruecolor($width, $height);
  276. if ($image->info['extension'] == 'gif') {
  277. // Grab transparent color index from image resource.
  278. $transparent = imagecolortransparent($image->resource);
  279. if ($transparent >= 0) {
  280. // The original must have a transparent color, allocate to the new image.
  281. $transparent_color = imagecolorsforindex($image->resource, $transparent);
  282. $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  283. // Flood with our new transparent color.
  284. imagefill($res, 0, 0, $transparent);
  285. imagecolortransparent($res, $transparent);
  286. }
  287. }
  288. elseif ($image->info['extension'] == 'png') {
  289. imagealphablending($res, FALSE);
  290. $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
  291. imagefill($res, 0, 0, $transparency);
  292. imagealphablending($res, TRUE);
  293. imagesavealpha($res, TRUE);
  294. }
  295. else {
  296. imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
  297. }
  298. return $res;
  299. }
  300. /**
  301. * Get details about an image.
  302. *
  303. * @param $image
  304. * An image object.
  305. * @return
  306. * FALSE, if the file could not be found or is not an image. Otherwise, a
  307. * keyed array containing information about the image:
  308. * - "width": Width, in pixels.
  309. * - "height": Height, in pixels.
  310. * - "extension": Commonly used file extension for the image.
  311. * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
  312. *
  313. * @see image_get_info()
  314. */
  315. function image_gd_get_info(stdClass $image) {
  316. $details = FALSE;
  317. $data = getimagesize($image->source);
  318. if (isset($data) && is_array($data)) {
  319. $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
  320. $extension = isset($extensions[$data[2]]) ? $extensions[$data[2]] : '';
  321. $details = array(
  322. 'width' => $data[0],
  323. 'height' => $data[1],
  324. 'extension' => $extension,
  325. 'mime_type' => $data['mime'],
  326. );
  327. }
  328. return $details;
  329. }
  330. /**
  331. * @} End of "addtogroup image".
  332. */