imagecache_canvasactions.module 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * http://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. // During devel, caching is pointless. Flush it
  41. // imagecache_action_definitions(TRUE);
  42. if (! function_exists('imagecache_actions_calculate_relative_position') ) {
  43. module_load_include('inc', 'imagecache_canvasactions', 'utility');
  44. }
  45. // @todo There doesn't seem to be a way to specify a file in hook_image_effect_info
  46. // so placing this here for the time being.
  47. module_load_include('inc', 'imagecache_canvasactions', 'canvasactions');
  48. module_load_include('inc', 'imagecache_canvasactions', 'rounded_corners');
  49. // imageapi extensions
  50. module_load_include('inc', 'imagcache_actions', 'image_overlay.inc');
  51. function imagecache_canvasactions_image_effect_info() {
  52. $effects = array();
  53. $effects['canvasactions_definecanvas'] = array(
  54. 'label' => t('Define canvas'),
  55. 'help' => t('Define the size of the working canvas and background color, this controls the dimensions of the output image.'),
  56. 'effect callback' => 'canvasactions_definecanvas_effect',
  57. 'dimensions callback' => 'canvasactions_definecanvas_dimensions',
  58. 'form callback' => 'canvasactions_definecanvas_form',
  59. 'summary theme' => 'canvasactions_definecanvas_summary',
  60. );
  61. $effects['canvasactions_imagemask'] = array(
  62. 'label' => t('Image mask'),
  63. 'help' => t(' Choose the file image you wish to use as a mask, and apply it to the canvas.'),
  64. 'effect callback' => 'canvasactions_imagemask_image',
  65. 'dimensions passthrough' => TRUE,
  66. 'form callback' => 'canvasactions_imagemask_form',
  67. 'summary theme' => 'canvasactions_imagemask_summary',
  68. );
  69. $effects['canvasactions_file2canvas'] = array(
  70. 'label' => t('Overlay (watermark)'),
  71. 'help' => t('Choose the file image you wish to use as an overlay, and position it in a layer on top of the canvas.'),
  72. 'effect callback' => 'canvasactions_file2canvas_image',
  73. 'dimensions passthrough' => TRUE,
  74. 'form callback' => 'canvasactions_file2canvas_form',
  75. 'summary theme' => 'canvasactions_file2canvas_summary',
  76. );
  77. $effects['canvasactions_canvas2file'] = array(
  78. 'label' => t('Underlay (background)'),
  79. 'help' => t('Choose the file image you wish to use as an background, and position the processed image on it.'),
  80. 'effect callback' => 'canvasactions_canvas2file_image',
  81. 'dimensions callback' => 'canvasactions_canvas2file_dimensions',
  82. 'form callback' => 'canvasactions_canvas2file_form',
  83. 'summary theme' => 'canvasactions_canvas2file_summary',
  84. );
  85. $effects['canvasactions_source2canvas'] = array(
  86. 'label' => t('Overlay: source image to canvas'),
  87. 'help' => t('Places the source image onto the canvas for compositing.'),
  88. 'effect callback' => 'canvasactions_source2canvas_image',
  89. 'dimensions passthrough' => TRUE,
  90. 'form callback' => 'canvasactions_source2canvas_form',
  91. 'summary theme' => 'canvasactions_source2canvas_summary',
  92. );
  93. $effects['canvasactions_roundedcorners'] = array(
  94. 'label' => t('Rounded Corners'),
  95. 'help' => t('This is true cropping, not overlays, so the result <em>can</em> be transparent.'),
  96. 'effect callback' => 'canvasactions_roundedcorners_image',
  97. 'dimensions passthrough' => TRUE,
  98. 'form callback' => 'canvasactions_roundedcorners_form',
  99. 'summary theme' => 'canvasactions_roundedcorners_summary',
  100. );
  101. $effects['canvasactions_aspect'] = array(
  102. 'label' => t('Aspect switcher'),
  103. '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.'),
  104. 'effect callback' => 'canvasactions_aspect_image',
  105. 'dimensions callback' => 'canvasactions_aspect_dimensions',
  106. 'form callback' => 'canvasactions_aspect_form',
  107. 'summary theme' => 'canvasactions_aspect_summary',
  108. );
  109. return $effects;
  110. }
  111. /**
  112. * Need to register the theme functions we expect to use
  113. */
  114. function imagecache_canvasactions_theme() {
  115. $util_dir = drupal_get_path('module', 'imagecache_actions');
  116. return array(
  117. 'canvasactions_definecanvas_summary' => array(
  118. 'file' => 'canvasactions.inc',
  119. 'variables' => array('data' => NULL),
  120. ),
  121. 'canvasactions_imagemask_summary' => array(
  122. 'file' => 'canvasactions.inc',
  123. 'arguments' => array('element' => NULL),
  124. ),
  125. 'canvasactions_file2canvas_summary' => array(
  126. 'file' => 'canvasactions.inc',
  127. 'variables' => array('data' => NULL),
  128. ),
  129. 'canvasactions_source2canvas_summary' => array(
  130. 'file' => 'canvasactions.inc',
  131. 'variables' => array('data' => NULL),
  132. ),
  133. 'canvasactions_canvas2file_summary' => array(
  134. 'file' => 'canvasactions.inc',
  135. 'variables' => array('data' => NULL),
  136. ),
  137. 'canvasactions_roundedcorners_summary' => array(
  138. 'file' => 'rounded_corners.inc',
  139. 'variables' => array('data' => NULL),
  140. ),
  141. 'canvasactions_aspect_summary' => array(
  142. 'file' => 'canvasactions.inc',
  143. 'variables' => array('data' => NULL),
  144. ),
  145. );
  146. }
  147. /**
  148. * Implements hook_image_style_flush.
  149. *
  150. * This hook checks if the image style that is being flushed is used in an
  151. * aspect switcher effect. If so, the style that contains the aspect switcher
  152. * effect, should be flushed as well as the flushed style was probably changed.
  153. *
  154. * @param array $flushed_style
  155. * The image style that is being flushed.
  156. */
  157. function imagecache_canvasactions_image_style_flush($flushed_style) {
  158. $styles = image_styles();
  159. foreach ($styles as $style) {
  160. if ($style['name'] !== $flushed_style['name']) {
  161. foreach ($style['effects'] as $effect) {
  162. if ($effect['name'] === 'canvasactions_aspect') {
  163. if ( (isset($effect['data']['portrait']) && $effect['data']['portrait'] === $flushed_style['name'])
  164. || (isset($effect['data']['landscape']) && $effect['data']['landscape'] === $flushed_style['name'])) {
  165. image_style_flush($style);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }