root/devel-backup/units/phpthumb/phpthumb.filters.php

Revision 45, 42.3 kB (checked in by sven, 3 years ago)

dos2unix to clean line endings, set svn property eol-style native, some whitespace homogenisation

  • Property svn:eol-style set to native
Line 
1 <?php
2 //////////////////////////////////////////////////////////////
3 ///  phpThumb() by James Heinrich <info@silisoftware.com>   //
4 //        available at http://phpthumb.sourceforge.net     ///
5 //////////////////////////////////////////////////////////////
6 ///                                                         //
7 // phpthumb.filters.php - image processing filter functions //
8 //                                                         ///
9 //////////////////////////////////////////////////////////////
10
11 class phpthumb_filters {
12
13     function ApplyMask(&$gdimg_mask, &$gdimg_image) {
14         if (phpthumb_functions::gd_version() < 2) {
15             //$this->DebugMessage('Skipping ApplyMask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
16             return false;
17         }
18         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
19
20             //$this->DebugMessage('Using alpha ApplyMask() technique', __FILE__, __LINE__);
21             if ($gdimg_mask_resized = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
22
23                 ImageCopyResampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, ImageSX($gdimg_image), ImageSY($gdimg_image), ImageSX($gdimg_mask), ImageSY($gdimg_mask));
24                 if ($gdimg_mask_blendtemp = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
25
26                     $color_background = ImageColorAllocate($gdimg_mask_blendtemp, 0, 0, 0);
27                     ImageFilledRectangle($gdimg_mask_blendtemp, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp), $color_background);
28                     ImageAlphaBlending($gdimg_mask_blendtemp, false);
29                     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
30                         ImageSaveAlpha($gdimg_mask_blendtemp, true);
31                     }
32                     for ($x = 0; $x < ImageSX($gdimg_image); $x++) {
33                         for ($y = 0; $y < ImageSY($gdimg_image); $y++) {
34                             //$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y);
35                             $RealPixel = phpthumb_functions::GetPixelColor($gdimg_image, $x, $y);
36                             $MaskPixel = phpthumb_functions::GrayscalePixel(phpthumb_functions::GetPixelColor($gdimg_mask_resized, $x, $y));
37                             $MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127)));
38                             $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha);
39                             ImageSetPixel($gdimg_mask_blendtemp, $x, $y, $newcolor);
40                         }
41                     }
42                     ImageAlphaBlending($gdimg_image, false);
43                     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
44                         ImageSaveAlpha($gdimg_image, true);
45                     }
46                     ImageCopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp));
47                     ImageDestroy($gdimg_mask_blendtemp);
48
49                 } else {
50                     //$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
51                 }
52                 ImageDestroy($gdimg_mask_resized);
53
54             } else {
55                 //$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
56             }
57
58         } else {
59             // alpha merging requires PHP v4.3.2+
60             //$this->DebugMessage('Skipping ApplyMask() technique because PHP is v"'.phpversion().'"', __FILE__, __LINE__);
61         }
62         return true;
63     }
64
65
66     function Bevel(&$gdimg, $width, $hexcolor1, $hexcolor2) {
67         $width     = ($width     ? $width     : 5);
68         $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
69         $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
70
71         ImageAlphaBlending($gdimg, true);
72         for ($i = 0; $i < $width; $i++) {
73             $alpha = round(($i / $width) * 127);
74             $color1[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1, false, $alpha);
75             $color2[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2, false, $alpha);
76
77             ImageLine($gdimg,                   $i,                   $i,                   $i, ImageSY($gdimg) - $i, $color1[$i]); // left
78             ImageLine($gdimg,                   $i,                   $i, ImageSX($gdimg) - $i,                   $i, $color1[$i]); // top
79             ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i,                   $i, $color2[$i]); // right
80             ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i,                   $i, ImageSY($gdimg) - $i, $color2[$i]); // bottom
81         }
82         return true;
83     }
84
85
86     function Blur(&$gdimg, $radius) {
87         // Taken from Torstein Hønsi's phpUnsharpMask (see phpthumb.unsharp.php)
88
89         $radius = round(max(0, min($radius, 50)) * 2);
90         if (!$radius) {
91             return false;
92         }
93
94         $w = ImageSX($gdimg);
95         $h = ImageSY($gdimg);
96         if ($imgBlur = ImageCreateTrueColor($w, $h)) {
97             // Gaussian blur matrix:
98             //    1    2    1
99             //    2    4    2
100             //    1    2    1
101
102             // Move copies of the image around one pixel at the time and merge them with weight
103             // according to the matrix. The same matrix is simply repeated for higher radii.
104             for ($i = 0; $i < $radius; $i++)    {
105                 ImageCopy     ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1);            // up left
106                 ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w,     $h,     50.00000);  // down right
107                 ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h,     33.33333);  // down left
108                 ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w,     $h - 1, 25.00000);  // up right
109                 ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h,     33.33333);  // left
110                 ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w,     $h,     25.00000);  // right
111                 ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w,     $h - 1, 20.00000);  // up
112                 ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w,     $h,     16.666667); // down
113                 ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w,     $h,     50.000000); // center
114                 ImageCopy     ($gdimg, $imgBlur, 0, 0, 0, 0, $w,     $h);
115             }
116             return true;
117         }
118         return false;
119     }
120
121
122     function Colorize(&$gdimg, $amount, $targetColor) {
123         $amount      = (is_numeric($amount)                          ? $amount      : 25);
124         $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray');
125
126         // overridden below for grayscale
127         if ($targetColor != 'gray') {
128             $TargetPixel['red']   = hexdec(substr($targetColor, 0, 2));
129             $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
130             $TargetPixel['blue']  = hexdec(substr($targetColor, 4, 2));
131         }
132
133         for ($x = 0; $x < ImageSX($gdimg); $x++) {
134             for ($y = 0; $y < ImageSY($gdimg); $y++) {
135                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
136                 if ($targetColor == 'gray') {
137                     $TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
138                 }
139                 foreach ($TargetPixel as $key => $value) {
140                     $NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * ($amount / 100)))));
141                 }
142                 //$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
143                 $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
144                 ImageSetPixel($gdimg, $x, $y, $newColor);
145             }
146         }
147         return true;
148     }
149
150
151     function Desaturate(&$gdimg, $amount, $color='') {
152         return phpthumb_filters::Colorize($gdimg, $amount, (phpthumb_functions::IsHexColor($color) ? $color : 'gray'));
153     }
154
155
156     function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $fade) {
157         if (phpthumb_functions::gd_version() < 2) {
158             return false;
159         }
160         $distance = ($distance ? $distance : 10);
161         $width    = ($width    ? $width    : 10);
162         $hexcolor = ($hexcolor ? $hexcolor : '000000');
163         $angle    = ($angle    ? $angle    : 225);
164         $fade     = ($fade     ? $fade     : 1);
165
166         $width_shadow  = cos(deg2rad($angle)) * ($distance + $width);
167         $height_shadow = sin(deg2rad($angle)) * ($distance + $width);
168
169         $scaling = min(ImageSX($gdimg) / (ImageSX($gdimg) + abs($width_shadow)), ImageSY($gdimg) / (ImageSY($gdimg) + abs($height_shadow)));
170
171         for ($i = 0; $i < $width; $i++) {
172             $WidthAlpha[$i] = (abs(($width / 2) - $i) / $width) * $fade;
173             $Offset['x'] = cos(deg2rad($angle)) * ($distance + $i);
174             $Offset['y'] = sin(deg2rad($angle)) * ($distance + $i);
175         }
176
177         $tempImageWidth  = ImageSX($gdimg)  + abs($Offset['x']);
178         $tempImageHeight = ImageSY($gdimg) + abs($Offset['y']);
179
180         if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) {
181
182             ImageAlphaBlending($gdimg_dropshadow_temp, false);
183             if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
184                 ImageSaveAlpha($gdimg_dropshadow_temp, true);
185             }
186             $transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127);
187             ImageFill($gdimg_dropshadow_temp, 0, 0, $transparent1);
188
189             for ($x = 0; $x < ImageSX($gdimg); $x++) {
190                 for ($y = 0; $y < ImageSY($gdimg); $y++) {
191                     $PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
192                 }
193             }
194             for ($x = 0; $x < $tempImageWidth; $x++) {
195                 for ($y = 0; $y < $tempImageHeight; $y++) {
196                     //for ($i = 0; $i < $width; $i++) {
197                     for ($i = 0; $i < 1; $i++) {
198                         if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
199                             if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) {
200                                 $thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']);
201                                 ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
202                             }
203                         }
204                     }
205                 }
206             }
207
208             ImageAlphaBlending($gdimg_dropshadow_temp, true);
209             for ($x = 0; $x < ImageSX($gdimg); $x++) {
210                 for ($y = 0; $y < ImageSY($gdimg); $y++) {
211                     if ($PixelMap[$x][$y]['alpha'] < 127) {
212                         $thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
213                         ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
214                     }
215                 }
216             }
217
218             if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
219                 ImageSaveAlpha($gdimg, true);
220             }
221             ImageAlphaBlending($gdimg, false);
222             //$this->is_alpha = true;
223             $transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127);
224             ImageFilledRectangle($gdimg, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $transparent2);
225             ImageCopyResampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg_dropshadow_temp), ImageSY($gdimg_dropshadow_temp));
226
227             ImageDestroy($gdimg_dropshadow_temp);
228         }
229         return true;
230     }
231
232
233     function Elipse($gdimg) {
234         if (phpthumb_functions::gd_version() < 2) {
235             return false;
236         }
237         // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
238         if ($gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg) * 2, ImageSY($gdimg) * 2)) {
239             if ($gdimg_elipsemask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
240
241                 $color_transparent = ImageColorAllocate($gdimg_elipsemask_double, 255, 255, 255);
242                 ImageFilledEllipse($gdimg_elipsemask_double, ImageSX($gdimg), ImageSY($gdimg), (ImageSX($gdimg) - 1) * 2, (ImageSY($gdimg) - 1) * 2, $color_transparent);
243                 ImageCopyResampled($gdimg_elipsemask, $gdimg_elipsemask_double, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg) * 2, ImageSY($gdimg) * 2);
244
245                 phpthumb_filters::ApplyMask($gdimg_elipsemask, $gdimg);
246                 ImageDestroy($gdimg_elipsemask);
247                 return true;
248
249             } else {
250                 //$this->DebugMessage('$gdimg_elipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
251             }
252             ImageDestroy($gdimg_elipsemask_double);
253         } else {
254             //$this->DebugMessage('$gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
255         }
256         return false;
257     }
258
259
260     function Flip(&$gdimg, $x=false, $y=false) {
261         if (!$x && !$y) {
262             return false;
263         }
264         if ($tempImage = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
265             if ($x) {
266                 ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
267                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
268                     ImageCopy($gdimg, $tempImage, ImageSX($gdimg) - 1 - $x, 0, $x, 0, 1, ImageSY($gdimg));
269                 }
270             }
271             if ($y) {
272                 ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
273                 for ($y = 0; $y < ImageSY($gdimg); $y++) {
274                     ImageCopy($gdimg, $tempImage, 0, ImageSY($gdimg) - 1 - $y, 0, $y, ImageSX($gdimg), 1);
275                 }
276             }
277             ImageDestroy($tempImage);
278         }
279         return true;
280     }
281
282
283     function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) {
284         $frame_width    = ($frame_width    ? $frame_width    : 5);
285         $edge_width     = ($edge_width     ? $edge_width     : 1);
286         $hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC');
287         $hexcolor1      = ($hexcolor1      ? $hexcolor1      : 'FFFFFF');
288         $hexcolor2      = ($hexcolor2      ? $hexcolor2      : '000000');
289
290         $color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame);
291         $color1      = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1);
292         $color2      = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2);
293         for ($i = 0; $i < $edge_width; $i++) {
294             // outer bevel
295             ImageLine($gdimg,                   $i,                   $i,                   $i, ImageSY($gdimg) - $i, $color1); // left
296             ImageLine($gdimg,                   $i,                   $i, ImageSX($gdimg) - $i,                   $i, $color1); // top
297             ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i,                   $i, $color2); // right
298             ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i,                   $i, ImageSY($gdimg) - $i, $color2); // bottom
299         }
300         for ($i = 0; $i < $frame_width; $i++) {
301             // actual frame
302             ImageRectangle($gdimg, $edge_width + $i, $edge_width + $i, ImageSX($gdimg) - $edge_width - $i, ImageSY($gdimg) - $edge_width - $i, $color_frame);
303         }
304         for ($i