1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: class Horde_Image_Effect_Gd_Unsharpmask extends Horde_Image_Effect
11: {
12: 13: 14: 15: 16: 17: 18:
19: protected $_params = array('amount' => 0,
20: 'radius' => 0,
21: 'threshold' => 0);
22:
23: 24: 25: 26: 27:
28: public function apply()
29: {
30: $amount = $this->_params['amount'];
31: $radius = $this->_params['radius'];
32: $threshold = $this->_params['threshold'];
33:
34:
35: $amount = min($amount, 500);
36: $amount = $amount * 0.016;
37: if ($amount == 0) {
38: return true;
39: }
40:
41: $radius = min($radius, 50);
42: $radius = $radius * 2;
43:
44: $threshold = min($threshold, 255);
45:
46: $radius = abs(round($radius));
47: if ($radius == 0) {
48: return true;
49: }
50:
51: $img = $this->_image->_im;
52: $w = ImageSX($img);
53: $h = ImageSY($img);
54: $imgCanvas = ImageCreateTrueColor($w, $h);
55: $imgCanvas2 = ImageCreateTrueColor($w, $h);
56: $imgBlur = ImageCreateTrueColor($w, $h);
57: $imgBlur2 = ImageCreateTrueColor($w, $h);
58: ImageCopy($imgCanvas, $img, 0, 0, 0, 0, $w, $h);
59: ImageCopy($imgCanvas2, $img, 0, 0, 0, 0, $w, $h);
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71: for ($i = 0; $i < $radius; $i++) {
72: ImageCopy ($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1);
73: ImageCopyMerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50);
74: ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333);
75: ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25);
76: ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333);
77: ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25);
78: ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20 );
79: ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667);
80: ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50);
81: ImageCopy ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);
82:
83:
84:
85:
86:
87: ImageCopy ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h);
88: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50);
89: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333);
90: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25);
91: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333);
92: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25);
93: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20 );
94: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667);
95: ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50);
96: ImageCopy ($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h);
97: }
98:
99:
100:
101: for ($x = 0; $x < $w; $x++) {
102: for ($y = 0; $y < $h; $y++) {
103:
104: $rgbOrig = ImageColorAt($imgCanvas2, $x, $y);
105: $rOrig = (($rgbOrig >> 16) & 0xFF);
106: $gOrig = (($rgbOrig >> 8) & 0xFF);
107: $bOrig = ($rgbOrig & 0xFF);
108:
109: $rgbBlur = ImageColorAt($imgCanvas, $x, $y);
110: $rBlur = (($rgbBlur >> 16) & 0xFF);
111: $gBlur = (($rgbBlur >> 8) & 0xFF);
112: $bBlur = ($rgbBlur & 0xFF);
113:
114:
115:
116: $rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
117: $gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
118: $bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
119:
120: if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
121: $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
122: ImageSetPixel($img, $x, $y, $pixCol);
123: }
124: }
125: }
126: ImageDestroy($imgCanvas);
127: ImageDestroy($imgCanvas2);
128: ImageDestroy($imgBlur);
129: ImageDestroy($imgBlur2);
130:
131: return true;
132: }
133:
134: }
135: