watermark.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @file Routine for image layering.
  4. *
  5. * Note that the below code is laboriously slow - it takes and compares every
  6. * pixel of the two inputs and calculates a new values for each of them. this is
  7. * only a fallback because reliable image toolkit transparencies was buggy on
  8. * certain platforms :(
  9. *
  10. * Code from: Niko (http://www.codeguru.com.ua).
  11. */
  12. class watermark {
  13. /**
  14. * TODO
  15. *
  16. * @param resource $main_img_obj
  17. * @param resource $watermark_img_obj
  18. * @param int $x_ins
  19. * @param int $y_ins
  20. * @param int $alpha_level
  21. *
  22. * @return resource
  23. *
  24. */
  25. function create_watermark($main_img_obj, $watermark_img_obj, $x_ins, $y_ins, $alpha_level = 100) {
  26. $alpha_level /= 100;
  27. // Should this change to match both images?
  28. //$main_img_obj_w = max(imagesx($main_img_obj), imagesx($watermark_img_obj));
  29. //$main_img_obj_h = max(imagesy($main_img_obj), imagesy($watermark_img_obj));
  30. $main_img_obj_w = imagesx($main_img_obj);
  31. $main_img_obj_h = imagesy($main_img_obj);
  32. $watermark_img_obj_w = imagesx($watermark_img_obj);
  33. $watermark_img_obj_h = imagesy($watermark_img_obj);
  34. $main_img_obj_min_x = $x_ins;
  35. $main_img_obj_min_y = $y_ins;
  36. $return_img = imagecreatetruecolor($main_img_obj_w, $main_img_obj_h);
  37. imagesavealpha($return_img, TRUE);
  38. imagealphablending($return_img, FALSE);
  39. for ($y = 0; $y < $main_img_obj_h; $y++) {
  40. for ($x = 0; $x < $main_img_obj_w; $x++) {
  41. $return_color = NULL;
  42. $watermark_x = $x - $main_img_obj_min_x;
  43. $watermark_y = $y - $main_img_obj_min_y;
  44. $main_rgb = imagecolorsforindex($main_img_obj, imagecolorat($main_img_obj, $x, $y));
  45. if ($watermark_x >= 0 && $watermark_x < $watermark_img_obj_w && $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h) {
  46. $watermark_rbg = imagecolorsforindex($watermark_img_obj, imagecolorat($watermark_img_obj, $watermark_x, $watermark_y));
  47. $watermark_alpha = round(((127 - $watermark_rbg['alpha']) / 127), 2);
  48. $watermark_alpha = $watermark_alpha * $alpha_level;
  49. $avg_red = $this->_get_ave_color($main_rgb['red'], $watermark_rbg['red'], $watermark_alpha);
  50. $avg_green = $this->_get_ave_color($main_rgb['green'], $watermark_rbg['green'], $watermark_alpha);
  51. $avg_blue = $this->_get_ave_color($main_rgb['blue'], $watermark_rbg['blue'], $watermark_alpha);
  52. // TODO figure out the maths for merging two transparent images.
  53. $new_alpha = min($watermark_rbg['alpha'], $main_rgb['alpha']);
  54. //$new_alpha = $main_rgb['alpha'];
  55. //$new_alpha = 0;
  56. $return_color = $this->_get_image_color($return_img, $avg_red, $avg_green, $avg_blue, $new_alpha);
  57. }
  58. else {
  59. $return_color = imagecolorat($main_img_obj, $x, $y);
  60. }
  61. imagesetpixel($return_img, $x, $y, $return_color);
  62. }
  63. }
  64. return $return_img;
  65. }
  66. /**
  67. * Get an average color.
  68. *
  69. * @param int $color_a
  70. * @param int $color_b
  71. * @param float $alpha_level
  72. *
  73. * @return float
  74. *
  75. */
  76. function _get_ave_color($color_a, $color_b, $alpha_level) {
  77. return round((($color_a * (1 - $alpha_level)) + ($color_b * $alpha_level)));
  78. }
  79. /**
  80. * TODO
  81. *
  82. * @param resource $image
  83. * @param int $r
  84. * @param int $g
  85. * @param int $b
  86. * @param int $alpha
  87. *
  88. * @return int
  89. */
  90. function _get_image_color($image, $r, $g, $b, $alpha) {
  91. $c = imagecolorexactalpha($image, $r, $g, $b, $alpha);
  92. if ($c != -1) {
  93. return $c;
  94. }
  95. // @todo: this now uses imagecolorallocatealpha(): test
  96. $c = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
  97. if ($c != -1) {
  98. return $c;
  99. }
  100. // @todo: this now uses imagecolorclosestalpha(): test
  101. return imagecolorclosestalpha($image, $r, $g, $b, $alpha);
  102. }
  103. }
  104. /****************************************************************************************************************************************/