root/afridex/plugins/Flutter/phpthumb.filters.php

Revision 21, 59.2 kB (checked in by admin, 18 years ago)
Line 
1<?php
2//////////////////////////////////////////////////////////////
3///  phpThumb() by James Heinrich <info@silisoftware.com>   //
4//        available at http://phpthumb.sourceforge.net     ///
5//////////////////////////////////////////////////////////////
6///                                                         //
7// phpthumb.filters.php - image processing filter functions //
8//                                                         ///
9//////////////////////////////////////////////////////////////
10
11class 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='*', $method=0, $threshold=0.1) {
527                // equivalent of "Auto Contrast" in Adobe Photoshop
528                // method 0 stretches according to RGB colors. Gives a more conservative stretch.
529                // method 1 band stretches according to grayscale which is color-biased (59% green, 30% red, 11% blue). May give a punchier / more aggressive stretch, possibly appearing over-saturated
530                $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
531                $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>(($method == 0) ? 'all' : 'gray'));
532                $band = substr($band, 0, 1);
533                if (!isset($keys[$band])) {
534                        return false;
535                }
536                $key = $keys[$band];
537
538                // If the absolute brightest and darkest pixels are used then one random
539                // pixel in the image could throw off the whole system. Instead, count up/down
540                // from the limit and allow <threshold> (default = 0.1%) of brightest/darkest
541                // pixels to be clipped to min/max
542                $threshold = floatval($threshold) / 100;
543                $clip_threshold = ImageSX($gdimg) * ImageSX($gdimg) * $threshold;
544                //if ($min >= 0) {
545                //      $range_min = min($min, 255);
546                //} else {
547                        $countsum = 0;
548                        for ($i = 0; $i <= 255; $i++) {
549                                if ($method == 0) {
550                                        $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
551                                } else {
552                                        $countsum += @$Analysis[$key][$i];
553                                }
554                                if ($countsum >= $clip_threshold) {
555                                        $range_min = $i - 1;
556                                        break;
557                                }
558                        }
559                        $range_min = max($range_min, 0);
560                //}
561                //if ($max > 0) {
562                //      $range_max = max($max, 255);
563                //} else {
564                        $countsum = 0;
565                        for ($i = 255; $i >= 0; $i--) {
566                                if ($method == 0) {
567                                        $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
568                                } else {
569                                        $countsum += @$Analysis[$key][$i];
570                                }
571                                if ($countsum >= $clip_threshold) {
572                                        $range_max = $i + 1;
573                                        break;
574                                }
575                        }
576                        $range_max = min($range_max, 255);
577                //}
578                $range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min)));
579                if (($range_min == 0) && ($range_max == 255)) {
580                        // no adjustment neccesary - don't waste CPU time!
581                        return true;
582                }
583
584                $ImageSX = ImageSX($gdimg);
585                $ImageSY = ImageSY($gdimg);
586                for ($x = 0; $x < $ImageSX; $x++) {
587                        for ($y = 0; $y < $ImageSY; $y++) {
588                                $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
589                                if ($band == '*') {
590                                        $new['red']   = min(255, max(0, ($OriginalPixel['red']   - $range_min) * $range_scale));
591                                        $new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale));
592                                        $new['blue']  = min(255, max(0, ($OriginalPixel['blue']  - $range_min) * $range_scale));
593                                        $new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale));
594                                } else {
595                                        $new = $OriginalPixel;
596                                        $new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale));
597                                }
598                                $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']);
599                                ImageSetPixel($gdimg, $x, $y, $newColor);
600                        }
601                }
602
603                return true;
604        }
605
606
607        function HistogramOverlay(&$gdimg, $bands='*', $colors='', $width=0.25, $height=0.25, $alignment='BR', $opacity=50, $margin_x=5, $margin_y=null) {
608                $margin_y = (is_null($margin_y) ? $margin_x : $margin_y);
609
610                $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
611                $histW = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
612                $histH = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
613                if ($gdHist = ImageCreateTrueColor($histW, $histH)) {
614                        $color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127);
615                        ImageFilledRectangle($gdHist, 0, 0, $histW, $histH, $color_back);
616                        ImageAlphaBlending($gdHist, false);
617                        ImageSaveAlpha($gdHist, true);
618
619                        $HistogramTempWidth  = 256;
620                        $HistogramTempHeight = 100;
621                        if ($gdHistTemp = ImageCreateTrueColor($HistogramTempWidth, $HistogramTempHeight)) {
622                                $color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127);
623                                ImageAlphaBlending($gdHistTemp, false);
624                                ImageSaveAlpha($gdHistTemp, true);
625                                ImageFilledRectangle($gdHistTemp, 0, 0, ImageSX($gdHistTemp), ImageSY($gdHistTemp), $color_back_temp);
626
627                                $DefaultColors = array('r'=>'FF0000', 'g'=>'00FF00', 'b'=>'0000FF', 'a'=>'999999', '*'=>'FFFFFF');
628                                $Colors = explode(';', $colors);
629                                $BandsToGraph = array_unique(preg_split('//', $bands));
630                                $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
631                                foreach ($BandsToGraph as $key => $band) {
632                                        if (!isset($keys[$band])) {
633                                                continue;
634                                        }
635                                        $PeakValue = max($Analysis[$keys[$band]]);
636                                        $thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]);
637                                        for ($x = 0; $x < $HistogramTempWidth; $x++) {
638                                                ImageLine($gdHistTemp, $x, $HistogramTempHeight - 1, $x, $HistogramTempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $HistogramTempHeight), $thisColor);
639                                        }
640                                        ImageLine($gdHistTemp, 0, $HistogramTempHeight - 1, $HistogramTempWidth - 1, $HistogramTempHeight - 1, $thisColor);
641                                        ImageLine($gdHistTemp, 0, $HistogramTempHeight - 2, $HistogramTempWidth - 1, $HistogramTempHeight - 2, $thisColor);
642                                }
643                                ImageCopyResampled($gdHist, $gdHistTemp, 0, 0, 0, 0, ImageSX($gdHist), ImageSY($gdHist), ImageSX($gdHistTemp), ImageSY($gdHistTemp));
644                                ImageDestroy($gdHistTemp);
645                        } else {
646                                return false;
647                        }
648
649                        phpthumb_filters::WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin_x, $margin_y);
650                        ImageDestroy($gdHist);
651                        return true;
652                }
653                return false;
654        }
655
656
657        function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) {
658                $border_width = ($border_width ? $border_width : 1);
659                $radius_x     = ($radius_x     ? $radius_x     : 0);
660                $radius_y     = ($radius_y     ? $radius_y     : 0);
661
662                $output_width  = ImageSX($gdimg);
663                $output_height = ImageSY($gdimg);
664
665                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));
666                $offset_x = ($radius_x ? $output_width  - $new_width  - $radius_x : 0);
667                $offset_y = ($radius_y ? $output_height - $new_height - $radius_y : 0);
668
669//header('Content-Type: image/png');
670//ImagePNG($gdimg);
671//exit;
672                if ($gd_border_canvas = phpthumb_functions::ImageCreateFunction($output_width, $output_height)) {
673
674                        ImageSaveAlpha($gd_border_canvas, true);
675                        ImageAlphaBlending($gd_border_canvas, false);
676                        $color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gd_border_canvas, 255, 255, 255, 127);
677                        ImageFilledRectangle($gd_border_canvas, 0, 0, $output_width, $output_height, $color_background);
678
679                        $color_border = phpthumb_functions::ImageHexColorAllocate($gd_border_canvas, (phpthumb_functions::IsHexColor($hexcolor_border) ? $hexcolor_border : '000000'));
680
681                        for ($i = 0; $i < $border_width; $i++) {
682                                ImageLine($gd_border_canvas,             floor($offset_x / 2) + $radius_x,                      $i, $output_width - $radius_x - ceil($offset_x / 2),                         $i, $color_border); // top
683                                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
684                                ImageLine($gd_border_canvas,                    floor($offset_x / 2) + $i,               $radius_y,                      floor($offset_x / 2) +  $i, $output_height - $radius_y, $color_border); // left
685                                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
686                        }
687
688                        if ($radius_x && $radius_y) {
689
690                                // PHP bug: ImageArc() with thicknesses > 1 give bad/undesirable/unpredicatable results
691                                // Solution: Draw multiple 1px arcs side-by-side.
692
693                                // Problem: parallel arcs give strange/ugly antialiasing problems
694                                // Solution: draw non-parallel arcs, from one side of the line thickness at the start angle
695                                //   to the opposite edge of the line thickness at the terminating angle
696                                for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
697                                        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
698                                        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
699                                        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
700                                        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
701                                }
702                                if ($border_width > 1) {
703                                        for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
704                                                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
705                                                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
706                                                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
707                                                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
708                                        }
709                                }
710
711                        }
712                        $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);
713
714                        ImageDestroy($gdimg);
715                        $gdimg = phpthumb_functions::ImageCreateFunction($output_width, $output_height);
716                        ImageSaveAlpha($gdimg, true);
717                        ImageAlphaBlending($gdimg, false);
718                        $gdimg_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 255, 255, 255, 127);
719                        ImageFilledRectangle($gdimg, 0, 0, $output_width, $output_height, $gdimg_color_background);
720
721                        ImageCopy($gdimg, $gd_border_canvas, 0, 0, 0, 0, $output_width, $output_height);
722                        //$gdimg = $gd_border_canvas;
723                        ImageDestroy($gd_border_canvas);
724                        return true;
725
726
727                } else {
728                        $this->DebugMessage('FAILED: $gd_border_canvas = phpthumb_functions::ImageCreateFunction('.$output_width.', '.$output_height.')', __FILE__, __LINE__);
729                }
730                return false;
731        }
732
733
734        function ImprovedImageRotate(&$gdimg_source, $rotate_angle=0, $config_background_hexcolor='FFFFFF', $bg=null) {
735                while ($rotate_angle < 0) {
736                        $rotate_angle += 360;
737                }
738                $rotate_angle = $rotate_angle % 360;
739                if ($rotate_angle != 0) {
740
741                        $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_source, $config_background_hexcolor);
742
743                        if ((phpthumb_functions::gd_version() >= 2) && !$bg && ($rotate_angle % 90)) {
744
745                                //$this->DebugMessage('Using alpha rotate', __FILE__, __LINE__);
746                                if ($gdimg_rotate_mask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_source), ImageSY($gdimg_source))) {
747
748                                        for ($i = 0; $i <= 255; $i++) {
749                                                $color_mask[$i] = ImageColorAllocate($gdimg_rotate_mask, $i, $i, $i);
750                                        }
751                                        ImageFilledRectangle($gdimg_rotate_mask, 0, 0, ImageSX($gdimg_rotate_mask), ImageSY($gdimg_rotate_mask), $color_mask[255]);
752                                        $imageX = ImageSX($gdimg_source);
753                                        $imageY = ImageSY($gdimg_source);
754                                        for ($x = 0; $x < $imageX; $x++) {
755                                                for ($y = 0; $y < $imageY; $y++) {
756                                                        $pixelcolor = phpthumb_functions::GetPixelColor($gdimg_source, $x, $y);
757                                                        ImageSetPixel($gdimg_rotate_mask, $x, $y, $color_mask[255 - round($pixelcolor['alpha'] * 255 / 127)]);
758                                                }
759                                        }
760                                        $gdimg_rotate_mask  = ImageRotate($gdimg_rotate_mask,  $rotate_angle, $color_mask[0]);
761                                        $gdimg_source = ImageRotate($gdimg_source, $rotate_angle, $background_color);
762
763                                        ImageAlphaBlending($gdimg_source, false);
764                                        ImageSaveAlpha($gdimg_source, true);
765                                        //$this->is_alpha = true;
766                                        $phpThumbFilters = new phpthumb_filters();
767                                        $phpThumbFilters->phpThumbObject = $this;
768                                        $phpThumbFilters->ApplyMask($gdimg_rotate_mask, $gdimg_source);
769
770                                        ImageDestroy($gdimg_rotate_mask);
771
772                                } else {
773                                        //$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
774                                }
775
776                        } else {
777
778                                if (phpthumb_functions::gd_version() < 2) {
779                                        //$this->DebugMessage('Using non-alpha rotate because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
780                                } elseif ($bg) {
781                                        //$this->DebugMessage('Using non-alpha rotate because $this->bg is "'.$bg.'"', __FILE__, __LINE__);
782                                } elseif ($rotate_angle % 90) {
783                                        //$this->DebugMessage('Using non-alpha rotate because ($rotate_angle % 90) = "'.($rotate_angle % 90).'"', __FILE__, __LINE__);
784                                } else {
785                                        //$this->DebugMessage('Using non-alpha rotate because $this->thumbnailFormat is "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
786                                }
787
788                                if (ImageColorTransparent($gdimg_source) >= 0) {
789                                        // ImageRotate() forgets all about an image's transparency and sets the transparent color to black
790                                        // To compensate, flood-fill the transparent color of the source image with the specified background color first
791                                        // then rotate and the colors should match
792
793                                        if (!function_exists('ImageIsTrueColor') || !ImageIsTrueColor($gdimg_source)) {
794                                                // convert paletted image to true-color before rotating to prevent nasty aliasing artifacts
795
796                                                //$this->source_width  = ImageSX($gdimg_source);
797                                                //$this->source_height = ImageSY($gdimg_source);
798                                                $gdimg_newsrc = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_source), ImageSY($gdimg_source));
799                                                $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor);
800                                                ImageFilledRectangle($gdimg_newsrc, 0, 0, ImageSX($gdimg_source), ImageSY($gdimg_source), phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor));
801                                                ImageCopy($gdimg_newsrc, $gdimg_source, 0, 0, 0, 0, ImageSX($gdimg_source), ImageSY($gdimg_source));
802                                                ImageDestroy($gdimg_source);
803                                                unset($gdimg_source);
804                                                $gdimg_source = $gdimg_newsrc;
805                                                unset($gdimg_newsrc);
806
807                                        } else {
808
809                                                ImageColorSet(
810                                                        $gdimg_source,
811                                                        ImageColorTransparent($gdimg_source),
812                                                        hexdec(substr($config_background_hexcolor, 0, 2)),
813                                                        hexdec(substr($config_background_hexcolor, 2, 2)),
814                                                        hexdec(substr($config_background_hexcolor, 4, 2)));
815
816                                                ImageColorTransparent($gdimg_source, -1);
817
818                                        }
819                                }
820
821                                $gdimg_source = ImageRotate($gdimg_source, $rotate_angle, $background_color);
822
823                        }
824                }
825                return true;
826        }
827
828
829        function MeanRemoval(&$gdimg) {
830                if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
831                        if (ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)) {
832                                return true;
833                        }
834                        $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)', __FILE__, __LINE__);
835                        // fall through and try it the hard way
836                }
837                // currently not implemented "the hard way"
838                $this->DebugMessage('FAILED: phpthumb_filters::MeanRemoval($gdimg) [function not implemented]', __FILE__, __LINE__);
839                return false;
840        }
841
842
843        function Negative(&$gdimg) {
844                if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
845                        if (ImageFilter($gdimg, IMG_FILTER_NEGATE)) {
846                                return true;
847                        }
848                        $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_NEGATE)', __FILE__, __LINE__);
849                        // fall through and try it the hard way
850                }
851                $ImageSX = ImageSX($gdimg);
852                $ImageSY = ImageSY($gdimg);
853                for ($x = 0; $x < $ImageSX; $x++) {
854                        for ($y = 0; $y < $ImageSY; $y++) {
855                                $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
856                                $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, (~$currentPixel['red'] & 0xFF), (~$currentPixel['green'] & 0xFF), (~$currentPixel['blue'] & 0xFF), $currentPixel['alpha']);
857                                ImageSetPixel($gdimg, $x, $y, $newColor);
858                        }
859                }
860                return true;
861        }
862
863
864        function RoundedImageCorners(&$gdimg, $radius_x, $radius_y) {
865                // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
866                // mask is generated as a white double-size elipse on a triple-size black background and copy-paste-resampled
867                // onto a correct-size mask image as 4 corners due to errors when the entire mask is resampled at once (gray edges)
868                if ($gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction($radius_x * 6, $radius_y * 6)) {
869                        if ($gdimg_cornermask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
870
871                                $color_transparent = ImageColorAllocate($gdimg_cornermask_triple, 255, 255, 255);
872                                ImageFilledEllipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent);
873
874                                ImageFilledRectangle($gdimg_cornermask, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $color_transparent);
875
876                                ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple,                           0,                           0,     $radius_x,     $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
877                                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);
878                                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);
879                                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);
880
881                                phpthumb_filters::ApplyMask($gdimg_cornermask, $gdimg);
882                                ImageDestroy($gdimg_cornermask);
883                                $this->DebugMessage('RoundedImageCorners('.$radius_x.', '.$radius_y.') succeeded', __FILE__, __LINE__);
884                                return true;
885
886                        } else {
887                                $this->DebugMessage('FAILED: $gdimg_cornermask = phpthumb_functions::ImageCreateFunction('.ImageSX($gdimg).', '.ImageSY($gdimg).')', __FILE__, __LINE__);
888                        }
889                        ImageDestroy($gdimg_cornermask_triple);
890
891                } else {
892                        $this->DebugMessage('FAILED: $gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction('.($radius_x * 6).', '.($radius_y * 6).')', __FILE__, __LINE__);
893                }
894                return false;
895        }
896
897
898        function Saturation(&$gdimg, $amount, $color='') {
899                if ($amount == 0) {
900                        return true;
901                } elseif ($amount > 0) {
902                        $amount = 0 - $amount;
903                } else {
904                        $amount = abs($amount);
905                }
906                return phpthumb_filters::Desaturate($gdimg, $amount, $color);
907        }
908
909
910        function Sepia(&$gdimg, $amount, $targetColor) {
911                $amount      = (is_numeric($amount) ? max(0, min(100, $amount)) : 50);
912                $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'A28065');
913
914                if ($amount == 0) {
915                        return true;
916                }
917
918                $TargetPixel['red']   = hexdec(substr($targetColor, 0, 2));
919                $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
920                $TargetPixel['blue']  = hexdec(substr($targetColor, 4, 2));
921
922                $ImageSX = ImageSX($gdimg);
923                $ImageSY = ImageSY($gdimg);
924                for ($x = 0; $x < $ImageSX; $x++) {
925                        for ($y = 0; $y < $ImageSY; $y++) {
926                                $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
927                                $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
928
929                                // http://www.gimpguru.org/Tutorials/SepiaToning/
930                                // "In the traditional sepia toning process, the tinting occurs most in
931                                // the mid-tones: the lighter and darker areas appear to be closer to B&W."
932                                $SepiaAmount = ((128 - abs($GrayPixel['red'] - 128)) / 128) * ($amount / 100);
933
934                                foreach ($TargetPixel as $key => $value) {
935                                        $NewPixel[$key] = round(max(0, min(255, $GrayPixel[$key] * (1 - $SepiaAmount) + ($TargetPixel[$key] * $SepiaAmount))));
936                                }
937                                $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
938                                ImageSetPixel($gdimg, $x, $y, $newColor);
939                        }
940                }
941                return true;
942        }
943
944
945        function Smooth(&$gdimg, $amount=6) {
946                $amount = min(25, max(0, $amount));
947                if ($amount == 0) {
948                        return true;
949                }
950                if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
951                        if (ImageFilter($gdimg, IMG_FILTER_SMOOTH, $amount)) {
952                                return true;
953                        }
954                        $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SMOOTH, '.$amount.')', __FILE__, __LINE__);
955                        // fall through and try it the hard way
956                }
957                // currently not implemented "the hard way"
958                $this->DebugMessage('FAILED: phpthumb_filters::Smooth($gdimg, '.$amount.') [function not implemented]', __FILE__, __LINE__);
959                return false;
960        }
961
962
963        function Threshold(&$gdimg, $cutoff) {
964                $cutoff = min(255, max(0, ($cutoff ? $cutoff : 128)));
965                for ($x = 0; $x < ImageSX($gdimg); $x++) {
966                        for ($y = 0; $y < ImageSY($gdimg); $y++) {
967                                $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
968                                $grayPixel = phpthumb_functions::GrayscalePixel($currentPixel);
969                                if ($grayPixel['red'] < $cutoff) {
970                                        $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0x00, 0x00, 0x00, $currentPixel['alpha']);
971                                } else {
972                                        $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0xFF, 0xFF, 0xFF, $currentPixel['alpha']);
973                                }
974                                ImageSetPixel($gdimg, $x, $y, $newColor);
975                        }
976                }
977                return true;
978        }
979
980
981        function ImageTrueColorToPalette2(&$image, $dither, $ncolors) {
982                // http://www.php.net/manual/en/function.imagetruecolortopalette.php
983                // zmorris at zsculpt dot com (17-Aug-2004 06:58)
984                $width  = ImageSX($image);
985                $height = ImageSY($image);
986                $image_copy = ImageCreateTrueColor($width, $height);
987                //ImageCopyMerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100);
988                ImageCopy($image_copy, $image, 0, 0, 0, 0, $width, $height);
989                ImageTrueColorToPalette($image, $dither, $ncolors);
990                ImageColorMatch($image_copy, $image);
991                ImageDestroy($image_copy);
992                return true;
993        }
994
995        function ReduceColorDepth(&$gdimg, $colors=256, $dither=true) {
996                $colors = max(min($colors, 256), 2);
997                // ImageTrueColorToPalette usually makes ugly colors, the replacement is a bit better
998                //ImageTrueColorToPalette($gdimg, $dither, $colors);
999                phpthumb_filters::ImageTrueColorToPalette2($gdimg, $dither, $colors);
1000                return true;
1001        }
1002
1003
1004        function WhiteBalance(&$gdimg, $targetColor='') {
1005                if (phpthumb_functions::IsHexColor($targetColor)) {
1006                        $targetPixel = array(
1007                                'red'   => hexdec(substr($targetColor, 0, 2)),
1008                                'green' => hexdec(substr($targetColor, 2, 2)),
1009                                'blue'  => hexdec(substr($targetColor, 4, 2))
1010                        );
1011                } else {
1012                        $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, false);
1013                        $targetPixel = array(
1014                                'red'   => max(array_keys($Analysis['red'])),
1015                                'green' => max(array_keys($Analysis['green'])),
1016                                'blue'  => max(array_keys($Analysis['blue']))
1017                        );
1018                }
1019                $grayValue = phpthumb_functions::GrayscaleValue($targetPixel['red'], $targetPixel['green'], $targetPixel['blue']);
1020                $scaleR = $grayValue / $targetPixel['red'];
1021                $scaleG = $grayValue / $targetPixel['green'];
1022                $scaleB = $grayValue / $targetPixel['blue'];
1023
1024                for ($x = 0; $x < ImageSX($gdimg); $x++) {
1025                        for ($y = 0; $y < ImageSY($gdimg); $y++) {
1026                                $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
1027                                $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe(
1028                                        $gdimg,
1029                                        max(0, min(255, round($currentPixel['red']   * $scaleR))),
1030                                        max(0, min(255, round($currentPixel['green'] * $scaleG))),
1031                                        max(0, min(255, round($currentPixel['blue']  * $scaleB))),
1032                                        $currentPixel['alpha']
1033                                );
1034                                ImageSetPixel($gdimg, $x, $y, $newColor);
1035                        }
1036                }
1037                return true;
1038        }
1039
1040
1041        function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color='000000', $ttffont='', $opacity=100, $margin=5, $angle=0, $bg_color=false, $bg_opacity=0, $fillextend='') {
1042                // text watermark requested
1043                if (!$text) {
1044                        return false;
1045                }
1046                ImageAlphaBlending($gdimg, true);
1047
1048                $metaTextArray = array(
1049                        '^Fb' =>       $this->phpThumbObject->getimagesizeinfo['filesize'],
1050                        '^Fk' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024),
1051                        '^Fm' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576),
1052                        '^X'  => $this->phpThumbObject->getimagesizeinfo[0],
1053                        '^Y'  => $this->phpThumbObject->getimagesizeinfo[1],
1054                        '^x'  => ImageSX($gdimg),
1055                        '^y'  => ImageSY($gdimg),
1056                        '^^'  => '^',
1057                );
1058                $text = strtr($text, $metaTextArray);
1059
1060                $text = str_replace("\r\n", "\n", $text);
1061                $text = str_replace("\r",   "\n", $text);
1062                $textlines = explode("\n", $text);
1063
1064                if (@is_readable($ttffont) && is_file($ttffont)) {
1065
1066                        $opacity = 100 - intval(max(min($opacity, 100), 0));
1067
1068                        $this->DebugMessage('Using TTF font "'.$ttffont.'"', __FILE__, __LINE__);
1069
1070                        $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
1071
1072                        $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
1073                        $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
1074                        //$text_width = round($max_x - $min_x + ($size * 0.5));
1075                        $text_width = round($max_x - $min_x);
1076
1077                        $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
1078                        $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
1079                        //$text_height = round($max_y - $min_y + ($size * 0.5));
1080                        $text_height = round($max_y - $min_y);
1081
1082                        $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'jH');
1083                        $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
1084                        $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
1085                        $char_height = round($char_max_y - $char_min_y);
1086
1087                        switch ($alignment) {
1088                                case 'T':
1089                                        $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
1090                                        $text_origin_y = $char_height + $margin;
1091                                        break;
1092
1093                                case 'B':
1094                                        $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
1095                                        $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin;
1096                                        break;
1097
1098                                case 'L':
1099                                        $text_origin_x = $margin;
1100                                        $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
1101                                        break;
1102
1103                                case 'R':
1104                                        $text_origin_x = ImageSX($gdimg) - $text_width  + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
1105                                        $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
1106                                        break;
1107
1108                                case 'C':
1109                                        $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
1110                                        $text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
1111                                        break;
1112
1113                                case 'TL':
1114                                        $text_origin_x = $margin;
1115                                        $text_origin_y = $char_height + $margin;
1116                                        break;
1117
1118                                case 'TR':
1119                                        $text_origin_x = ImageSX($gdimg) - $text_width  + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
1120                                        $text_origin_y = $char_height + $margin;
1121                                        break;
1122
1123                                case 'BL':
1124                                        $text_origin_x = $margin;
1125                                        $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin;
1126                                        break;
1127
1128                                case 'BR':
1129                                default:
1130                                        $text_origin_x = ImageSX($gdimg) - $text_width  + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
1131                                        $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin;
1132                                        break;
1133                        }
1134                        $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
1135
1136                        if ($alignment == '*') {
1137
1138                                $text_origin_y = $char_height + $margin;
1139                                while (($text_origin_y - $text_height) < ImageSY($gdimg)) {
1140                                        $text_origin_x = $margin;
1141                                        while ($text_origin_x < ImageSX($gdimg)) {
1142                                                ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
1143                                                $text_origin_x += ($text_width + $margin);
1144                                        }
1145                                        $text_origin_y += ($text_height + $margin);
1146                                }
1147
1148                        } else {
1149
1150                                //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);
1151                                if (phpthumb_functions::IsHexColor($bg_color)) {
1152                                        $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
1153                                        $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, $bg_color, false, $text_background_alpha);
1154                                } else {
1155                                        $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, 'FFFFFF', false, 127);
1156                                }
1157                                $x1 = $text_origin_x + $min_x;
1158                                $y1 = $text_origin_y + $TTFbox[1];
1159                                $x2 = $text_origin_x + $min_x + $text_width;
1160                                $y2 = $text_origin_y + $TTFbox[1] - $text_height;
1161                                $x_TL = eregi('x', $fillextend) ?               0 : min($x1, $x2);
1162                                $y_TL = eregi('y', $fillextend) ?               0 : min($y1, $y2);
1163                                $x_BR = eregi('x', $fillextend) ? ImageSX($gdimg) : max($x1, $x2);
1164                                $y_BR = eregi('y', $fillextend) ? ImageSY($gdimg) : max($y1, $y2);
1165                                //while ($y_BR > ImageSY($gdimg)) {
1166                                //      $y_TL--;
1167                                //      $y_BR--;
1168                                //      $text_origin_y--;
1169                                //}
1170                                ImageFilledRectangle($gdimg, $x_TL, $y_TL, $x_BR, $y_BR, $text_color_background);
1171                                ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
1172
1173                        }
1174                        return true;
1175
1176                } else {
1177
1178                        $size = min(5, max(1, $size));
1179                        $this->DebugMessage('Using built-in font (size='.$size.') for text watermark'.($ttffont ? ' because $ttffont !is_readable('.$ttffont.')' : ''), __FILE__, __LINE__);
1180
1181                        $text_width  = 0;
1182                        $text_height = 0;
1183                        foreach ($textlines as $dummy => $line) {
1184                                $text_width   = max($text_width, ImageFontWidth($size) * strlen($line));
1185                                $text_height += ImageFontHeight($size);
1186                        }
1187                        if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
1188                                ImageAlphaBlending($img_watermark, false);
1189                                if (phpthumb_functions::IsHexColor($bg_color)) {
1190                                        $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
1191                                        $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, $bg_color, false, $text_background_alpha);
1192                                } else {
1193                                        $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, 'FFFFFF', false, 127);
1194                                }
1195                                ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
1196
1197                                if ($angle && function_exists('ImageRotate')) {
1198                                        // using $img_watermark_mask is pointless if ImageRotate function isn't available
1199                                        if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
1200                                                $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
1201                                                ImageAlphaBlending($img_watermark_mask, false);
1202                                                ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
1203                                                $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
1204                                        }
1205                                }
1206
1207                                $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
1208                                foreach ($textlines as $key => $line) {
1209                                        switch ($alignment) {
1210                                                case 'C':
1211                                                case 'T':
1212                                                case 'B':
1213                                                        $x_offset = round(($text_width - (ImageFontWidth($size) * strlen($line))) / 2);
1214                                                        break;
1215
1216                                                case 'L':
1217                                                case 'TL':
1218                                                case 'BL':
1219                                                        $x_offset = 0;
1220                                                        break;
1221
1222                                                case 'R':
1223                                                case 'TR':
1224                                                case 'BR':
1225                                                default:
1226                                                        $x_offset = $text_width - (ImageFontWidth($size) * strlen($line));
1227                                                        break;
1228                                        }
1229                                        ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
1230                                        if ($angle && $img_watermark_mask) {
1231                                                ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
1232                                        }
1233                                }
1234                                if ($angle && $img_watermark_mask) {
1235                                        $img_watermark      = ImageRotate($img_watermark,      $angle, $text_color_background);
1236                                        $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
1237                                        phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
1238                                }
1239                                phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
1240                                ImageDestroy($img_watermark);
1241                                return true;
1242                        }
1243
1244                }
1245                return false;
1246        }
1247
1248
1249        function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment='*', $opacity=50, $margin_x=5, $margin_y=null) {
1250                if (is_resource($gdimg_dest) && is_resource($img_watermark)) {
1251                        $watermark_source_x        = 0;
1252                        $watermark_source_y        = 0;
1253                        $img_source_width          = ImageSX($gdimg_dest);
1254                        $img_source_height         = ImageSY($gdimg_dest);
1255                        $watermark_source_width    = ImageSX($img_watermark);
1256                        $watermark_source_height   = ImageSY($img_watermark);
1257                        $watermark_opacity_percent = max(0, min(100, $opacity));
1258                        $margin_y = (is_null($margin_y) ? $margin_x : $margin_y);
1259                        $watermark_margin_x = ((($margin_x > 0) && ($margin_x < 1)) ? round((1 - $margin_x) * $img_source_width)  : $margin_x);
1260                        $watermark_margin_y = ((($margin_y > 0) && ($margin_y < 1)) ? round((1 - $margin_y) * $img_source_height) : $margin_y);
1261                        switch ($alignment) {
1262                                case '*':
1263                                        if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) {
1264
1265                                                ImageAlphaBlending($gdimg_tiledwatermark, false);
1266                                                ImageSaveAlpha($gdimg_tiledwatermark, true);
1267                                                $text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127);
1268                                                ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent);
1269
1270                                                // set the tiled image transparent color to whatever the untiled image transparency index is
1271//                                              ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark));
1272
1273                                                // a "cleaner" way of doing it, but can't handle the margin feature :(
1274//                                              ImageSetTile($gdimg_tiledwatermark, $img_watermark);
1275//                                              ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED);
1276//                                              break;
1277
1278//                                              ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark));
1279                                                // tile the image as many times as can fit
1280                                                for ($x = $watermark_margin_x; $x < ($img_source_width + $watermark_source_width); $x += ($watermark_source_width + $watermark_margin_x)) {
1281                                                        for ($y = $watermark_margin_y; $y < ($img_source_height + $watermark_source_height); $y += ($watermark_source_height + $watermark_margin_y)) {
1282                                                                ImageCopy(
1283                                                                        $gdimg_tiledwatermark,
1284                                                                        $img_watermark,
1285                                                                        $x,
1286                                                                        $y,
1287                                                                        0,
1288                                                                        0,
1289                                                                        min($watermark_source_width,  $img_source_width  - $x - $watermark_margin_x),
1290                                                                        min($watermark_source_height, $img_source_height - $y - $watermark_margin_y)
1291                                                                );
1292                                                        }
1293                                                }
1294
1295                                                $watermark_source_width  = ImageSX($gdimg_tiledwatermark);
1296                                                $watermark_source_height = ImageSY($gdimg_tiledwatermark);
1297                                                $watermark_destination_x = 0;
1298                                                $watermark_destination_y = 0;
1299
1300                                                ImageDestroy($img_watermark);
1301                                                $img_watermark = $gdimg_tiledwatermark;
1302                                        }
1303                                        break;
1304
1305                                case 'T':
1306                                        $watermark_destination_x = round((($img_source_width  / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
1307                                        $watermark_destination_y = $watermark_margin_y;
1308                                        break;
1309
1310                                case 'B':
1311                                        $watermark_destination_x = round((($img_source_width  / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
1312                                        $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y;
1313                                        break;
1314
1315                                case 'L':
1316                                        $watermark_destination_x = $watermark_margin_x;
1317                                        $watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
1318                                        break;
1319
1320                                case 'R':
1321                                        $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x;
1322                                        $watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
1323                                        break;
1324
1325                                case 'C':
1326                                        $watermark_destination_x = round(($img_source_width  / 2) - ($watermark_source_width  / 2));
1327                                        $watermark_destination_y = round(($img_source_height / 2) - ($watermark_source_height / 2));
1328                                        break;
1329
1330                                case 'TL':
1331                                        $watermark_destination_x = $watermark_margin_x;
1332                                        $watermark_destination_y = $watermark_margin_y;
1333                                        break;
1334
1335                                case 'TR':
1336                                        $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x;
1337                                        $watermark_destination_y = $watermark_margin_y;
1338                                        break;
1339
1340                                case 'BL':
1341//echo '<pre>';
1342////var_dump($watermark_destination_x);
1343////var_dump($watermark_destination_y);
1344//var_dump($watermark_margin_x);
1345//var_dump($img_source_height);
1346//var_dump($watermark_source_height);
1347//var_dump($watermark_margin_y);
1348                                        $watermark_destination_x = $watermark_margin_x;
1349                                        $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y;
1350                                        break;
1351
1352                                case 'BR':
1353                                default:
1354                                        $watermark_destination_x = $img_source_width  - $watermark_source_width  - $watermark_margin_x;
1355                                        $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y;
1356                                        break;
1357                        }
1358                        ImageAlphaBlending($gdimg_dest, false);
1359                        ImageSaveAlpha($gdimg_dest, true);
1360                        ImageSaveAlpha($img_watermark, true);
1361                        phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent);
1362
1363                        return true;
1364                }
1365                return false;
1366        }
1367
1368
1369        function DebugMessage($message, $file='', $line='') {
1370                if (is_object($this->phpThumbObject)) {
1371                        return $this->phpThumbObject->DebugMessage($message, $file, $line);
1372                }
1373                return false;
1374        }
1375}
1376
1377?>
Note: See TracBrowser for help on using the browser.