imagecache_canvasactions.module 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @file A collection of canvas (layer) type manipulations for imagecache -
  4. * including "Watermark"
  5. *
  6. * Based on first draft of the code by Dimm (imagecache.module 5--1)
  7. * https://drupal.org/node/184816
  8. *
  9. * Rewritten and ported to Imagecache actions API (imagecache.module 5--2) by
  10. * dman http://coders.co.nz/
  11. *
  12. *
  13. * Notes about imagecache action extensions. For each action:
  14. *
  15. * 1: Implement imagecache_HOOK_form($formdata) to define the config form.
  16. *
  17. * 1a: Implement theme_imagecache_HOOK_form if needed - optional
  18. *
  19. * 2: Implement imagecache_HOOK_image($image, $data) to DO the process
  20. *
  21. * 3: Implement theme_imagecache_HOOK($element) to return a text description of
  22. * the setting
  23. *
  24. * 4: Declare the action in HOOK_imagecache_actions()
  25. *
  26. *
  27. * API ref for hook_image()
  28. *
  29. * @param $image array defining an image file, including :
  30. *
  31. * $image- >source as the filename,
  32. *
  33. * $image->info array
  34. *
  35. * $image->resource handle on the image object
  36. *
  37. * @param $action array of settings as defined in your form.
  38. *
  39. */
  40. if (! function_exists('imagecache_actions_calculate_relative_position') ) {
  41. module_load_include('inc', 'imagecache_actions', 'utility');
  42. }
  43. // @todo There doesn't seem to be a way to specify a file in hook_image_effect_info
  44. // so placing this here for the time being.
  45. module_load_include('inc', 'imagecache_canvasactions', 'canvasactions');
  46. module_load_include('inc', 'imagecache_canvasactions', 'rounded_corners');
  47. /**
  48. * Implements hook_image_effect_info().
  49. *
  50. * Defines information about the supported effects.
  51. */
  52. function imagecache_canvasactions_image_effect_info() {
  53. $effects = array();
  54. $effects['canvasactions_definecanvas'] = array(
  55. 'label' => t('Define canvas'),
  56. 'help' => t('Define the size of the working canvas and background color, this controls the dimensions of the output image.'),
  57. 'effect callback' => 'canvasactions_definecanvas_effect',
  58. 'dimensions callback' => 'canvasactions_definecanvas_dimensions',
  59. 'form callback' => 'canvasactions_definecanvas_form',
  60. 'summary theme' => 'canvasactions_definecanvas_summary',
  61. );
  62. $effects['canvasactions_imagemask'] = array(
  63. 'label' => t('Image mask'),
  64. 'help' => t(' Choose the file image you wish to use as a mask, and apply it to the canvas.'),
  65. 'dimensions passthrough' => TRUE,
  66. 'effect callback' => 'canvasactions_imagemask_effect',
  67. 'form callback' => 'canvasactions_imagemask_form',
  68. 'summary theme' => 'canvasactions_imagemask_summary',
  69. );
  70. $effects['canvasactions_file2canvas'] = array(
  71. 'label' => t('Overlay (watermark)'),
  72. 'help' => t('Choose the file image you wish to use as an overlay, and position it in a layer on top of the canvas.'),
  73. 'dimensions passthrough' => TRUE,
  74. 'effect callback' => 'canvasactions_file2canvas_effect',
  75. 'form callback' => 'canvasactions_file2canvas_form',
  76. 'summary theme' => 'canvasactions_file2canvas_summary',
  77. );
  78. $effects['canvasactions_canvas2file'] = array(
  79. 'label' => t('Underlay (background)'),
  80. 'help' => t('Choose the file image you wish to use as an background, and position the processed image on it.'),
  81. 'effect callback' => 'canvasactions_canvas2file_effect',
  82. 'dimensions callback' => 'canvasactions_canvas2file_dimensions',
  83. 'form callback' => 'canvasactions_canvas2file_form',
  84. 'summary theme' => 'canvasactions_canvas2file_summary',
  85. );
  86. $effects['canvasactions_source2canvas'] = array(
  87. 'label' => t('Overlay: source image to canvas'),
  88. 'help' => t('Places the source image onto the canvas for compositing.'),
  89. 'dimensions passthrough' => TRUE,
  90. 'effect callback' => 'canvasactions_source2canvas_effect',
  91. 'form callback' => 'canvasactions_source2canvas_form',
  92. 'summary theme' => 'canvasactions_source2canvas_summary',
  93. );
  94. $effects['canvasactions_roundedcorners'] = array(
  95. 'label' => t('Rounded Corners'),
  96. 'help' => t('This is true cropping, not overlays, so the result <em>can</em> be transparent.'),
  97. 'dimensions passthrough' => TRUE,
  98. 'effect callback' => 'canvasactions_roundedcorners_effect',
  99. 'form callback' => 'canvasactions_roundedcorners_form',
  100. 'summary theme' => 'canvasactions_roundedcorners_summary',
  101. );
  102. $effects['canvasactions_aspect'] = array(
  103. 'label' => t('Aspect switcher'),
  104. 'help' => t('Use different effects depending on whether the image is landscape of portrait shaped. This re-uses other preset definitions, and just chooses between them based on the rule.'),
  105. 'effect callback' => 'canvasactions_aspect_effect',
  106. 'dimensions callback' => 'canvasactions_aspect_dimensions',
  107. 'form callback' => 'canvasactions_aspect_form',
  108. 'summary theme' => 'canvasactions_aspect_summary',
  109. );
  110. $effects['canvasactions_resizepercent'] = array(
  111. 'label' => t('Resize (percent)'),
  112. 'help' => t('Resize the image based on percent. If only a single dimension is specified, the other dimension will be calculated.'),
  113. 'effect callback' => 'canvasactions_resizepercent_effect',
  114. 'dimensions callback' => 'canvasactions_resizepercent_dimensions',
  115. 'form callback' => 'canvasactions_resizepercent_form',
  116. 'summary theme' => 'canvasactions_resizepercent_summary',
  117. );
  118. $effects['canvasactions_blur'] = array(
  119. 'label' => t('Blur'),
  120. 'help' => t('Make the image become unclear.'),
  121. 'effect callback' => 'canvasactions_blur_effect',
  122. 'dimensions passthrough' => TRUE,
  123. 'form callback' => 'canvasactions_blur_form',
  124. 'summary theme' => 'canvasactions_blur_summary',
  125. );
  126. $effects['canvasactions_interlace'] = array(
  127. 'label' => t('Interlace / Progressive'),
  128. 'help' => t('Create interlaced PNG/GIF or progressive JPG.'),
  129. 'effect callback' => 'canvasactions_interlace_effect',
  130. 'dimensions passthrough' => TRUE,
  131. 'form callback' => 'canvasactions_interlace_form',
  132. );
  133. $effects['canvasactions_perspective'] = array(
  134. 'label' => t('Perspective transform'),
  135. 'help' => t('Adds a perspective transformation to the image.'),
  136. 'effect callback' => 'canvasactions_perspective_effect',
  137. 'dimensions passthrough' => TRUE,
  138. 'form callback' => 'canvasactions_perspective_form',
  139. 'summary theme' => 'canvasactions_perspective_summary',
  140. );
  141. return $effects;
  142. }
  143. /**
  144. * Implements hook_theme().
  145. *
  146. * Registers theme functions for the effect summaries.
  147. */
  148. function imagecache_canvasactions_theme() {
  149. return array(
  150. 'canvasactions_definecanvas_summary' => array(
  151. 'variables' => array('data' => NULL),
  152. 'file' => 'canvasactions.inc',
  153. ),
  154. 'canvasactions_imagemask_summary' => array(
  155. 'arguments' => array('element' => NULL),
  156. 'file' => 'canvasactions.inc',
  157. ),
  158. 'canvasactions_file2canvas_summary' => array(
  159. 'variables' => array('data' => NULL),
  160. 'file' => 'canvasactions.inc',
  161. ),
  162. 'canvasactions_source2canvas_summary' => array(
  163. 'variables' => array('data' => NULL),
  164. 'file' => 'canvasactions.inc',
  165. ),
  166. 'canvasactions_canvas2file_summary' => array(
  167. 'variables' => array('data' => NULL),
  168. 'file' => 'canvasactions.inc',
  169. ),
  170. 'canvasactions_roundedcorners_summary' => array(
  171. 'variables' => array('data' => NULL),
  172. 'file' => 'canvasactions.inc',
  173. ),
  174. 'canvasactions_aspect_summary' => array(
  175. 'variables' => array('data' => NULL),
  176. 'file' => 'canvasactions.inc',
  177. ),
  178. 'canvasactions_resizepercent_summary' => array(
  179. 'variables' => array('data' => NULL),
  180. 'file' => 'canvasactions.inc',
  181. ),
  182. 'canvasactions_blur_summary' => array(
  183. 'variables' => array('data' => NULL),
  184. 'file' => 'canvasactions.inc',
  185. ),
  186. 'canvasactions_perspective_summary' => array(
  187. 'variables' => array('data' => NULL),
  188. 'file' => 'canvasactions.inc',
  189. ),
  190. 'canvasactions_perspective_anchor' => array(
  191. 'render element' => 'element',
  192. ),
  193. );
  194. }
  195. /**
  196. * Implements hook_image_style_flush().
  197. *
  198. * This hook checks if the image style that is being flushed is used in an
  199. * aspect switcher effect. If so, the style that contains the aspect switcher
  200. * effect, should be flushed as well as the flushed style was probably changed.
  201. *
  202. * @param array $flushed_style
  203. * The image style that is being flushed.
  204. */
  205. function imagecache_canvasactions_image_style_flush(/*array*/ $flushed_style) {
  206. $styles = image_styles();
  207. foreach ($styles as $style) {
  208. if ($style['name'] !== $flushed_style['name']) {
  209. foreach ($style['effects'] as $effect) {
  210. if ($effect['name'] === 'canvasactions_aspect') {
  211. if ( (isset($effect['data']['portrait']) && $effect['data']['portrait'] === $flushed_style['name'])
  212. || (isset($effect['data']['landscape']) && $effect['data']['landscape'] === $flushed_style['name'])) {
  213. image_style_flush($style);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }