image_overlay.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @file extension to imageapi, provide an overlay action for blending two
  4. * layers, preserving transparency.
  5. */
  6. /**
  7. * Places one image over another.
  8. *
  9. * @param stdClass $image
  10. * A valid image object.
  11. * @param stdClass $layer
  12. * A valid image object to be placed over or under the $image image.
  13. * @param int $x
  14. * Position of the overlay.
  15. * @param int $y
  16. * Position of the overlay.
  17. * @param int $alpha
  18. * Transparency of the overlay from 0-100. 0 is totally transparent. 100
  19. * (default) is totally opaque.
  20. * @param boolean $reverse
  21. * Flag to indicate the 'overlay' actually goes under the image.
  22. *
  23. * @return boolean
  24. * true on success, false otherwise.
  25. */
  26. function image_overlay(stdClass $image, stdClass $layer, $x, $y, $alpha = 100, $reverse = FALSE) {
  27. if ($reverse) {
  28. $x = imagecache_actions_keyword_filter($x, $layer->info['width'], $image->info['width']);
  29. $y = imagecache_actions_keyword_filter($y, $layer->info['height'], $image->info['height']);
  30. }
  31. else {
  32. $x = imagecache_actions_keyword_filter($x, $image->info['width'], $layer->info['width']);
  33. $y = imagecache_actions_keyword_filter($y, $image->info['height'], $layer->info['height']);
  34. }
  35. return image_toolkit_invoke('overlay', $image, array($layer, $x, $y, $alpha, $reverse));
  36. }
  37. /**
  38. * GD toolkit specific implementation of the image overlay effect.
  39. *
  40. * NOTE that the PHP libraries are not great at merging images SO we include a
  41. * library that does it pixel-by-pixel which is INCREDIBLY inefficient. If this
  42. * can be improved, in a way that supports all transparency, please let us know!
  43. *
  44. * A watermark is layer onto image, return the image. An underlay is image onto
  45. * layer, return the layer. Almost identical, but seeing as we work with
  46. * resource handles, the handle needs to be swapped before returning.
  47. *
  48. * @param stdClass $image
  49. * An image object.
  50. * @param stdClass $layer
  51. * Image object to be placed over or under the $image image.
  52. * @param int $x
  53. * Position of the overlay.
  54. * @param int $y
  55. * Position of the overlay.
  56. * @param int $alpha
  57. * Transparency of the overlay from 0-100. 0 is totally transparent. 100
  58. * (default) is totally opaque.
  59. * @param boolean $reverse
  60. * Flag to indicate that the 'overlay' actually goes under the image.
  61. *
  62. * @return boolean
  63. * true on success, false otherwise.
  64. */
  65. function image_gd_overlay(stdClass $image, stdClass $layer, $x, $y, $alpha = 100, $reverse = FALSE) {
  66. // If the given alpha is 100%, we can use imagecopy - which actually works,
  67. // is more efficient, and seems to retain the overlays partial transparency.
  68. // Still does not work great for indexed gifs though?
  69. if ($reverse) {
  70. $upper = &$image;
  71. $lower = &$layer;
  72. }
  73. else {
  74. $upper = &$layer;
  75. $lower = &$image;
  76. }
  77. if ($alpha == 100 && ($upper->info['mime_type'] != 'image/gif')) {
  78. imagealphablending($lower->resource, TRUE);
  79. imagesavealpha($lower->resource, TRUE);
  80. imagealphablending($upper->resource, TRUE);
  81. imagesavealpha($upper->resource, TRUE);
  82. imagecopy($lower->resource, $upper->resource, $x, $y, 0, 0, $upper->info['width'], $upper->info['height']);
  83. imagedestroy($upper->resource);
  84. $image->resource = $lower->resource;
  85. $image->info = $lower->info;
  86. }
  87. else {
  88. // imagecopy() cannot be used and we have to use the slow library.
  89. module_load_include('inc', 'imagecache_actions', 'watermark');
  90. $watermark = new watermark();
  91. $result_img = $watermark->create_watermark($lower->resource, $upper->resource, $x, $y, $alpha);
  92. // Watermark creates a new image resource, so clean up both old images.
  93. imagedestroy($lower->resource);
  94. imagedestroy($upper->resource);
  95. $image->resource = $result_img;
  96. $image->info = $lower->info;
  97. }
  98. return TRUE;
  99. }
  100. /**
  101. * Imagemagick toolkit specific implementation of the image overlay effect.
  102. *
  103. * An underlay should can be created like:
  104. * -background None -extent 300x250-50-50 under.jpg -compose dst-over -composite
  105. *
  106. * Explanation:
  107. * - first enlarge the canvas, making any new part fully transparent.
  108. * - placing the "original" image on its requested position
  109. * - define the "source" image to IM
  110. * - compose the images placing the original over the source
  111. *
  112. * An overlay can be created like:
  113. * overlay.png -geometry 50x50+100+75 -compose src-over -composite
  114. *
  115. * Explanation:
  116. * - define the overlay image to IM
  117. * - and define its size and position on the current image
  118. * - compose the images placing the original over the source
  119. *
  120. * Please be aware of the limitations of imagemagick libraries out there - the
  121. * versions distributed on hosted servers (if any) are often several years
  122. * behind. Using the latest imagemagick release features will make this function
  123. * unusable in real deployments.
  124. *
  125. * @param stdClass $image
  126. * An image object.
  127. * @param stdClass $layer
  128. * Image object to be placed over or under the $image image.
  129. * @param int $x
  130. * Position of the overlay.
  131. * @param int $y
  132. * Position of the overlay.
  133. * @param int $alpha
  134. * Transparency of the overlay from 0-100. 0 is totally transparent. 100
  135. * (default) is totally opaque.
  136. * @param boolean $reverse
  137. * Flag to indicate that the 'overlay' actually goes under the image.
  138. *
  139. * @return boolean
  140. * true on success, false otherwise.
  141. */
  142. function image_imagemagick_overlay(stdClass $image, stdClass $layer, $x = 0, $y = 0, $alpha = 100, $reverse = FALSE) {
  143. $realPath = imagecache_actions_find_file($layer->source);
  144. if (!$realPath) {
  145. return FALSE;
  146. }
  147. // Reset any gravity settings from earlier effects.
  148. $image->ops[] = '-gravity None';
  149. // In imagemagick terms:
  150. // - $image is the destination (the image being constructed)
  151. // - $layer is the source (the source of the current operation)
  152. // Add the layer image to the imagemagick command line.
  153. // Set the dimensions of the overlay. Use of the scale option means that we
  154. // need to change the dimensions: always set them, they don't harm when the
  155. // scale option is not used.
  156. $sign = $reverse ? -1 : 1;
  157. $geometry = sprintf('%ux%u%+d%+d', $layer->info['width'], $layer->info['height'], $sign * $x, $sign * $y);
  158. $compose_operator = $reverse ? 'dst-over' : 'src-over';
  159. // And compose it with the destination.
  160. if ($alpha == 100) {
  161. // Lay one image over the other. The transparency channel of the upper
  162. // image and/or its dimensions (being smaller than the lower image) will
  163. // determine what remains visible of the lower image).
  164. //
  165. // Note: In explicitly setting a -compose operator we reset/overwrite any
  166. // previously set one (former versions could produce erroneous results
  167. // in combination with other effects before this one).
  168. if ($reverse) {
  169. // Underlay.
  170. $image->ops[] = '-background';
  171. $image->ops[] = escapeshellarg('rgba(0,0,0,0)');
  172. $image->ops[] = "-extent $geometry";
  173. $image->ops[] = escapeshellarg($realPath);
  174. }
  175. else {
  176. $image->ops[] = escapeshellarg($realPath);
  177. $image->ops[] = "-geometry $geometry";
  178. }
  179. $image->ops[] = "-compose $compose_operator -composite";
  180. }
  181. else {
  182. // Alpha is not 100, so this image effect turns into a blend operation.
  183. // The alpha determines what percentage of the upper image pixel will be
  184. // taken. From the lower image pixel, 100 - alpha percent will be taken.
  185. //
  186. // Note 1: I'm not sure if and how transparency of one or both images is
  187. // used in or after the blend operation.
  188. // Note 2: As of IM v6.5.3-4 (around june 2009) we can use:
  189. // -compose blend -define compose:args=30[,70]
  190. $image->ops[] = escapeshellarg($realPath);
  191. $image->ops[] = "-geometry $geometry";
  192. $image->ops[] = "-compose blend -define compose:args=$alpha -composite";
  193. }
  194. return TRUE;
  195. }