Basic PHP document management system, including automatic detection of corporate logos in letters
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?PHP
  2. function isClear(&$im, $x, $y) {
  3. $rgb = imagecolorat($im, $x, $y);
  4. $r = ($rgb >> 16) & 0xFF;
  5. $g = ($rgb >> 8) & 0xFF;
  6. $b = $rgb & 0xFF;
  7. return $r + $g + $b > 600;
  8. }
  9. function getLineScore(&$im, $y, $xmin, $xmax) {
  10. $count = 0;
  11. for ($x = $xmin; $x < $xmax; $x++) {
  12. if (isClear($im, $x, $y)) {
  13. $count++;
  14. }
  15. }
  16. return $count / ($xmax - $xmin);
  17. }
  18. function getColScore(&$im, $ymin, $ymax, $x) {
  19. $count = 0;
  20. for ($y = $ymin; $y < $ymax; $y++) {
  21. if (isClear($im, $x, $y)) {
  22. $count++;
  23. }
  24. }
  25. return $count / ($ymax - $ymin);
  26. }
  27. // Number of blank lines required in a row
  28. define('BLANK_THRESHOLD', 15);
  29. function doBlankCols(&$im, $ymin, $ymax, $xmin, $xmax, $colour = false) {
  30. // Check for blank columns
  31. if ($xmin == $xmax || $ymin == $ymax) { return array(); }
  32. $lastx = $laststreak = -100; $count = 0; $res = array();
  33. for ($x = $xmin; $x <= $xmax; $x++) {
  34. $score = getColScore($im, $ymin, $ymax, $x);
  35. if ($score > 0.99) {
  36. if (++$lastx == $x) {
  37. $count++;
  38. if ($count == BLANK_THRESHOLD) {
  39. if ($colour) { imagefilledrectangle($im, $x - $count, $ymin, $x, $ymax, imagecolorallocatealpha($im, 0, 0, 0, 50)); }
  40. $res = array_merge($res, doBlankLines($im, $ymin, $ymax, max($xmin, $laststreak + 1), $x - $count - 1, $colour));
  41. $laststreak = $x;
  42. } else if ($count > BLANK_THRESHOLD) {
  43. if ($colour) { imageline($im, $x, $ymin, $x, $ymax, imagecolorallocatealpha($im, 0, 0, 0, 50)); }
  44. $laststreak = $x;
  45. }
  46. } else {
  47. $lastx = $x;
  48. $count = 1;
  49. }
  50. }
  51. }
  52. if (count($res) > 0 && $laststreak + 1 < $xmax) {
  53. $res = array_merge($res, doBlankLines($im, $ymin, $ymax, max($xmin, $laststreak + 1), $xmax));
  54. }
  55. if (count($res) == 0) {
  56. $res[] = array($ymin, $ymax, $xmin, $xmax);
  57. }
  58. return $res;
  59. }
  60. function doBlankLines(&$im, $ymin, $ymax, $xmin, $xmax, $colour = false) {
  61. // Check for blank lines
  62. if ($xmin == $xmax || $ymin == $ymax) { return array(); }
  63. $lasty = $laststreak = -100; $count = 0; $res = array();
  64. for ($y = $ymin; $y <= $ymax; $y++) {
  65. $score = getLineScore($im, $y, $xmin, $xmax);
  66. if ($xmin > 0) {
  67. //imageline($im, $xmin, $y, $xmax, $y, imagecolorallocatealpha($im, 0, 0xFF * $score, 0, 50));
  68. }
  69. if ($score > 0.99) {
  70. if (++$lasty == $y) {
  71. $count++;
  72. if ($count == BLANK_THRESHOLD) {
  73. if ($colour) {
  74. imagefilledrectangle($im, $xmin, $y - $count, $xmax, $y, imagecolorallocatealpha($im, 0, 0xFF, 0, 50));
  75. }
  76. $res = array_merge($res, doBlankCols($im, max($ymin, 1 + $laststreak), $y - $count, $xmin, $xmax, $colour));
  77. $laststreak = $y;
  78. } else if ($count > BLANK_THRESHOLD) {
  79. if ($colour) { imageline($im, $xmin, $y, $xmax, $y, imagecolorallocatealpha($im, 0, 0xFF, 0, 50)); }
  80. $laststreak = $y;
  81. }
  82. } else {
  83. $count = 1;
  84. $lasty = $y;
  85. }
  86. }
  87. }
  88. if (count($res) > 0 && $laststreak + 1 < $ymax) {
  89. $res = array_merge($res, doBlankCols($im, max($ymin, 1 + $laststreak), $ymax, $xmin, $xmax));
  90. }
  91. if (count($res) == 0) {
  92. $res[] = array($ymin, $ymax, $xmin, $xmax);
  93. }
  94. return $res;
  95. }
  96. function logoFilter1($logo) {
  97. global $im;
  98. $height = $logo[1] - $logo[0];
  99. $width = $logo[3] - $logo[2];
  100. if ($width < 3 * BLANK_THRESHOLD || $height < 3 * BLANK_THRESHOLD) { return false; }
  101. if ($width > 0.4 * imagesx($im)) { return false; }
  102. return true;
  103. }
  104. function logoFilter2($logo) {
  105. global $im, $logos;
  106. $left = $logo[2] < 0.5 * imagesx($im);
  107. foreach ($logos as $other) {
  108. if (($left && $other[2] < $logo[2] - 20) || (!$left && $other[3] > $logo[3] + 20)) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. function logoFilter3($logo) {
  115. global $logos;
  116. foreach ($logos as $other) {
  117. if ($other[0] < $logo[0]) { return false; }
  118. }
  119. return true;
  120. }
  121. function getLogo($im) {
  122. $logos = doBlankLines($im, 0, imagesy($im) / 3, 0, imagesx($im), false);
  123. $GLOBALS['im'] =& $im;
  124. $GLOBALS['logos'] =& $logos;
  125. $logos = array_filter($logos, 'logoFilter1');
  126. $logos = array_filter($logos, 'logoFilter2');
  127. $logos = array_filter($logos, 'logoFilter3');
  128. $logo = array_pop($logos);
  129. $im2 = imagecreatetruecolor($logo[3] - $logo[2], $logo[1] - $logo[0]);
  130. imagecopy($im2, $im, 0, 0, $logo[2], $logo[0], $logo[3] - $logo[2], $logo[1] - $logo[0]);
  131. return $im2;
  132. }
  133. ?>