phpthumb.unsharp.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. //////////////////////////////////////////////////////////////
  3. ////
  4. //// Unsharp Mask for PHP - version 2.1.1
  5. ////
  6. //// Unsharp mask algorithm by Torstein Hønsi 2003-07.
  7. //// thoensi_at_netcom_dot_no.
  8. //// Please leave this notice.
  9. ////
  10. //////////////////////////////////////////////////////////////
  11. // From: http://vikjavev.no/computing/ump.php //
  12. // //
  13. // Reformatted by James Heinrich <info@silisoftware.com> //
  14. // for use in phpThumb() on 3 February 2003. //
  15. // updated to v2.1.1 on 24 April 2011 //
  16. // //
  17. // phpThumb() is found at http://phpthumb.sourceforge.net //
  18. // and/or https://github.com/JamesHeinrich/phpThumb //
  19. //////////////////////////////////////////////////////////////
  20. /*
  21. New:
  22. - In version 2.1 (February 26 2007) Tom Bishop has done some important speed enhancements.
  23. - From version 2 (July 17 2006) the script uses the imageconvolution function in PHP
  24. version >= 5.1, which improves the performance considerably.
  25. Unsharp masking is a traditional darkroom technique that has proven very suitable for
  26. digital imaging. The principle of unsharp masking is to create a blurred copy of the image
  27. and compare it to the underlying original. The difference in colour values
  28. between the two images is greatest for the pixels near sharp edges. When this
  29. difference is subtracted from the original image, the edges will be
  30. accentuated.
  31. The Amount parameter simply says how much of the effect you want. 100 is 'normal'.
  32. Radius is the radius of the blurring circle of the mask. 'Threshold' is the least
  33. difference in colour values that is allowed between the original and the mask. In practice
  34. this means that low-contrast areas of the picture are left unrendered whereas edges
  35. are treated normally. This is good for pictures of e.g. skin or blue skies.
  36. Any suggenstions for improvement of the algorithm, especially regarding the speed
  37. and the roundoff errors in the Gaussian blur process, are welcome.
  38. */
  39. class phpUnsharpMask {
  40. public static function applyUnsharpMask(&$img, $amount, $radius, $threshold) {
  41. // $img is an image that is already created within php using
  42. // imgcreatetruecolor. No url! $img must be a truecolor image.
  43. // Attempt to calibrate the parameters to Photoshop:
  44. $amount = min($amount, 500) * 0.016;
  45. $radius = abs(round(min(50, $radius) * 2)); // Only integers make sense.
  46. $threshold = min(255, $threshold);
  47. if ($radius == 0) {
  48. return true;
  49. }
  50. $w = imagesx($img);
  51. $h = imagesy($img);
  52. $imgCanvas = imagecreatetruecolor($w, $h);
  53. $imgBlur = imagecreatetruecolor($w, $h);
  54. // Gaussian blur matrix:
  55. //
  56. // 1 2 1
  57. // 2 4 2
  58. // 1 2 1
  59. //
  60. //////////////////////////////////////////////////
  61. if (function_exists('imageconvolution')) { // PHP >= 5.1
  62. $matrix = array(
  63. array(1, 2, 1),
  64. array(2, 4, 2),
  65. array(1, 2, 1)
  66. );
  67. imagecopy($imgBlur, $img, 0, 0, 0, 0, $w, $h);
  68. imageconvolution($imgBlur, $matrix, 16, 0);
  69. } else {
  70. // Move copies of the image around one pixel at the time and merge them with weight
  71. // according to the matrix. The same matrix is simply repeated for higher radii.
  72. for ($i = 0; $i < $radius; $i++) {
  73. imagecopy( $imgBlur, $img, 0, 0, 1, 0, $w - 1, $h); // left
  74. imagecopymerge($imgBlur, $img, 1, 0, 0, 0, $w , $h, 50); // right
  75. imagecopymerge($imgBlur, $img, 0, 0, 0, 0, $w , $h, 50); // center
  76. imagecopy( $imgCanvas, $imgBlur, 0, 0, 0, 0, $w , $h);
  77. imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w , $h - 1, 33.33333); // up
  78. imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w , $h, 25); // down
  79. }
  80. }
  81. if ($threshold > 0){
  82. // Calculate the difference between the blurred pixels and the original
  83. // and set the pixels
  84. for ($x = 0; $x < $w-1; $x++) { // each row
  85. for ($y = 0; $y < $h; $y++) { // each pixel
  86. $rgbOrig = imagecolorat($img, $x, $y);
  87. $rOrig = (($rgbOrig >> 16) & 0xFF);
  88. $gOrig = (($rgbOrig >> 8) & 0xFF);
  89. $bOrig = ($rgbOrig & 0xFF);
  90. $rgbBlur = imagecolorat($imgBlur, $x, $y);
  91. $rBlur = (($rgbBlur >> 16) & 0xFF);
  92. $gBlur = (($rgbBlur >> 8) & 0xFF);
  93. $bBlur = ($rgbBlur & 0xFF);
  94. // When the masked pixels differ less from the original
  95. // than the threshold specifies, they are set to their original value.
  96. $rNew = ((abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig);
  97. $gNew = ((abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig);
  98. $bNew = ((abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig);
  99. if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
  100. $pixCol = imagecolorallocate($img, $rNew, $gNew, $bNew);
  101. imagesetpixel($img, $x, $y, $pixCol);
  102. }
  103. }
  104. }
  105. } else {
  106. for ($x = 0; $x < $w; $x++) { // each row
  107. for ($y = 0; $y < $h; $y++) { // each pixel
  108. $rgbOrig = imagecolorat($img, $x, $y);
  109. $rOrig = (($rgbOrig >> 16) & 0xFF);
  110. $gOrig = (($rgbOrig >> 8) & 0xFF);
  111. $bOrig = ($rgbOrig & 0xFF);
  112. $rgbBlur = imagecolorat($imgBlur, $x, $y);
  113. $rBlur = (($rgbBlur >> 16) & 0xFF);
  114. $gBlur = (($rgbBlur >> 8) & 0xFF);
  115. $bBlur = ($rgbBlur & 0xFF);
  116. $rNew = min(255, max(0, ($amount * ($rOrig - $rBlur)) + $rOrig));
  117. $gNew = min(255, max(0, ($amount * ($gOrig - $gBlur)) + $gOrig));
  118. $bNew = min(255, max(0, ($amount * ($bOrig - $bBlur)) + $bOrig));
  119. $rgbNew = ($rNew << 16) + ($gNew <<8) + $bNew;
  120. imagesetpixel($img, $x, $y, $rgbNew);
  121. }
  122. }
  123. }
  124. imagedestroy($imgCanvas);
  125. imagedestroy($imgBlur);
  126. return true;
  127. }
  128. }