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