OSDN Git Service

original file
[nucleus-jp/nucleus-plugins.git] / trunk / NP_TinyMCE2j / tinymce2j / plugins / ibrowser / scripts / phpThumb / phpthumb.filters.php
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 BlurGaussian(&$gdimg) {
123                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
124                         if (ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)) {
125                                 return true;
126                         }
127                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)', __FILE__, __LINE__);
128                         // fall through and try it the hard way
129                 }
130                 // currently not implemented "the hard way"
131                 $this->DebugMessage('FAILED: phpthumb_filters::BlurGaussian($gdimg) [function not implemented]', __FILE__, __LINE__);
132                 return false;
133         }
134
135
136         function BlurSelective(&$gdimg) {
137                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
138                         if (ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)) {
139                                 return true;
140                         }
141                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)', __FILE__, __LINE__);
142                         // fall through and try it the hard way
143                 }
144                 // currently not implemented "the hard way"
145                 $this->DebugMessage('FAILED: phpthumb_filters::BlurSelective($gdimg) [function not implemented]', __FILE__, __LINE__);
146                 return false;
147         }
148
149
150         function Brightness(&$gdimg, $amount=0) {
151                 if ($amount == 0) {
152                         return true;
153                 }
154                 $amount = max(-255, min(255, $amount));
155
156                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
157                         if (ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, $amount)) {
158                                 return true;
159                         }
160                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, '.$amount.')', __FILE__, __LINE__);
161                         // fall through and try it the hard way
162                 }
163
164                 $scaling = (255 - abs($amount)) / 255;
165                 $baseamount = (($amount > 0) ? $amount : 0);
166                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
167                         for ($y = 0; $y < ImageSY($gdimg); $y++) {
168                                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
169                                 foreach ($OriginalPixel as $key => $value) {
170                                         $NewPixel[$key] = round($baseamount + ($OriginalPixel[$key] * $scaling));
171                                 }
172                                 $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
173                                 ImageSetPixel($gdimg, $x, $y, $newColor);
174                         }
175                 }
176                 return true;
177         }
178
179
180         function Contrast(&$gdimg, $amount=0) {
181                 if ($amount == 0) {
182                         return true;
183                 }
184                 $amount = max(-255, min(255, $amount));
185
186                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
187                         if (ImageFilter($gdimg, IMG_FILTER_CONTRAST, $amount)) {
188                                 return true;
189                         }
190                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_CONTRAST, '.$amount.')', __FILE__, __LINE__);
191                         // fall through and try it the hard way
192                 }
193
194                 if ($amount > 0) {
195                         $scaling = 1 + ($amount / 255);
196                 } else {
197                         $scaling = (255 - abs($amount)) / 255;
198                 }
199                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
200                         for ($y = 0; $y < ImageSY($gdimg); $y++) {
201                                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
202                                 foreach ($OriginalPixel as $key => $value) {
203                                         $NewPixel[$key] = min(255, max(0, round($OriginalPixel[$key] * $scaling)));
204                                 }
205                                 $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
206                                 ImageSetPixel($gdimg, $x, $y, $newColor);
207                         }
208                 }
209         }
210
211
212         function Colorize(&$gdimg, $amount, $targetColor) {
213                 $amount      = (is_numeric($amount)                          ? $amount      : 25);
214                 $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray');
215
216                 if ($amount == 0) {
217                         return true;
218                 }
219
220                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
221                         if ($targetColor == 'gray') {
222                                 $targetColor = '808080';
223                         }
224                         $r = substr($targetColor, 0, 2);
225                         $g = substr($targetColor, 2, 2);
226                         $b = substr($targetColor, 4, 2);
227                         if (ImageFilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
228                                 return true;
229                         }
230                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
231                         // fall through and try it the hard way
232                 }
233
234                 // overridden below for grayscale
235                 if ($targetColor != 'gray') {
236                         $TargetPixel['red']   = hexdec(substr($targetColor, 0, 2));
237                         $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
238                         $TargetPixel['blue']  = hexdec(substr($targetColor, 4, 2));
239                 }
240
241                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
242                         for ($y = 0; $y < ImageSY($gdimg); $y++) {
243                                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
244                                 if ($targetColor == 'gray') {
245                                         $TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
246                                 }
247                                 foreach ($TargetPixel as $key => $value) {
248                                         $NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * ($amount / 100)))));
249                                 }
250                                 //$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
251                                 $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
252                                 ImageSetPixel($gdimg, $x, $y, $newColor);
253                         }
254                 }
255                 return true;
256         }
257
258
259         function Desaturate(&$gdimg, $amount, $color='') {
260                 if ($amount == 0) {
261                         return true;
262                 }
263                 return phpthumb_filters::Colorize($gdimg, $amount, (phpthumb_functions::IsHexColor($color) ? $color : 'gray'));
264         }
265
266
267         function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $fade) {
268                 if (phpthumb_functions::gd_version() < 2) {
269                         return false;
270                 }
271                 $distance = ($distance ? $distance : 10);
272                 $width    = ($width    ? $width    : 10);
273                 $hexcolor = ($hexcolor ? $hexcolor : '000000');
274                 $angle    = ($angle    ? $angle    : 225);
275                 $fade     = ($fade     ? $fade     : 1);
276
277                 $width_shadow  = cos(deg2rad($angle)) * ($distance + $width);
278                 $height_shadow = sin(deg2rad($angle)) * ($distance + $width);
279
280                 $scaling = min(ImageSX($gdimg) / (ImageSX($gdimg) + abs($width_shadow)), ImageSY($gdimg) / (ImageSY($gdimg) + abs($height_shadow)));
281
282                 for ($i = 0; $i < $width; $i++) {
283                         $WidthAlpha[$i] = (abs(($width / 2) - $i) / $width) * $fade;
284                         $Offset['x'] = cos(deg2rad($angle)) * ($distance + $i);
285                         $Offset['y'] = sin(deg2rad($angle)) * ($distance + $i);
286                 }
287
288                 $tempImageWidth  = ImageSX($gdimg)  + abs($Offset['x']);
289                 $tempImageHeight = ImageSY($gdimg) + abs($Offset['y']);
290
291                 if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) {
292
293                         ImageAlphaBlending($gdimg_dropshadow_temp, false);
294                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
295                                 ImageSaveAlpha($gdimg_dropshadow_temp, true);
296                         }
297                         $transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127);
298                         ImageFill($gdimg_dropshadow_temp, 0, 0, $transparent1);
299
300                         for ($x = 0; $x < ImageSX($gdimg); $x++) {
301                                 for ($y = 0; $y < ImageSY($gdimg); $y++) {
302                                         $PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
303                                 }
304                         }
305                         for ($x = 0; $x < $tempImageWidth; $x++) {
306                                 for ($y = 0; $y < $tempImageHeight; $y++) {
307                                         //for ($i = 0; $i < $width; $i++) {
308                                         for ($i = 0; $i < 1; $i++) {
309                                                 if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
310                                                         if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) {
311                                                                 $thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']);
312                                                                 ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
313                                                         }
314                                                 }
315                                         }
316                                 }
317                         }
318
319                         ImageAlphaBlending($gdimg_dropshadow_temp, true);
320                         for ($x = 0; $x < ImageSX($gdimg); $x++) {
321                                 for ($y = 0; $y < ImageSY($gdimg); $y++) {
322                                         if ($PixelMap[$x][$y]['alpha'] < 127) {
323                                                 $thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
324                                                 ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
325                                         }
326                                 }
327                         }
328
329                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
330                                 ImageSaveAlpha($gdimg, true);
331                         }
332                         ImageAlphaBlending($gdimg, false);
333                         //$this->is_alpha = true;
334                         $transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127);
335                         ImageFilledRectangle($gdimg, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $transparent2);
336                         ImageCopyResampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg_dropshadow_temp), ImageSY($gdimg_dropshadow_temp));
337
338                         ImageDestroy($gdimg_dropshadow_temp);
339                 }
340                 return true;
341         }
342
343
344         function EdgeDetect(&$gdimg) {
345                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
346                         if (ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)) {
347                                 return true;
348                         }
349                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)', __FILE__, __LINE__);
350                         // fall through and try it the hard way
351                 }
352                 // currently not implemented "the hard way"
353                 $this->DebugMessage('FAILED: phpthumb_filters::EdgeDetect($gdimg) [function not implemented]', __FILE__, __LINE__);
354                 return false;
355         }
356
357
358         function Elipse($gdimg) {
359                 if (phpthumb_functions::gd_version() < 2) {
360                         return false;
361                 }
362                 // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
363                 if ($gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg) * 2, ImageSY($gdimg) * 2)) {
364                         if ($gdimg_elipsemask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
365
366                                 $color_transparent = ImageColorAllocate($gdimg_elipsemask_double, 255, 255, 255);
367                                 ImageFilledEllipse($gdimg_elipsemask_double, ImageSX($gdimg), ImageSY($gdimg), (ImageSX($gdimg) - 1) * 2, (ImageSY($gdimg) - 1) * 2, $color_transparent);
368                                 ImageCopyResampled($gdimg_elipsemask, $gdimg_elipsemask_double, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg) * 2, ImageSY($gdimg) * 2);
369
370                                 phpthumb_filters::ApplyMask($gdimg_elipsemask, $gdimg);
371                                 ImageDestroy($gdimg_elipsemask);
372                                 return true;
373
374                         } else {
375                                 //$this->DebugMessage('$gdimg_elipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
376                         }
377                         ImageDestroy($gdimg_elipsemask_double);
378                 } else {
379                         //$this->DebugMessage('$gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
380                 }
381                 return false;
382         }
383
384
385         function Emboss(&$gdimg) {
386                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
387                         if (ImageFilter($gdimg, IMG_FILTER_EMBOSS)) {
388                                 return true;
389                         }
390                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EMBOSS)', __FILE__, __LINE__);
391                         // fall through and try it the hard way
392                 }
393                 // currently not implemented "the hard way"
394                 $this->DebugMessage('FAILED: phpthumb_filters::Emboss($gdimg) [function not implemented]', __FILE__, __LINE__);
395                 return false;
396         }
397
398
399         function Flip(&$gdimg, $x=false, $y=false) {
400                 if (!$x && !$y) {
401                         return false;
402                 }
403                 if ($tempImage = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
404                         if ($x) {
405                                 ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
406                                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
407                                         ImageCopy($gdimg, $tempImage, ImageSX($gdimg) - 1 - $x, 0, $x, 0, 1, ImageSY($gdimg));
408                                 }
409                         }
410                         if ($y) {
411                                 ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
412                                 for ($y = 0; $y < ImageSY($gdimg); $y++) {
413                                         ImageCopy($gdimg, $tempImage, 0, ImageSY($gdimg) - 1 - $y, 0, $y, ImageSX($gdimg), 1);
414                                 }
415                         }
416                         ImageDestroy($tempImage);
417                 }
418                 return true;
419         }
420
421
422         function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) {
423                 $frame_width    = ($frame_width    ? $frame_width    : 5);
424                 $edge_width     = ($edge_width     ? $edge_width     : 1);
425                 $hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC');
426                 $hexcolor1      = ($hexcolor1      ? $hexcolor1      : 'FFFFFF');
427                 $hexcolor2      = ($hexcolor2      ? $hexcolor2      : '000000');
428
429                 $color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame);
430                 $color1      = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1);
431                 $color2      = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2);
432                 for ($i = 0; $i < $edge_width; $i++) {
433                         // outer bevel
434                         ImageLine($gdimg,                   $i,                   $i,                   $i, ImageSY($gdimg) - $i, $color1); // left
435                         ImageLine($gdimg,                   $i,                   $i, ImageSX($gdimg) - $i,                   $i, $color1); // top
436                         ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i,                   $i, $color2); // right
437                         ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i,                   $i, ImageSY($gdimg) - $i, $color2); // bottom
438                 }
439                 for ($i = 0; $i < $frame_width; $i++) {
440                         // actual frame
441                         ImageRectangle($gdimg, $edge_width + $i, $edge_width + $i, ImageSX($gdimg) - $edge_width - $i, ImageSY($gdimg) - $edge_width - $i, $color_frame);
442                 }
443                 for ($i = 0; $i < $edge_width; $i++) {
444                         // inner bevel
445                         ImageLine($gdimg,                   $frame_width + $edge_width + $i,                   $frame_width + $edge_width + $i,                   $frame_width + $edge_width + $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, $color2); // left
446                         ImageLine($gdimg,                   $frame_width + $edge_width + $i,                   $frame_width + $edge_width + $i, ImageSX($gdimg) - $frame_width - $edge_width - $i,                   $frame_width + $edge_width + $i, $color2); // top
447                         ImageLine($gdimg, ImageSX($gdimg) - $frame_width - $edge_width - $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, ImageSX($gdimg) - $frame_width - $edge_width - $i,                   $frame_width + $edge_width + $i, $color1); // right
448                         ImageLine($gdimg, ImageSX($gdimg) - $frame_width - $edge_width - $i, ImageSY($gdimg) - $frame_width - $edge_width - $i,                   $frame_width + $edge_width + $i, ImageSY($gdimg) - $frame_width - $edge_width - $i, $color1); // bottom
449                 }
450                 return true;
451         }
452
453
454         function Gamma(&$gdimg, $amount) {
455                 if (number_format($amount, 4) == '1.0000') {
456                         return true;
457                 }
458                 return ImageGammaCorrect($gdimg, 1.0, $amount);
459         }
460
461
462         function Grayscale(&$gdimg) {
463                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
464                         if (ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)) {
465                                 return true;
466                         }
467                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
468                         // fall through and try it the hard way
469                 }
470                 return phpthumb_filters::Colorize($gdimg, 100, 'gray');
471         }
472
473
474         function HistogramAnalysis(&$gdimg, $calculateGray=false) {
475                 $ImageSX = ImageSX($gdimg);
476                 $ImageSY = ImageSY($gdimg);
477                 for ($x = 0; $x < $ImageSX; $x++) {
478                         for ($y = 0; $y < $ImageSY; $y++) {
479                                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
480                                 @$Analysis['red'][$OriginalPixel['red']]++;
481                                 @$Analysis['green'][$OriginalPixel['green']]++;
482                                 @$Analysis['blue'][$OriginalPixel['blue']]++;
483                                 @$Analysis['alpha'][$OriginalPixel['alpha']]++;
484                                 if ($calculateGray) {
485                                         $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
486                                         @$Analysis['gray'][$GrayPixel['red']]++;
487                                 }
488                         }
489                 }
490                 $keys = array('red', 'green', 'blue', 'alpha');
491                 if ($calculateGray) {
492                         $keys[] = 'gray';
493                 }
494                 foreach ($keys as $key) {
495                         ksort($Analysis[$key]);
496                 }
497                 return $Analysis;
498         }
499
500
501         function HistogramStretch(&$gdimg, $band='*', $min=-1, $max=-1) {
502                 // equivalent of "Auto Contrast" in Adobe Photoshop
503
504                 $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
505                 $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
506                 if (!isset($keys[$band])) {
507                         return false;
508                 }
509                 $key = $keys[$band];
510
511                 // If the absolute brightest and darkest pixels are used then one random
512                 // pixel in the image could throw off the whole system. Instead, count up/down
513                 // from the limit and allow 0.1% of brightest/darkest pixels to be clipped to min/max
514                 $clip_threshold = ImageSX($gdimg) * ImageSX($gdimg) * 0.001;
515                 if ($min >= 0) {
516                         $range_min = min($min, 255);
517                 } else {
518                         $countsum = 0;
519                         for ($i = 0; $i <= 255; $i++) {
520                                 $countsum += @$Analysis[$key][$i];
521                                 if ($countsum >= $clip_threshold) {
522                                         $range_min = $i - 1;
523                                         break;
524                                 }
525                         }
526                         $range_min = max($range_min, 0);
527                 }
528                 if ($max >= 0) {
529                         $range_max = max($max, 255);
530                 } else {
531                         $countsum = 0;
532                         $threshold = ImageSX($gdimg) * ImageSX($gdimg) * 0.001; // 0.1% of brightest and darkest pixels can be clipped
533                         for ($i = 255; $i >= 0; $i--) {
534                                 $countsum += @$Analysis[$key][$i];
535                                 if ($countsum >= $clip_threshold) {
536                                         $range_max = $i + 1;
537                                         break;
538                                 }
539                         }
540                         $range_max = min($range_max, 255);
541                 }
542                 $range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min)));
543                 if (($range_min == 0) && ($range_max == 255)) {
544                         // no adjustment neccesary - don't waste CPU time!
545                         return true;
546                 }
547
548                 $ImageSX = ImageSX($gdimg);
549                 $ImageSY = ImageSY($gdimg);
550                 for ($x = 0; $x < $ImageSX; $x++) {
551                         for ($y = 0; $y < $ImageSY; $y++) {
552                                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
553                                 if ($band == '*') {
554                                         $new['red']   = min(255, max(0, ($OriginalPixel['red']   - $range_min) * $range_scale));
555                                         $new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale));
556                                         $new['blue']  = min(255, max(0, ($OriginalPixel['blue']  - $range_min) * $range_scale));
557                                         $new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale));
558                                 } else {
559                                         $new = $OriginalPixel;
560                                         $new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale));
561                                 }
562                                 $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']);
563                                 ImageSetPixel($gdimg, $x, $y, $newColor);
564                         }
565                 }
566
567                 return true;
568         }
569
570
571         function HistogramOverlay(&$gdimg, $bands='*', $colors='', $width=0.25, $height=0.25, $alignment='BR', $opacity=50, $margin=5) {
572                 $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
573
574                 $histW = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
575                 $histH = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
576                 if ($gdHist = ImageCreateTrueColor($histW, $histH)) {
577                         $color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127);
578                         ImageFilledRectangle($gdHist, 0, 0, $histW, $histH, $color_back);
579                         ImageAlphaBlending($gdHist, false);
580                         ImageSaveAlpha($gdHist, true);
581
582                         if ($gdHistTemp = ImageCreateTrueColor(256, 100)) {
583                                 $color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127);
584                                 ImageAlphaBlending($gdHistTemp, false);
585                                 ImageSaveAlpha($gdHistTemp, true);
586                                 ImageFilledRectangle($gdHistTemp, 0, 0, ImageSX($gdHistTemp), ImageSY($gdHistTemp), $color_back_temp);
587
588                                 $DefaultColors = array('r'=>'FF0000', 'g'=>'00FF00', 'b'=>'0000FF', 'a'=>'999999', '*'=>'FFFFFF');
589                                 $Colors = explode(';', $colors);
590                                 $BandsToGraph = array_unique(preg_split('//', $bands));
591                                 $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
592                                 foreach ($BandsToGraph as $key => $band) {
593                                         if (!isset($keys[$band])) {
594                                                 continue;
595                                         }
596                                         $PeakValue = max($Analysis[$keys[$band]]);
597                                         $thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]);
598                                         $tempHeight = ImageSY($gdHistTemp);
599                                         for ($x = 0; $x <= 255; $x++) {
600                                                 ImageLine($gdHistTemp, $x, $tempHeight - 1, $x, $tempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $tempHeight), $thisColor);
601                                         }
602                                         ImageLine($gdHistTemp, 0, $tempHeight - 1, 255, $tempHeight - 1, $thisColor);
603                                         ImageLine($gdHistTemp, 0, $tempHeight - 2, 255, $tempHeight - 2, $thisColor);
604                                 }
605                                 ImageCopyResampled($gdHist, $gdHistTemp, 0, 0, 0, 0, ImageSX($gdHist), ImageSY($gdHist), ImageSX($gdHistTemp), ImageSY($gdHistTemp));
606                                 ImageDestroy($gdHistTemp);
607                         } else {
608                                 return false;
609                         }
610
611                         phpthumb_filters::WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin);
612                         ImageDestroy($gdHist);
613                         return true;
614                 }
615                 return false;
616         }
617
618
619         function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) {
620                 $border_width = ($border_width ? $border_width : 1);
621                 $radius_x     = ($radius_x     ? $radius_x     : 0);
622                 $radius_y     = ($radius_y     ? $radius_y     : 0);
623
624                 $output_width  = ImageSX($gdimg);
625                 $output_height = ImageSY($gdimg);
626
627                 list($new_width, $new_height) = phpthumb_functions::ProportionalResize($output_width, $output_height, $output_width - max($border_width * 2, $radius_x), $output_height - max($border_width * 2, $radius_y));
628                 $offset_x = ($radius_x ? $output_width  - $new_width  - $radius_x : 0);
629                 $offset_y = ($radius_y ? $output_height - $new_height - $radius_y : 0);
630
631                 if ($gd_border_canvas = phpthumb_functions::ImageCreateFunction($output_width, $output_height)) {
632
633                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
634                                 ImageSaveAlpha($gd_border_canvas, true);
635                         }
636                         ImageAlphaBlending($gd_border_canvas, false);
637                         $color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gd_border_canvas, 255, 255, 255, 127);
638                         ImageFilledRectangle($gd_border_canvas, 0, 0, $output_width, $output_height, $color_background);
639
640                         $color_border = phpthumb_functions::ImageHexColorAllocate($gd_border_canvas, (phpthumb_functions::IsHexColor($hexcolor_border) ? $hexcolor_border : '000000'));
641
642                         for ($i = 0; $i < $border_width; $i++) {
643                                 ImageLine($gd_border_canvas,             floor($offset_x / 2) + $radius_x,                      $i, $output_width - $radius_x - ceil($offset_x / 2),                         $i, $color_border); // top
644                                 ImageLine($gd_border_canvas,             floor($offset_x / 2) + $radius_x, $output_height - 1 - $i, $output_width - $radius_x - ceil($offset_x / 2),    $output_height - 1 - $i, $color_border); // bottom
645                                 ImageLine($gd_border_canvas,                    floor($offset_x / 2) + $i,               $radius_y,                      floor($offset_x / 2) +  $i, $output_height - $radius_y, $color_border); // left
646                                 ImageLine($gd_border_canvas, $output_width - 1 - $i - ceil($offset_x / 2),               $radius_y,    $output_width - 1 - $i - ceil($offset_x / 2), $output_height - $radius_y, $color_border); // right
647                         }
648
649                         if ($radius_x && $radius_y) {
650
651                                 // PHP bug: ImageArc() with thicknesses > 1 give bad/undesirable/unpredicatable results
652                                 // Solution: Draw multiple 1px arcs side-by-side.
653
654                                 // Problem: parallel arcs give strange/ugly antialiasing problems
655                                 // Solution: draw non-parallel arcs, from one side of the line thickness at the start angle
656                                 //   to the opposite edge of the line thickness at the terminating angle
657                                 for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
658                                         ImageArc($gd_border_canvas, floor($offset_x / 2) + 1 +                 $radius_x,              $thickness_offset - 1 + $radius_y, $radius_x * 2, $radius_y * 2, 180, 270, $color_border); // top-left
659                                         ImageArc($gd_border_canvas,                     $output_width - $radius_x - 1 - ceil($offset_x / 2),              $thickness_offset - 1 + $radius_y, $radius_x * 2, $radius_y * 2, 270, 360, $color_border); // top-right
660                                         ImageArc($gd_border_canvas,                     $output_width - $radius_x - 1 - ceil($offset_x / 2), $output_height - $thickness_offset - $radius_y, $radius_x * 2, $radius_y * 2,   0,  90, $color_border); // bottom-right
661                                         ImageArc($gd_border_canvas, floor($offset_x / 2) + 1 +                 $radius_x, $output_height - $thickness_offset - $radius_y, $radius_x * 2, $radius_y * 2,  90, 180, $color_border); // bottom-left
662                                 }
663                                 if ($border_width > 1) {
664                                         for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
665                                                 ImageArc($gd_border_canvas, floor($offset_x / 2) + $thickness_offset + $radius_x,                                      $radius_y, $radius_x * 2, $radius_y * 2, 180, 270, $color_border); // top-left
666                                                 ImageArc($gd_border_canvas, $output_width - $thickness_offset - $radius_x - 1 - ceil($offset_x / 2),                                      $radius_y, $radius_x * 2, $radius_y * 2, 270, 360, $color_border); // top-right
667                                                 ImageArc($gd_border_canvas, $output_width - $thickness_offset - $radius_x - 1 - ceil($offset_x / 2),                     $output_height - $radius_y, $radius_x * 2, $radius_y * 2,   0,  90, $color_border); // bottom-right
668                                                 ImageArc($gd_border_canvas, floor($offset_x / 2) + $thickness_offset + $radius_x,                     $output_height - $radius_y, $radius_x * 2, $radius_y * 2,  90, 180, $color_border); // bottom-left
669                                         }
670                                 }
671
672                         }
673                         phpthumb_functions::ImageResizeFunction($gd_border_canvas, $gdimg, floor(($output_width - $new_width) / 2), round(($output_height - $new_height) / 2), 0, 0, $new_width, $new_height, $output_width, $output_height);
674
675                         ImageDestroy($gdimg);
676                         $gdimg = phpthumb_functions::ImageCreateFunction($output_width, $output_height);
677                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
678                                 ImageSaveAlpha($gdimg, true);
679                         }
680                         ImageAlphaBlending($gdimg, false);
681                         $gdimg_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 255, 255, 255, 127);
682                         ImageFilledRectangle($gdimg, 0, 0, $output_width, $output_height, $gdimg_color_background);
683
684                         ImageCopy($gdimg, $gd_border_canvas, 0, 0, 0, 0, $output_width, $output_height);
685                         //$gdimg = $gd_border_canvas;
686                         ImageDestroy($gd_border_canvas);
687                         return true;
688
689
690                 } else {
691                         $this->DebugMessage('FAILED: $gd_border_canvas = phpthumb_functions::ImageCreateFunction('.$output_width.', '.$output_height.')', __FILE__, __LINE__);
692                 }
693                 return false;
694         }
695
696
697         function MeanRemoval(&$gdimg) {
698                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
699                         if (ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)) {
700                                 return true;
701                         }
702                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)', __FILE__, __LINE__);
703                         // fall through and try it the hard way
704                 }
705                 // currently not implemented "the hard way"
706                 $this->DebugMessage('FAILED: phpthumb_filters::MeanRemoval($gdimg) [function not implemented]', __FILE__, __LINE__);
707                 return false;
708         }
709
710
711         function Negative(&$gdimg) {
712                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
713                         if (ImageFilter($gdimg, IMG_FILTER_NEGATE)) {
714                                 return true;
715                         }
716                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_NEGATE)', __FILE__, __LINE__);
717                         // fall through and try it the hard way
718                 }
719                 $ImageSX = ImageSX($gdimg);
720                 $ImageSY = ImageSY($gdimg);
721                 for ($x = 0; $x < $ImageSX; $x++) {
722                         for ($y = 0; $y < $ImageSY; $y++) {
723                                 $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
724                                 $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, (~$currentPixel['red'] & 0xFF), (~$currentPixel['green'] & 0xFF), (~$currentPixel['blue'] & 0xFF), $currentPixel['alpha']);
725                                 ImageSetPixel($gdimg, $x, $y, $newColor);
726                         }
727                 }
728                 return true;
729         }
730
731
732         function RoundedImageCorners(&$gdimg, $radius_x, $radius_y) {
733                 // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
734                 // mask is generated as a white double-size elipse on a triple-size black background and copy-paste-resampled
735                 // onto a correct-size mask image as 4 corners due to errors when the entire mask is resampled at once (gray edges)
736                 if ($gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction($radius_x * 6, $radius_y * 6)) {
737                         if ($gdimg_cornermask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
738
739                                 $color_transparent = ImageColorAllocate($gdimg_cornermask_triple, 255, 255, 255);
740                                 ImageFilledEllipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent);
741
742                                 ImageFilledRectangle($gdimg_cornermask, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $color_transparent);
743
744                                 ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple,                           0,                           0,     $radius_x,     $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
745                                 ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple,                           0, ImageSY($gdimg) - $radius_y,     $radius_x, $radius_y * 3, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
746                                 ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, ImageSX($gdimg) - $radius_x, ImageSY($gdimg) - $radius_y, $radius_x * 3, $radius_y * 3, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
747                                 ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, ImageSX($gdimg) - $radius_x,                           0, $radius_x * 3,     $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
748
749                                 phpthumb_filters::ApplyMask($gdimg_cornermask, $gdimg);
750                                 ImageDestroy($gdimg_cornermask);
751                                 $this->DebugMessage('RoundedImageCorners('.$radius_x.', '.$radius_y.') succeeded', __FILE__, __LINE__);
752                                 return true;
753
754                         } else {
755                                 $this->DebugMessage('FAILED: $gdimg_cornermask = phpthumb_functions::ImageCreateFunction('.ImageSX($gdimg).', '.ImageSY($gdimg).')', __FILE__, __LINE__);
756                         }
757                         ImageDestroy($gdimg_cornermask_triple);
758
759                 } else {
760                         $this->DebugMessage('FAILED: $gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction('.($radius_x * 6).', '.($radius_y * 6).')', __FILE__, __LINE__);
761                 }
762                 return false;
763         }
764
765
766         function Saturation(&$gdimg, $amount, $color='') {
767                 if ($amount == 0) {
768                         return true;
769                 } elseif ($amount > 0) {
770                         $amount = 0 - $amount;
771                 } else {
772                         $amount = abs($amount);
773                 }
774                 return phpthumb_filters::Desaturate($gdimg, $amount, $color);
775         }
776
777
778         function Sepia(&$gdimg, $amount, $targetColor) {
779                 $amount      = (is_numeric($amount) ? max(0, min(100, $amount)) : 50);
780                 $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'A28065');
781
782                 if ($amount == 0) {
783                         return true;
784                 }
785
786                 $TargetPixel['red']   = hexdec(substr($targetColor, 0, 2));
787                 $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
788                 $TargetPixel['blue']  = hexdec(substr($targetColor, 4, 2));
789
790                 $ImageSX = ImageSX($gdimg);
791                 $ImageSY = ImageSY($gdimg);
792                 for ($x = 0; $x < $ImageSX; $x++) {
793                         for ($y = 0; $y < $ImageSY; $y++) {
794                                 $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
795                                 $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
796
797                                 // http://www.gimpguru.org/Tutorials/SepiaToning/
798                                 // "In the traditional sepia toning process, the tinting occurs most in
799                                 // the mid-tones: the lighter and darker areas appear to be closer to B&W."
800                                 $SepiaAmount = ((128 - abs($GrayPixel['red'] - 128)) / 128) * ($amount / 100);
801
802                                 foreach ($TargetPixel as $key => $value) {
803                                         $NewPixel[$key] = round(max(0, min(255, $GrayPixel[$key] * (1 - $SepiaAmount) + ($TargetPixel[$key] * $SepiaAmount))));
804                                 }
805                                 $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
806                                 ImageSetPixel($gdimg, $x, $y, $newColor);
807                         }
808                 }
809                 return true;
810         }
811
812
813         function Smooth(&$gdimg, $amount=6) {
814                 $amount = min(25, max(0, $amount));
815                 if ($amount == 0) {
816                         return true;
817                 }
818                 if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
819                         if (ImageFilter($gdimg, IMG_FILTER_SMOOTH, $amount)) {
820                                 return true;
821                         }
822                         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SMOOTH, '.$amount.')', __FILE__, __LINE__);
823                         // fall through and try it the hard way
824                 }
825                 // currently not implemented "the hard way"
826                 $this->DebugMessage('FAILED: phpthumb_filters::Smooth($gdimg, '.$amount.') [function not implemented]', __FILE__, __LINE__);
827                 return false;
828         }
829
830
831         function Threshold(&$gdimg, $cutoff) {
832                 $cutoff = min(255, max(0, ($cutoff ? $cutoff : 128)));
833                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
834                         for ($y = 0; $y < ImageSY($gdimg); $y++) {
835                                 $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
836                                 $grayPixel = phpthumb_functions::GrayscalePixel($currentPixel);
837                                 if ($grayPixel['red'] < $cutoff) {
838                                         $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0x00, 0x00, 0x00, $currentPixel['alpha']);
839                                 } else {
840                                         $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0xFF, 0xFF, 0xFF, $currentPixel['alpha']);
841                                 }
842                                 ImageSetPixel($gdimg, $x, $y, $newColor);
843                         }
844                 }
845                 return true;
846         }
847
848
849         function ImageTrueColorToPalette2(&$image, $dither, $ncolors) {
850                 // http://www.php.net/manual/en/function.imagetruecolortopalette.php
851                 // zmorris at zsculpt dot com (17-Aug-2004 06:58)
852                 $width  = ImageSX($image);
853                 $height = ImageSY($image);
854                 $image_copy = ImageCreateTrueColor($width, $height);
855                 //ImageCopyMerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100);
856                 ImageCopy($image_copy, $image, 0, 0, 0, 0, $width, $height);
857                 ImageTrueColorToPalette($image, $dither, $ncolors);
858                 ImageColorMatch($image_copy, $image);
859                 ImageDestroy($image_copy);
860                 return true;
861         }
862
863         function ReduceColorDepth(&$gdimg, $colors=256, $dither=true) {
864                 $colors = max(min($colors, 256), 2);
865                 // ImageTrueColorToPalette usually makes ugly colors, the replacement is a bit better
866                 //ImageTrueColorToPalette($gdimg, $dither, $colors);
867                 phpthumb_filters::ImageTrueColorToPalette2($gdimg, $dither, $colors);
868                 return true;
869         }
870
871
872         function WhiteBalance(&$gdimg, $targetColor='') {
873                 if (phpthumb_functions::IsHexColor($targetColor)) {
874                         $targetPixel = array(
875                                 'red'   => hexdec(substr($targetColor, 0, 2)),
876                                 'green' => hexdec(substr($targetColor, 2, 2)),
877                                 'blue'  => hexdec(substr($targetColor, 4, 2))
878                         );
879                 } else {
880                         $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, false);
881                         $targetPixel = array(
882                                 'red'   => max(array_keys($Analysis['red'])),
883                                 'green' => max(array_keys($Analysis['green'])),
884                                 'blue'  => max(array_keys($Analysis['blue']))
885                         );
886                 }
887                 $grayValue = phpthumb_functions::GrayscaleValue($targetPixel['red'], $targetPixel['green'], $targetPixel['blue']);
888                 $scaleR = $grayValue / $targetPixel['red'];
889                 $scaleG = $grayValue / $targetPixel['green'];
890                 $scaleB = $grayValue / $targetPixel['blue'];
891
892                 for ($x = 0; $x < ImageSX($gdimg); $x++) {
893                         for ($y = 0; $y < ImageSY($gdimg); $y++) {
894                                 $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
895                                 $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe(
896                                         $gdimg,
897                                         max(0, min(255, round($currentPixel['red']   * $scaleR))),
898                                         max(0, min(255, round($currentPixel['green'] * $scaleG))),
899                                         max(0, min(255, round($currentPixel['blue']  * $scaleB))),
900                                         $currentPixel['alpha']
901                                 );
902                                 ImageSetPixel($gdimg, $x, $y, $newColor);
903                         }
904                 }
905                 return true;
906         }
907
908
909         function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color='000000', $ttffont='', $opacity=100, $margin=5, $angle=0) {
910                 // text watermark requested
911                 if (!$text) {
912                         return false;
913                 }
914                 ImageAlphaBlending($gdimg, true);
915
916                 $text = str_replace("\r\n", "\n", $text);
917                 $text = str_replace("\r",   "\n", $text);
918                 $textlines = explode("\n", $text);
919
920                 if (@is_readable($ttffont) && is_file($ttffont)) {
921
922                         $opacity = 100 - intval(max(min($opacity, 100), 0));
923
924                         $this->DebugMessage('Using TTF font "'.$ttffont.'"', __FILE__, __LINE__);
925
926                         $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
927
928                         $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
929                         $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
930                         //$text_width = round($max_x - $min_x + ($size * 0.5));
931                         $text_width = round($max_x - $min_x);
932
933                         $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
934                         $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
935                         //$text_height = round($max_y - $min_y + ($size * 0.5));
936                         $text_height = round($max_y - $min_y);
937
938                         $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'pH');
939                         $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
940                         $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
941                         $char_height = round($char_max_y - $char_min_y);
942
943                         switch ($alignment) {
944                                 case 'T':
945                                         $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
946                                         $text_origin_y = $char_height + $margin;
947                                         break;
948
949                                 case 'B':
950                                         $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
951                                         $text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
952                                         break;
953
954                                 case 'L':
955                                         $text_origin_x = $margin;
956                                         $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
957                                         break;
958
959                                 case 'R':
960                                         $text_origin_x = ImageSX($gdimg) - $text_width  + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
961                                         $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
962                                         break;
963
964                                 case 'C':
965                                         $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
966                                         $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
967                                         break;
968
969                                 case 'TL':
970                                         $text_origin_x = $margin;
971                                         $text_origin_y = $char_height + $margin;
972                                         break;
973
974                                 case 'TR':
975                                         $text_origin_x = ImageSX($gdimg) - $text_width  + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
976                                         $text_origin_y = $char_height + $margin;
977                                         break;
978
979                                 case 'BL':
980                                         $text_origin_x = $margin;
981                                         $text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
982                                         break;
983
984                                 case 'BR':
985                                 default:
986                                         $text_origin_x = ImageSX($gdimg) - $text_width  + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
987                                         $text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
988                                         break;
989                         }
990                         $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
991
992                         if ($alignment == '*') {
993
994                                 $text_origin_y = $char_height + $margin;
995                                 while (($text_origin_y - $text_height) < ImageSY($gdimg)) {
996                                         $text_origin_x = $margin;
997                                         while ($text_origin_x < ImageSX($gdimg)) {
998                                                 ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
999                                                 $text_origin_x += ($text_width + $margin);
1000                                         }
1001                                         $text_origin_y += ($text_height + $margin);
1002                                 }
1003
1004                         } else {
1005
1006                                 //ImageRectangle($gdimg, $text_origin_x + $min_x, $text_origin_y + $TTFbox[1], $text_origin_x + $min_x + $text_width, $text_origin_y + $TTFbox[1] - $text_height, $letter_color_text);
1007                                 ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
1008
1009                         }
1010
1011                         return true;
1012
1013                         //if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
1014                         //      if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
1015                         //              $letter_color_background      = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark,      127, 127, 127, 127);
1016                         //              $letter_color_background_mask = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark_mask, 127, 127, 127, 127);
1017                         //              ImageFilledRectangle($img_watermark,      0, 0, ImageSX($img_watermark),      ImageSY($img_watermark),      $letter_color_background);
1018                         //              ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $letter_color_background_mask);
1019             //
1020                         //              $letter_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
1021                         //              $letter_color_watermark_mask = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
1022             //
1023                         //              ImageTTFtext($img_watermark,      $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y,  $letter_color_watermark,      $ttffont, $text);
1024                         //              ImageTTFtext($img_watermark_mask, $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y, -$letter_color_watermark_mask, $ttffont, $text);
1025             //
1026                         //              $this->ApplyMask($img_watermark_mask, $img_watermark);
1027                         //              $this->WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
1028             //
1029                         //              ImageDestroy($img_watermark);
1030                         //              ImageDestroy($img_watermark_mask);
1031                         //              return true;
1032                         //      }
1033                         //}
1034
1035                 } else {
1036
1037                         $size = min(5, max(1, $size));
1038                         $this->DebugMessage('Using built-in font (size='.$size.') for text watermark'.($ttffont ? ' because $ttffont !is_readable('.$ttffont.')' : ''), __FILE__, __LINE__);
1039
1040                         $text_width  = 0;
1041                         $text_height = 0;
1042                         foreach ($textlines as $line) {
1043                                 $text_width   = max($text_width, ImageFontWidth($size) * strlen($line));
1044                                 $text_height += ImageFontHeight($size);
1045                         }
1046                         if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
1047                                 ImageAlphaBlending($img_watermark, false);
1048                                 $text_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark, 255, 255, 255, 127);
1049                                 ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
1050
1051                                 if ($angle) {
1052                                         if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
1053                                                 $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
1054                                                 ImageAlphaBlending($img_watermark_mask, false);
1055                                                 ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
1056                                                 $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
1057                                         }
1058                                 }
1059
1060                                 $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
1061                                 foreach ($textlines as $key => $line) {
1062                                         switch ($alignment) {
1063                                                 case 'C':
1064                                                 case 'T':
1065                                                 case 'B':
1066                                                         $x_offset = round(($text_width - (ImageFontWidth($size) * strlen($line))) / 2);
1067                                                         break;
1068
1069                                                 case 'L':
1070                                                 case 'TL':
1071                                                 case 'BL':
1072                                                         $x_offset = 0;
1073                                                         break;
1074
1075                                                 case 'R':
1076                                                 case 'TR':
1077                                                 case 'BR':
1078                                                 default:
1079                                                         $x_offset = $text_width - (ImageFontWidth($size) * strlen($line));
1080                                                         break;
1081                                         }
1082                                         ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
1083                                         if ($angle && $img_watermark_mask) {
1084                                                 ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
1085                                         }
1086                                 }
1087                                 if ($angle && $img_watermark_mask) {
1088                                         $img_watermark      = ImageRotate($img_watermark,      $angle, $text_color_background);
1089                                         $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
1090                                         phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
1091                                 }
1092                                 phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
1093                                 ImageDestroy($img_watermark);
1094                                 return true;
1095                         }
1096
1097                 }
1098
1099                 return false;
1100         }
1101
1102
1103         function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment='*', $opacity=50, $margin=5) {
1104                 if (is_resource($gdimg_dest) && is_resource($img_watermark)) {
1105                         $watermark_source_x        = 0;
1106                         $watermark_source_y        = 0;
1107                         $img_source_width          = ImageSX($gdimg_dest);
1108                         $img_source_height         = ImageSY($gdimg_dest);
1109                         $watermark_source_width    = ImageSX($img_watermark);
1110                         $watermark_source_height   = ImageSY($img_watermark);
1111                         $watermark_opacity_percent = max(0, min(100, $opacity));
1112                         if ($margin < 1) {
1113                                 $watermark_margin_percent = 1 - $margin;
1114                         } else {
1115                                 $watermark_margin_percent = (100 - max(0, min(100, $margin))) / 100;
1116                         }
1117                         $watermark_margin_x = round((1 - $watermark_margin_percent) * $img_source_width);
1118                         $watermark_margin_y = round((1 - $watermark_margin_percent) * $img_source_height);
1119                         switch ($alignment) {
1120                                 case '*':
1121                                         if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) {
1122
1123                                                 ImageAlphaBlending($gdimg_tiledwatermark, false);
1124                                                 if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
1125                                                         ImageSaveAlpha($gdimg_tiledwatermark, true);
1126                                                 }
1127                                                 $text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127);
1128                                                 ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent);
1129
1130                                                 // set the tiled image transparent color to whatever the untiled image transparency index is
1131 //                                              ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark));
1132
1133                                                 // a "cleaner" way of doing it, but can't handle the margin feature :(
1134 //                                              ImageSetTile($gdimg_tiledwatermark, $img_watermark);
1135 //                                              ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED);
1136 //                                              break;
1137
1138 //                                              ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark));
1139                                                 // tile the image as many times as can fit
1140                                                 for ($x = $watermark_margin_x; $x < ($img_source_width + $watermark_source_width); $x += round($watermark_source_width + ((1 - $watermark_margin_percent) * $img_source_width))) {
1141                                                         for ($y = $watermark_margin_y; $y < ($img_source_height + $watermark_source_height); $y += round($watermark_source_height + ((1 - $watermark_margin_percent) * $img_source_height))) {
1142                                                                 ImageCopy(
1143                                                                         $gdimg_tiledwatermark,
1144                                                                         $img_watermark,
1145                                                                         $x,
1146                                                                         $y,
1147                                                                         0,
1148                                                                         0,
1149                                                                         min($watermark_source_width,  $img_source_width  - $x - ((1 - $watermark_margin_percent) * $img_source_width)),
1150                                                                         min($watermark_source_height, $img_source_height - $y - ((1 - $watermark_margin_percent) * $img_source_height))
1151                                                                 );
1152                                                         }
1153                                                 }
1154
1155                                                 $watermark_source_width  = ImageSX($gdimg_tiledwatermark);
1156                                                 $watermark_source_height = ImageSY($gdimg_tiledwatermark);
1157                                                 $watermark_destination_x = 0;
1158                                                 $watermark_destination_y = 0;
1159
1160                                                 ImageDestroy($img_watermark);
1161                                                 $img_watermark = $gdimg_tiledwatermark;
1162                                         }
1163                                         break;
1164
1165                                 case 'T':
1166                                         $watermark_destination_x = round((($img_source_width  / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
1167                                         $watermark_destination_y = $watermark_margin_y;
1168                                         break;
1169
1170                                 case 'B':
1171                                         $watermark_destination_x = round((($img_source_width  / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
1172                                         $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
1173                                         break;
1174
1175                                 case 'L':
1176                                         $watermark_destination_x = $watermark_margin_x;
1177                                         $watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
1178                                         break;
1179
1180                                 case 'R':
1181                                         $watermark_destination_x = round(($img_source_width - $watermark_source_width)  * $watermark_margin_percent);
1182                                         $watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
1183                                         break;
1184
1185                                 case 'C':
1186                                         $watermark_destination_x = round(($img_source_width  / 2) - ($watermark_source_width  / 2));
1187                                         $watermark_destination_y = round(($img_source_height / 2) - ($watermark_source_height / 2));
1188                                         break;
1189
1190                                 case 'TL':
1191                                         $watermark_destination_x = $watermark_margin_x;
1192                                         $watermark_destination_y = $watermark_margin_y;
1193                                         break;
1194
1195                                 case 'TR':
1196                                         $watermark_destination_x = round(($img_source_width - $watermark_source_width)  * $watermark_margin_percent);
1197                                         $watermark_destination_y = $watermark_margin_y;
1198                                         break;
1199
1200                                 case 'BL':
1201                                         $watermark_destination_x = $watermark_margin_x;
1202                                         $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
1203                                         break;
1204
1205                                 case 'BR':
1206                                 default:
1207                                         $watermark_destination_x = round(($img_source_width  - $watermark_source_width)  * $watermark_margin_percent);
1208                                         $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
1209                                         break;
1210                         }
1211                         ImageAlphaBlending($gdimg_dest, false);
1212                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
1213                                 ImageSaveAlpha($gdimg_dest, true);
1214                                 ImageSaveAlpha($img_watermark, true);
1215                         }
1216                         phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent);
1217
1218                         return true;
1219                 }
1220                 return false;
1221         }
1222
1223
1224 }
1225
1226 ?>