1 | <?php |
---|
2 | // Version 1.1 |
---|
3 | $archivo = explode('.',$_GET['img']); |
---|
4 | |
---|
5 | if ( strtolower($archivo[1]) == ( 'jpg' || 'jpeg' ) ) { |
---|
6 | header("Content-type: image/jpeg"); |
---|
7 | $srcimage = imagecreatefromjpeg($_GET['img']); |
---|
8 | } elseif ( strtolower($archivo[1]) == 'png' ) { |
---|
9 | header("Content-type: image/png"); |
---|
10 | $srcimage = imagecreatefrompng($_GET['img']); |
---|
11 | } elseif ( strtolower($archivo[1]) == 'gif' ) { |
---|
12 | header("Content-type: image/gif"); |
---|
13 | $srcimage = imagecreatefromgif($_GET['img']); |
---|
14 | } |
---|
15 | |
---|
16 | $width = imagesx($srcimage); |
---|
17 | $height = imagesy($srcimage); |
---|
18 | |
---|
19 | if ( isset($_GET['tamano']) && ( $width > $height ) && $_GET['tamano'] < $width ) { |
---|
20 | $t_width = $_GET['tamano']; |
---|
21 | $proporcion = $t_width / $width; |
---|
22 | $t_height = $proporcion * $height; |
---|
23 | } elseif ( isset($_GET['tamano']) && ( $height > $width ) && $_GET['tamano'] < $height ) { |
---|
24 | $t_height = $_GET['tamano']; |
---|
25 | $proporcion = $t_height / $height; |
---|
26 | $t_width = $proporcion * $width; |
---|
27 | } else { |
---|
28 | $t_width = imagesx($srcimage); |
---|
29 | $t_height = imagesy($srcimage); |
---|
30 | } |
---|
31 | |
---|
32 | $destimage = imagecreatetruecolor($t_width,$t_height); |
---|
33 | |
---|
34 | imagecopyresampled($destimage,$srcimage,0,0,0,0,$t_width,$t_height,$width,$height); |
---|
35 | if (isset($_GET['tamano'])) { |
---|
36 | if (strtolower($archivo[1]) == ( 'jpg' || 'jppeg')) { |
---|
37 | imagejpeg(UnsharpMask($destimage,80,0.5,3)); |
---|
38 | } elseif (strtolower($archivo[1]) == 'png') { |
---|
39 | imagepng(UnsharpMask($destimage,80,0.5,3)); |
---|
40 | } elseif (strtolower($archivo[1]) == 'gif') { |
---|
41 | imagegif(UnsharpMask($destimage,80,0.5,3)); |
---|
42 | } |
---|
43 | } else { |
---|
44 | if (strtolower($archivo[1]) == ( 'jpg' || 'jpeg' )) { |
---|
45 | imagejpeg($destimage); |
---|
46 | } elseif (strtolower($archivo[1]) == 'png') { |
---|
47 | imagepng($destimage); |
---|
48 | } elseif (strtolower($archivo[1]) == 'gif') { |
---|
49 | imagegif($destimage); |
---|
50 | } |
---|
51 | } |
---|
52 | imagedestroy($destimage); |
---|
53 | |
---|
54 | /* |
---|
55 | |
---|
56 | WARNING! Due to a known bug in PHP 4.3.2 this script is not working well in this version. The sharpened images get too dark. The bug is fixed in version 4.3.3. |
---|
57 | |
---|
58 | |
---|
59 | Unsharp masking is a traditional darkroom technique that has proven very suitable for |
---|
60 | digital imaging. The principle of unsharp masking is to create a blurred copy of the image |
---|
61 | and compare it to the underlying original. The difference in colour values |
---|
62 | between the two images is greatest for the pixels near sharp edges. When this |
---|
63 | difference is subtracted from the original image, the edges will be |
---|
64 | accentuated. |
---|
65 | |
---|
66 | The Amount parameter simply says how much of the effect you want. 100 is 'normal'. |
---|
67 | Radius is the radius of the blurring circle of the mask. 'Threshold' is the least |
---|
68 | difference in colour values that is allowed between the original and the mask. In practice |
---|
69 | this means that low-contrast areas of the picture are left unrendered whereas edges |
---|
70 | are treated normally. This is good for pictures of e.g. skin or blue skies. |
---|
71 | |
---|
72 | Any suggenstions for improvement of the algorithm, expecially regarding the speed |
---|
73 | and the roundoff errors in the Gaussian blur process, are welcome. |
---|
74 | |
---|
75 | */ |
---|
76 | |
---|
77 | function UnsharpMask($img, $amount, $radius, $threshold) { |
---|
78 | |
---|
79 | //////////////////////////////////////////////////////////////////////////////////////////////// |
---|
80 | //// |
---|
81 | //// p h p U n s h a r p M a s k |
---|
82 | //// |
---|
83 | //// Unsharp mask algorithm by Torstein Hnsi 2003. |
---|
84 | //// thoensi_at_netcom_dot_no. |
---|
85 | //// Please leave this notice. |
---|
86 | //// |
---|
87 | /////////////////////////////////////////////////////////////////////////////////////////////// |
---|
88 | |
---|
89 | |
---|
90 | // $img is an image that is already created within php using |
---|
91 | // imgcreatetruecolor. No url! $img must be a truecolor image. |
---|
92 | |
---|
93 | // Attempt to calibrate the parameters to Photoshop: |
---|
94 | if ($amount > 500) $amount = 500; |
---|
95 | $amount = $amount * 0.016; |
---|
96 | if ($radius > 50) $radius = 50; |
---|
97 | $radius = $radius * 2; |
---|
98 | if ($threshold > 255) $threshold = 255; |
---|
99 | |
---|
100 | $radius = abs(round($radius)); // Only integers make sense. |
---|
101 | if ($radius == 0) { |
---|
102 | return $img; imagedestroy($img); break; } |
---|
103 | $w = imagesx($img); $h = imagesy($img); |
---|
104 | $imgCanvas = imagecreatetruecolor($w, $h); |
---|
105 | $imgCanvas2 = imagecreatetruecolor($w, $h); |
---|
106 | $imgBlur = imagecreatetruecolor($w, $h); |
---|
107 | $imgBlur2 = imagecreatetruecolor($w, $h); |
---|
108 | imagecopy ($imgCanvas, $img, 0, 0, 0, 0, $w, $h); |
---|
109 | imagecopy ($imgCanvas2, $img, 0, 0, 0, 0, $w, $h); |
---|
110 | |
---|
111 | |
---|
112 | // Gaussian blur matrix: |
---|
113 | // |
---|
114 | // 1 2 1 |
---|
115 | // 2 4 2 |
---|
116 | // 1 2 1 |
---|
117 | // |
---|
118 | ////////////////////////////////////////////////// |
---|
119 | |
---|
120 | // Move copies of the image around one pixel at the time and merge them with weight |
---|
121 | // according to the matrix. The same matrix is simply repeated for higher radii. |
---|
122 | for ($i = 0; $i < $radius; $i++) { |
---|
123 | imagecopy ($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // up left |
---|
124 | imagecopymerge ($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right |
---|
125 | imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left |
---|
126 | imagecopymerge ($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right |
---|
127 | imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left |
---|
128 | imagecopymerge ($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right |
---|
129 | imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20 ); // up |
---|
130 | imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down |
---|
131 | imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center |
---|
132 | imagecopy ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h); |
---|
133 | |
---|
134 | // During the loop above the blurred copy darkens, possibly due to a roundoff |
---|
135 | // error. Therefore the sharp picture has to go through the same loop to |
---|
136 | // produce a similar image for comparison. This is not a good thing, as processing |
---|
137 | // time increases heavily. |
---|
138 | imagecopy ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h); |
---|
139 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); |
---|
140 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); |
---|
141 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); |
---|
142 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333); |
---|
143 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25); |
---|
144 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20 ); |
---|
145 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667); |
---|
146 | imagecopymerge ($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50); |
---|
147 | imagecopy ($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h); |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | // Calculate the difference between the blurred pixels and the original |
---|
152 | // and set the pixels |
---|
153 | for ($x = 0; $x < $w; $x++) { // each row |
---|
154 | for ($y = 0; $y < $h; $y++) { // each pixel |
---|
155 | |
---|
156 | $rgbOrig = ImageColorAt($imgCanvas2, $x, $y); |
---|
157 | $rOrig = (($rgbOrig >> 16) & 0xFF); |
---|
158 | $gOrig = (($rgbOrig >> 8) & 0xFF); |
---|
159 | $bOrig = ($rgbOrig & 0xFF); |
---|
160 | |
---|
161 | $rgbBlur = ImageColorAt($imgCanvas, $x, $y); |
---|
162 | |
---|
163 | $rBlur = (($rgbBlur >> 16) & 0xFF); |
---|
164 | $gBlur = (($rgbBlur >> 8) & 0xFF); |
---|
165 | $bBlur = ($rgbBlur & 0xFF); |
---|
166 | |
---|
167 | // When the masked pixels differ less from the original |
---|
168 | // than the threshold specifies, they are set to their original value. |
---|
169 | $rNew = (abs($rOrig - $rBlur) >= $threshold) |
---|
170 | ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) |
---|
171 | : $rOrig; |
---|
172 | $gNew = (abs($gOrig - $gBlur) >= $threshold) |
---|
173 | ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) |
---|
174 | : $gOrig; |
---|
175 | $bNew = (abs($bOrig - $bBlur) >= $threshold) |
---|
176 | ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) |
---|
177 | : $bOrig; |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) { |
---|
182 | $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); |
---|
183 | ImageSetPixel($img, $x, $y, $pixCol); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | imagedestroy($imgCanvas); |
---|
189 | imagedestroy($imgCanvas2); |
---|
190 | imagedestroy($imgBlur); |
---|
191 | imagedestroy($imgBlur2); |
---|
192 | |
---|
193 | return $img; |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | ?> |
---|