watermark.inc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // $ID $
  3. /**
  4. * @file routine for image layering
  5. *
  6. */
  7. /****************************************************************************************************************************************/
  8. /**
  9. * Note that the below code is laboriously slow - it takes and compares every pixel of the two inputs
  10. * and calculates a new valus for each of them. this is only a fallback because reliable image toolkit
  11. * transparencies was buggy on certain platforms :(
  12. */
  13. //Niko (http://www.codeguru.com.ua)
  14. class watermark {
  15. function create_watermark($main_img_obj, $watermark_img_obj, $x_ins, $y_ins, $alpha_level = 100) {
  16. $alpha_level /= 100;
  17. # Should this change to match both images?
  18. #$main_img_obj_w = max(imagesx($main_img_obj), imagesx($watermark_img_obj));
  19. #$main_img_obj_h = max(imagesy($main_img_obj), imagesy($watermark_img_obj));
  20. $main_img_obj_w = imagesx($main_img_obj);
  21. $main_img_obj_h = imagesy($main_img_obj);
  22. $watermark_img_obj_w = imagesx($watermark_img_obj);
  23. $watermark_img_obj_h = imagesy($watermark_img_obj);
  24. $main_img_obj_min_x = $x_ins;
  25. $main_img_obj_min_y = $y_ins;
  26. $return_img = imagecreatetruecolor($main_img_obj_w, $main_img_obj_h);
  27. imagesavealpha($return_img, true);
  28. imagealphablending($return_img, false);
  29. for ($y = 0; $y < $main_img_obj_h; $y++) {
  30. for ($x = 0; $x < $main_img_obj_w; $x++) {
  31. $return_color = NULL;
  32. $watermark_x = $x - $main_img_obj_min_x;
  33. $watermark_y = $y - $main_img_obj_min_y;
  34. $main_rgb = imagecolorsforindex($main_img_obj, imagecolorat($main_img_obj, $x, $y));
  35. if ($watermark_x >= 0 && $watermark_x < $watermark_img_obj_w && $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h) {
  36. $watermark_rbg = imagecolorsforindex($watermark_img_obj, imagecolorat($watermark_img_obj, $watermark_x, $watermark_y));
  37. $watermark_alpha = round(((127 - $watermark_rbg['alpha']) / 127), 2);
  38. $watermark_alpha = $watermark_alpha * $alpha_level;
  39. $avg_red = $this->_get_ave_color($main_rgb['red'], $watermark_rbg['red'], $watermark_alpha);
  40. $avg_green = $this->_get_ave_color($main_rgb['green'], $watermark_rbg['green'], $watermark_alpha);
  41. $avg_blue = $this->_get_ave_color($main_rgb['blue'], $watermark_rbg['blue'], $watermark_alpha);
  42. // TODO figure out the maths for merging two transparent images
  43. $new_alpha = min($watermark_rbg['alpha'], $main_rgb['alpha']);
  44. #$new_alpha = $main_rgb['alpha'];
  45. #$new_alpha = 0;
  46. $return_color = $this->_get_image_color($return_img, $avg_red, $avg_green, $avg_blue, $new_alpha);
  47. }
  48. else {
  49. $return_color = imagecolorat($main_img_obj, $x, $y);
  50. }
  51. imagesetpixel($return_img, $x, $y, $return_color);
  52. }
  53. }
  54. return $return_img;
  55. }
  56. function _get_ave_color($color_a, $color_b, $alpha_level) {
  57. return round((($color_a * (1 - $alpha_level)) + ($color_b * $alpha_level)));
  58. }
  59. function _get_image_color($im, $r, $g, $b, $alpha) {
  60. $c = imagecolorexactalpha($im, $r, $g, $b, $alpha);
  61. if ($c != -1)
  62. return $c;
  63. $c = imagecolorallocate($im, $r, $g, $b, $alpha);
  64. if ($c != -1)
  65. return $c;
  66. return imagecolorclosest($im, $r, $g, $b, $alpha);
  67. }
  68. }
  69. /****************************************************************************************************************************************/