image.effects.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file
  4. * Functions needed to execute image effects provided by Image module.
  5. */
  6. /**
  7. * Implements hook_image_effect_info().
  8. */
  9. function image_image_effect_info() {
  10. $effects = array(
  11. 'image_resize' => array(
  12. 'label' => t('Resize'),
  13. 'help' => t('Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.'),
  14. 'effect callback' => 'image_resize_effect',
  15. 'dimensions callback' => 'image_resize_dimensions',
  16. 'form callback' => 'image_resize_form',
  17. 'summary theme' => 'image_resize_summary',
  18. ),
  19. 'image_scale' => array(
  20. 'label' => t('Scale'),
  21. 'help' => t('Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.'),
  22. 'effect callback' => 'image_scale_effect',
  23. 'dimensions callback' => 'image_scale_dimensions',
  24. 'form callback' => 'image_scale_form',
  25. 'summary theme' => 'image_scale_summary',
  26. ),
  27. 'image_scale_and_crop' => array(
  28. 'label' => t('Scale and crop'),
  29. 'help' => t('Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.'),
  30. 'effect callback' => 'image_scale_and_crop_effect',
  31. 'dimensions callback' => 'image_resize_dimensions',
  32. 'form callback' => 'image_resize_form',
  33. 'summary theme' => 'image_resize_summary',
  34. ),
  35. 'image_crop' => array(
  36. 'label' => t('Crop'),
  37. 'help' => t('Cropping will remove portions of an image to make it the specified dimensions.'),
  38. 'effect callback' => 'image_crop_effect',
  39. 'dimensions callback' => 'image_resize_dimensions',
  40. 'form callback' => 'image_crop_form',
  41. 'summary theme' => 'image_crop_summary',
  42. ),
  43. 'image_desaturate' => array(
  44. 'label' => t('Desaturate'),
  45. 'help' => t('Desaturate converts an image to grayscale.'),
  46. 'effect callback' => 'image_desaturate_effect',
  47. 'dimensions passthrough' => TRUE,
  48. ),
  49. 'image_rotate' => array(
  50. 'label' => t('Rotate'),
  51. 'help' => t('Rotating an image may cause the dimensions of an image to increase to fit the diagonal.'),
  52. 'effect callback' => 'image_rotate_effect',
  53. 'dimensions callback' => 'image_rotate_dimensions',
  54. 'form callback' => 'image_rotate_form',
  55. 'summary theme' => 'image_rotate_summary',
  56. ),
  57. );
  58. return $effects;
  59. }
  60. /**
  61. * Image effect callback; Resize an image resource.
  62. *
  63. * @param $image
  64. * An image object returned by image_load().
  65. * @param $data
  66. * An array of attributes to use when performing the resize effect with the
  67. * following items:
  68. * - "width": An integer representing the desired width in pixels.
  69. * - "height": An integer representing the desired height in pixels.
  70. *
  71. * @return
  72. * TRUE on success. FALSE on failure to resize image.
  73. *
  74. * @see image_resize()
  75. */
  76. function image_resize_effect(&$image, $data) {
  77. if (!image_resize($image, $data['width'], $data['height'])) {
  78. watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
  79. return FALSE;
  80. }
  81. return TRUE;
  82. }
  83. /**
  84. * Image dimensions callback; Resize.
  85. *
  86. * @param $dimensions
  87. * Dimensions to be modified - an array with components width and height, in
  88. * pixels.
  89. * @param $data
  90. * An array of attributes to use when performing the resize effect with the
  91. * following items:
  92. * - "width": An integer representing the desired width in pixels.
  93. * - "height": An integer representing the desired height in pixels.
  94. */
  95. function image_resize_dimensions(array &$dimensions, array $data) {
  96. // The new image will have the exact dimensions defined for the effect.
  97. $dimensions['width'] = $data['width'];
  98. $dimensions['height'] = $data['height'];
  99. }
  100. /**
  101. * Image effect callback; Scale an image resource.
  102. *
  103. * @param $image
  104. * An image object returned by image_load().
  105. * @param $data
  106. * An array of attributes to use when performing the scale effect with the
  107. * following items:
  108. * - "width": An integer representing the desired width in pixels.
  109. * - "height": An integer representing the desired height in pixels.
  110. * - "upscale": A boolean indicating that the image should be upscaled if the
  111. * dimensions are larger than the original image.
  112. *
  113. * @return
  114. * TRUE on success. FALSE on failure to scale image.
  115. *
  116. * @see image_scale()
  117. */
  118. function image_scale_effect(&$image, $data) {
  119. // Set sane default values.
  120. $data += array(
  121. 'width' => NULL,
  122. 'height' => NULL,
  123. 'upscale' => FALSE,
  124. );
  125. if (!image_scale($image, $data['width'], $data['height'], $data['upscale'])) {
  126. watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
  127. return FALSE;
  128. }
  129. return TRUE;
  130. }
  131. /**
  132. * Image dimensions callback; Scale.
  133. *
  134. * @param $dimensions
  135. * Dimensions to be modified - an array with components width and height, in
  136. * pixels.
  137. * @param $data
  138. * An array of attributes to use when performing the scale effect with the
  139. * following items:
  140. * - "width": An integer representing the desired width in pixels.
  141. * - "height": An integer representing the desired height in pixels.
  142. * - "upscale": A boolean indicating that the image should be upscaled if the
  143. * dimensions are larger than the original image.
  144. */
  145. function image_scale_dimensions(array &$dimensions, array $data) {
  146. if ($dimensions['width'] && $dimensions['height']) {
  147. image_dimensions_scale($dimensions, $data['width'], $data['height'], $data['upscale']);
  148. }
  149. }
  150. /**
  151. * Image effect callback; Crop an image resource.
  152. *
  153. * @param $image
  154. * An image object returned by image_load().
  155. * @param $data
  156. * An array of attributes to use when performing the crop effect with the
  157. * following items:
  158. * - "width": An integer representing the desired width in pixels.
  159. * - "height": An integer representing the desired height in pixels.
  160. * - "anchor": A string describing where the crop should originate in the form
  161. * of "XOFFSET-YOFFSET". XOFFSET is either a number of pixels or
  162. * "left", "center", "right" and YOFFSET is either a number of pixels or
  163. * "top", "center", "bottom".
  164. * @return
  165. * TRUE on success. FALSE on failure to crop image.
  166. * @see image_crop()
  167. */
  168. function image_crop_effect(&$image, $data) {
  169. // Set sane default values.
  170. $data += array(
  171. 'anchor' => 'center-center',
  172. );
  173. list($x, $y) = explode('-', $data['anchor']);
  174. $x = image_filter_keyword($x, $image->info['width'], $data['width']);
  175. $y = image_filter_keyword($y, $image->info['height'], $data['height']);
  176. if (!image_crop($image, $x, $y, $data['width'], $data['height'])) {
  177. watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
  178. return FALSE;
  179. }
  180. return TRUE;
  181. }
  182. /**
  183. * Image effect callback; Scale and crop an image resource.
  184. *
  185. * @param $image
  186. * An image object returned by image_load().
  187. * @param $data
  188. * An array of attributes to use when performing the scale and crop effect
  189. * with the following items:
  190. * - "width": An integer representing the desired width in pixels.
  191. * - "height": An integer representing the desired height in pixels.
  192. * @return
  193. * TRUE on success. FALSE on failure to scale and crop image.
  194. * @see image_scale_and_crop()
  195. */
  196. function image_scale_and_crop_effect(&$image, $data) {
  197. if (!image_scale_and_crop($image, $data['width'], $data['height'])) {
  198. watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
  199. return FALSE;
  200. }
  201. return TRUE;
  202. }
  203. /**
  204. * Image effect callback; Desaturate (grayscale) an image resource.
  205. *
  206. * @param $image
  207. * An image object returned by image_load().
  208. * @param $data
  209. * An array of attributes to use when performing the desaturate effect.
  210. * @return
  211. * TRUE on success. FALSE on failure to desaturate image.
  212. * @see image_desaturate()
  213. */
  214. function image_desaturate_effect(&$image, $data) {
  215. if (!image_desaturate($image)) {
  216. watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }
  221. /**
  222. * Image effect callback; Rotate an image resource.
  223. *
  224. * @param $image
  225. * An image object returned by image_load().
  226. * @param $data
  227. * An array of attributes to use when performing the rotate effect containing
  228. * the following items:
  229. * - "degrees": The number of (clockwise) degrees to rotate the image.
  230. * - "random": A boolean indicating that a random rotation angle should be
  231. * used for this image. The angle specified in "degrees" is used as a
  232. * positive and negative maximum.
  233. * - "bgcolor": The background color to use for exposed areas of the image.
  234. * Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave
  235. * blank for transparency on image types that support it.
  236. * @return
  237. * TRUE on success. FALSE on failure to rotate image.
  238. * @see image_rotate().
  239. */
  240. function image_rotate_effect(&$image, $data) {
  241. // Set sane default values.
  242. $data += array(
  243. 'degrees' => 0,
  244. 'bgcolor' => NULL,
  245. 'random' => FALSE,
  246. );
  247. // Convert short #FFF syntax to full #FFFFFF syntax.
  248. if (strlen($data['bgcolor']) == 4) {
  249. $c = $data['bgcolor'];
  250. $data['bgcolor'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3];
  251. }
  252. // Convert #FFFFFF syntax to hexadecimal colors.
  253. if ($data['bgcolor'] != '') {
  254. $data['bgcolor'] = hexdec(str_replace('#', '0x', $data['bgcolor']));
  255. }
  256. else {
  257. $data['bgcolor'] = NULL;
  258. }
  259. if (!empty($data['random'])) {
  260. $degrees = abs((float) $data['degrees']);
  261. $data['degrees'] = rand(-1 * $degrees, $degrees);
  262. }
  263. if (!image_rotate($image, $data['degrees'], $data['bgcolor'])) {
  264. watchdog('image', 'Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
  265. return FALSE;
  266. }
  267. return TRUE;
  268. }
  269. /**
  270. * Image dimensions callback; Rotate.
  271. *
  272. * @param $dimensions
  273. * Dimensions to be modified - an array with components width and height, in
  274. * pixels.
  275. * @param $data
  276. * An array of attributes to use when performing the rotate effect containing
  277. * the following items:
  278. * - "degrees": The number of (clockwise) degrees to rotate the image.
  279. * - "random": A boolean indicating that a random rotation angle should be
  280. * used for this image. The angle specified in "degrees" is used as a
  281. * positive and negative maximum.
  282. */
  283. function image_rotate_dimensions(array &$dimensions, array $data) {
  284. // If the rotate is not random and the angle is a multiple of 90 degrees,
  285. // then the new dimensions can be determined.
  286. if (!$data['random'] && ((int) ($data['degrees']) == $data['degrees']) && ($data['degrees'] % 90 == 0)) {
  287. if ($data['degrees'] % 180 != 0) {
  288. $temp = $dimensions['width'];
  289. $dimensions['width'] = $dimensions['height'];
  290. $dimensions['height'] = $temp;
  291. }
  292. }
  293. else {
  294. $dimensions['width'] = $dimensions['height'] = NULL;
  295. }
  296. }