Total Complexity | 334 |
Total Lines | 1498 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like phpthumb_filters often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use phpthumb_filters, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class phpthumb_filters |
||
13 | { |
||
14 | /** |
||
15 | * @var phpthumb |
||
16 | */ |
||
17 | |||
18 | public $phpThumbObject = null; |
||
19 | |||
20 | public function DebugMessage($message, $file = '', $line = '') |
||
21 | { |
||
22 | if (is_object($this->phpThumbObject)) { |
||
23 | return $this->phpThumbObject->DebugMessage($message, $file, $line); |
||
24 | } |
||
25 | return false; |
||
26 | } |
||
27 | |||
28 | public function ApplyMask(&$gdimg_mask, &$gdimg_image) |
||
29 | { |
||
30 | if (phpthumb_functions::gd_version() < 2) { |
||
31 | $this->DebugMessage('Skipping ApplyMask() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__); |
||
32 | return false; |
||
33 | } |
||
34 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.2', '>=')) { |
||
35 | $this->DebugMessage('Using alpha ApplyMask() technique', __FILE__, __LINE__); |
||
36 | if ($gdimg_mask_resized = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_image), imagesy($gdimg_image))) { |
||
37 | imagecopyresampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, imagesx($gdimg_image), imagesy($gdimg_image), imagesx($gdimg_mask), imagesy($gdimg_mask)); |
||
38 | if ($gdimg_mask_blendtemp = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_image), imagesy($gdimg_image))) { |
||
39 | $color_background = imagecolorallocate($gdimg_mask_blendtemp, 0, 0, 0); |
||
40 | imagefilledrectangle($gdimg_mask_blendtemp, 0, 0, imagesx($gdimg_mask_blendtemp), imagesy($gdimg_mask_blendtemp), $color_background); |
||
41 | imagealphablending($gdimg_mask_blendtemp, false); |
||
42 | imagesavealpha($gdimg_mask_blendtemp, true); |
||
43 | for ($x = 0, $xMax = imagesx($gdimg_image); $x < $xMax; $x++) { |
||
44 | for ($y = 0, $yMax = imagesy($gdimg_image); $y < $yMax; $y++) { |
||
45 | //$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y); |
||
46 | $RealPixel = phpthumb_functions::GetPixelColor($gdimg_image, $x, $y); |
||
47 | $MaskPixel = phpthumb_functions::GrayscalePixel(phpthumb_functions::GetPixelColor($gdimg_mask_resized, $x, $y)); |
||
48 | $MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127))); |
||
49 | $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha); |
||
50 | imagesetpixel($gdimg_mask_blendtemp, $x, $y, $newcolor); |
||
51 | } |
||
52 | } |
||
53 | imagealphablending($gdimg_image, false); |
||
54 | imagesavealpha($gdimg_image, true); |
||
55 | imagecopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, imagesx($gdimg_mask_blendtemp), imagesy($gdimg_mask_blendtemp)); |
||
56 | imagedestroy($gdimg_mask_blendtemp); |
||
57 | } else { |
||
58 | $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__); |
||
59 | } |
||
60 | imagedestroy($gdimg_mask_resized); |
||
61 | } else { |
||
62 | $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__); |
||
63 | } |
||
64 | } else { |
||
65 | // alpha merging requires PHP v4.3.2+ |
||
66 | $this->DebugMessage('Skipping ApplyMask() technique because PHP is v"' . PHP_VERSION . '"', __FILE__, __LINE__); |
||
67 | } |
||
68 | return true; |
||
69 | } |
||
70 | |||
71 | public function Bevel(&$gdimg, $width, $hexcolor1, $hexcolor2) |
||
72 | { |
||
73 | $width = ($width ? $width : 5); |
||
74 | $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF'); |
||
75 | $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000'); |
||
76 | |||
77 | imagealphablending($gdimg, true); |
||
78 | for ($i = 0; $i < $width; $i++) { |
||
79 | $alpha = round(($i / $width) * 127); |
||
80 | $color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1, false, $alpha); |
||
81 | $color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2, false, $alpha); |
||
82 | |||
83 | imageline($gdimg, $i, $i + 1, $i, imagesy($gdimg) - $i - 1, $color1); // left |
||
84 | imageline($gdimg, $i, $i, imagesx($gdimg) - $i, $i, $color1); // top |
||
85 | imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i - 1, imagesx($gdimg) - $i, $i + 1, $color2); // right |
||
86 | imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i, $i, imagesy($gdimg) - $i, $color2); // bottom |
||
87 | } |
||
88 | return true; |
||
89 | } |
||
90 | |||
91 | public function Blur(&$gdimg, $radius = 0.5) |
||
92 | { |
||
93 | // Taken from Torstein Hønsi's phpUnsharpMask (see phpthumb.unsharp.php) |
||
94 | |||
95 | $radius = round(max(0, min($radius, 50)) * 2); |
||
96 | if (!$radius) { |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | $w = imagesx($gdimg); |
||
101 | $h = imagesy($gdimg); |
||
102 | if ($imgBlur = imagecreatetruecolor($w, $h)) { |
||
103 | // Gaussian blur matrix: |
||
104 | // 1 2 1 |
||
105 | // 2 4 2 |
||
106 | // 1 2 1 |
||
107 | |||
108 | // Move copies of the image around one pixel at the time and merge them with weight |
||
109 | // according to the matrix. The same matrix is simply repeated for higher radii. |
||
110 | for ($i = 0; $i < $radius; $i++) { |
||
111 | imagecopy($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left |
||
112 | imagecopymerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.00000); // down right |
||
113 | imagecopymerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left |
||
|
|||
114 | imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.00000); // up right |
||
115 | imagecopymerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left |
||
116 | imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.00000); // right |
||
117 | imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.00000); // up |
||
118 | imagecopymerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667); // down |
||
119 | imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.000000); // center |
||
120 | imagecopy($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h); |
||
121 | } |
||
122 | return true; |
||
123 | } |
||
124 | return false; |
||
125 | } |
||
126 | |||
127 | public function BlurGaussian(&$gdimg) |
||
128 | { |
||
129 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
130 | if (imagefilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)) { |
||
131 | return true; |
||
132 | } |
||
133 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)', __FILE__, __LINE__); |
||
134 | // fall through and try it the hard way |
||
135 | } |
||
136 | $this->DebugMessage('FAILED: phpthumb_filters::BlurGaussian($gdimg) [using phpthumb_filters::Blur() instead]', __FILE__, __LINE__); |
||
137 | return $this->Blur($gdimg, 0.5); |
||
138 | } |
||
139 | |||
140 | public function BlurSelective(&$gdimg) |
||
141 | { |
||
142 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
143 | if (imagefilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)) { |
||
144 | return true; |
||
145 | } |
||
146 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)', __FILE__, __LINE__); |
||
147 | // fall through and try it the hard way |
||
148 | } |
||
149 | // currently not implemented "the hard way" |
||
150 | $this->DebugMessage('FAILED: phpthumb_filters::BlurSelective($gdimg) [function not implemented]', __FILE__, __LINE__); |
||
151 | return false; |
||
152 | } |
||
153 | |||
154 | public function Brightness(&$gdimg, $amount = 0) |
||
155 | { |
||
156 | if ($amount == 0) { |
||
157 | return true; |
||
158 | } |
||
159 | $amount = max(-255, min(255, $amount)); |
||
160 | |||
161 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
162 | if (imagefilter($gdimg, IMG_FILTER_BRIGHTNESS, $amount)) { |
||
163 | return true; |
||
164 | } |
||
165 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_BRIGHTNESS, ' . $amount . ')', __FILE__, __LINE__); |
||
166 | // fall through and try it the hard way |
||
167 | } |
||
168 | |||
169 | $scaling = (255 - abs($amount)) / 255; |
||
170 | $baseamount = (($amount > 0) ? $amount : 0); |
||
171 | for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) { |
||
172 | for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) { |
||
173 | $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
174 | $NewPixel = []; |
||
175 | foreach ($OriginalPixel as $key => $value) { |
||
176 | $NewPixel[$key] = round($baseamount + ($OriginalPixel[$key] * $scaling)); |
||
177 | } |
||
178 | $newColor = imagecolorallocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']); |
||
179 | imagesetpixel($gdimg, $x, $y, $newColor); |
||
180 | } |
||
181 | } |
||
182 | return true; |
||
183 | } |
||
184 | |||
185 | public function Contrast(&$gdimg, $amount = 0) |
||
186 | { |
||
187 | if ($amount == 0) { |
||
188 | return true; |
||
189 | } |
||
190 | $amount = max(-255, min(255, $amount)); |
||
191 | |||
192 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
193 | // imagefilter(IMG_FILTER_CONTRAST) has range +100 to -100 (positive numbers make it darker!) |
||
194 | $amount = ($amount / 255) * -100; |
||
195 | if (imagefilter($gdimg, IMG_FILTER_CONTRAST, $amount)) { |
||
196 | return true; |
||
197 | } |
||
198 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_CONTRAST, ' . $amount . ')', __FILE__, __LINE__); |
||
199 | // fall through and try it the hard way |
||
200 | } |
||
201 | |||
202 | if ($amount > 0) { |
||
203 | $scaling = 1 + ($amount / 255); |
||
204 | } else { |
||
205 | $scaling = (255 - abs($amount)) / 255; |
||
206 | } |
||
207 | for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) { |
||
208 | for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) { |
||
209 | $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
210 | $NewPixel = []; |
||
211 | foreach ($OriginalPixel as $key => $value) { |
||
212 | $NewPixel[$key] = min(255, max(0, round($OriginalPixel[$key] * $scaling))); |
||
213 | } |
||
214 | $newColor = imagecolorallocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']); |
||
215 | imagesetpixel($gdimg, $x, $y, $newColor); |
||
216 | } |
||
217 | } |
||
218 | return true; |
||
219 | } |
||
220 | |||
221 | public function Colorize(&$gdimg, $amount, $targetColor) |
||
222 | { |
||
223 | $amount = (is_numeric($amount) ? $amount : 25); |
||
224 | $amountPct = $amount / 100; |
||
225 | $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray'); |
||
226 | |||
227 | if ($amount == 0) { |
||
228 | return true; |
||
229 | } |
||
230 | |||
231 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
232 | if ($targetColor == 'gray') { |
||
233 | $targetColor = '808080'; |
||
234 | } |
||
235 | $r = round($amountPct * hexdec(substr($targetColor, 0, 2))); |
||
236 | $g = round($amountPct * hexdec(substr($targetColor, 2, 2))); |
||
237 | $b = round($amountPct * hexdec(substr($targetColor, 4, 2))); |
||
238 | if (imagefilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) { |
||
239 | return true; |
||
240 | } |
||
241 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__); |
||
242 | // fall through and try it the hard way |
||
243 | } |
||
244 | |||
245 | // overridden below for grayscale |
||
246 | $TargetPixel = []; |
||
247 | if ($targetColor != 'gray') { |
||
248 | $TargetPixel['red'] = hexdec(substr($targetColor, 0, 2)); |
||
249 | $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2)); |
||
250 | $TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2)); |
||
251 | } |
||
252 | |||
253 | for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) { |
||
254 | for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) { |
||
255 | $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
256 | if ($targetColor == 'gray') { |
||
257 | $TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel); |
||
258 | } |
||
259 | $NewPixel = []; |
||
260 | foreach ($TargetPixel as $key => $value) { |
||
261 | $NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * $amountPct)))); |
||
262 | } |
||
263 | //$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']); |
||
264 | $newColor = imagecolorallocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']); |
||
265 | imagesetpixel($gdimg, $x, $y, $newColor); |
||
266 | } |
||
267 | } |
||
268 | return true; |
||
269 | } |
||
270 | |||
271 | public function Crop(&$gdimg, $left = 0, $right = 0, $top = 0, $bottom = 0) |
||
272 | { |
||
273 | if (!$left && !$right && !$top && !$bottom) { |
||
274 | return true; |
||
275 | } |
||
276 | $oldW = imagesx($gdimg); |
||
277 | $oldH = imagesy($gdimg); |
||
278 | if (($left > 0) && ($left < 1)) { |
||
279 | $left = round($left * $oldW); |
||
280 | } |
||
281 | if (($right > 0) && ($right < 1)) { |
||
282 | $right = round($right * $oldW); |
||
283 | } |
||
284 | if (($top > 0) && ($top < 1)) { |
||
285 | $top = round($top * $oldH); |
||
286 | } |
||
287 | if (($bottom > 0) && ($bottom < 1)) { |
||
288 | $bottom = round($bottom * $oldH); |
||
289 | } |
||
290 | $right = min($oldW - $left - 1, $right); |
||
291 | $bottom = min($oldH - $top - 1, $bottom); |
||
292 | $newW = $oldW - $left - $right; |
||
293 | $newH = $oldH - $top - $bottom; |
||
294 | |||
295 | if ($imgCropped = imagecreatetruecolor($newW, $newH)) { |
||
296 | imagecopy($imgCropped, $gdimg, 0, 0, $left, $top, $newW, $newH); |
||
297 | if ($gdimg = imagecreatetruecolor($newW, $newH)) { |
||
298 | imagecopy($gdimg, $imgCropped, 0, 0, 0, 0, $newW, $newH); |
||
299 | imagedestroy($imgCropped); |
||
300 | return true; |
||
301 | } |
||
302 | imagedestroy($imgCropped); |
||
303 | } |
||
304 | return false; |
||
305 | } |
||
306 | |||
307 | public function Desaturate(&$gdimg, $amount, $color = '') |
||
308 | { |
||
309 | if ($amount == 0) { |
||
310 | return true; |
||
311 | } |
||
312 | return $this->Colorize($gdimg, $amount, (phpthumb_functions::IsHexColor($color) ? $color : 'gray')); |
||
313 | } |
||
314 | |||
315 | public function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $alpha) |
||
316 | { |
||
317 | if (phpthumb_functions::gd_version() < 2) { |
||
318 | return false; |
||
319 | } |
||
320 | $distance = ($distance ? $distance : 10); |
||
321 | $width = ($width ? $width : 10); |
||
322 | $hexcolor = ($hexcolor ? $hexcolor : '000000'); |
||
323 | $angle = ($angle ? $angle : 225) % 360; |
||
324 | $alpha = max(0, min(100, ($alpha ? $alpha : 100))); |
||
325 | |||
326 | if ($alpha <= 0) { |
||
327 | // invisible shadow, nothing to do |
||
328 | return true; |
||
329 | } |
||
330 | if ($distance <= 0) { |
||
331 | // shadow completely obscured by source image, nothing to do |
||
332 | return true; |
||
333 | } |
||
334 | |||
335 | //$width_shadow = cos(deg2rad($angle)) * ($distance + $width); |
||
336 | //$height_shadow = sin(deg2rad($angle)) * ($distance + $width); |
||
337 | //$scaling = min(imagesx($gdimg) / (imagesx($gdimg) + abs($width_shadow)), imagesy($gdimg) / (imagesy($gdimg) + abs($height_shadow))); |
||
338 | |||
339 | $Offset = []; |
||
340 | for ($i = 0; $i < $width; $i++) { |
||
341 | $WidthAlpha[$i] = (abs(($width / 2) - $i) / $width); |
||
342 | $Offset['x'] = cos(deg2rad($angle)) * ($distance + $i); |
||
343 | $Offset['y'] = sin(deg2rad($angle)) * ($distance + $i); |
||
344 | } |
||
345 | |||
346 | $tempImageWidth = imagesx($gdimg) + abs($Offset['x']); |
||
347 | $tempImageHeight = imagesy($gdimg) + abs($Offset['y']); |
||
348 | |||
349 | if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) { |
||
350 | imagealphablending($gdimg_dropshadow_temp, false); |
||
351 | imagesavealpha($gdimg_dropshadow_temp, true); |
||
352 | $transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127); |
||
353 | imagefill($gdimg_dropshadow_temp, 0, 0, $transparent1); |
||
354 | |||
355 | $PixelMap = []; |
||
356 | for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) { |
||
357 | for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) { |
||
358 | $PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
359 | } |
||
360 | } |
||
361 | for ($x = 0; $x < $tempImageWidth; $x++) { |
||
362 | for ($y = 0; $y < $tempImageHeight; $y++) { |
||
363 | //for ($i = 0; $i < $width; $i++) { |
||
364 | for ($i = 0; $i < 1; $i++) { |
||
365 | if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) { |
||
366 | if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) { |
||
367 | $thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']); |
||
368 | imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor); |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | |||
375 | imagealphablending($gdimg_dropshadow_temp, true); |
||
376 | for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) { |
||
377 | for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) { |
||
378 | if ($PixelMap[$x][$y]['alpha'] < 127) { |
||
379 | $thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']); |
||
380 | imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor); |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | |||
385 | imagesavealpha($gdimg, true); |
||
386 | imagealphablending($gdimg, false); |
||
387 | //$this->is_alpha = true; |
||
388 | $transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127); |
||
389 | imagefilledrectangle($gdimg, 0, 0, imagesx($gdimg), imagesy($gdimg), $transparent2); |
||
390 | imagecopyresampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg), imagesx($gdimg_dropshadow_temp), imagesy($gdimg_dropshadow_temp)); |
||
391 | |||
392 | imagedestroy($gdimg_dropshadow_temp); |
||
393 | } |
||
394 | return true; |
||
395 | } |
||
396 | |||
397 | public function EdgeDetect(&$gdimg) |
||
398 | { |
||
399 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
400 | if (imagefilter($gdimg, IMG_FILTER_EDGEDETECT)) { |
||
401 | return true; |
||
402 | } |
||
403 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_EDGEDETECT)', __FILE__, __LINE__); |
||
404 | // fall through and try it the hard way |
||
405 | } |
||
406 | // currently not implemented "the hard way" |
||
407 | $this->DebugMessage('FAILED: phpthumb_filters::EdgeDetect($gdimg) [function not implemented]', __FILE__, __LINE__); |
||
408 | return false; |
||
409 | } |
||
410 | |||
411 | public function Ellipse($gdimg) |
||
412 | { |
||
413 | if (phpthumb_functions::gd_version() < 2) { |
||
414 | return false; |
||
415 | } |
||
416 | // generate mask at twice desired resolution and downsample afterwards for easy antialiasing |
||
417 | if ($gdimg_ellipsemask_double = phpthumb_functions::ImageCreateFunction(imagesx($gdimg) * 2, imagesy($gdimg) * 2)) { |
||
418 | if ($gdimg_ellipsemask = phpthumb_functions::ImageCreateFunction(imagesx($gdimg), imagesy($gdimg))) { |
||
419 | $color_transparent = imagecolorallocate($gdimg_ellipsemask_double, 255, 255, 255); |
||
420 | imagefilledellipse($gdimg_ellipsemask_double, imagesx($gdimg), imagesy($gdimg), (imagesx($gdimg) - 1) * 2, (imagesy($gdimg) - 1) * 2, $color_transparent); |
||
421 | imagecopyresampled($gdimg_ellipsemask, $gdimg_ellipsemask_double, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg), imagesx($gdimg) * 2, imagesy($gdimg) * 2); |
||
422 | |||
423 | $this->ApplyMask($gdimg_ellipsemask, $gdimg); |
||
424 | imagedestroy($gdimg_ellipsemask); |
||
425 | return true; |
||
426 | } else { |
||
427 | $this->DebugMessage('$gdimg_ellipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__); |
||
428 | } |
||
429 | imagedestroy($gdimg_ellipsemask_double); |
||
430 | } else { |
||
431 | $this->DebugMessage('$gdimg_ellipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__); |
||
432 | } |
||
433 | return false; |
||
434 | } |
||
435 | |||
436 | public function Emboss(&$gdimg) |
||
437 | { |
||
438 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
439 | if (imagefilter($gdimg, IMG_FILTER_EMBOSS)) { |
||
440 | return true; |
||
441 | } |
||
442 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_EMBOSS)', __FILE__, __LINE__); |
||
443 | // fall through and try it the hard way |
||
444 | } |
||
445 | // currently not implemented "the hard way" |
||
446 | $this->DebugMessage('FAILED: phpthumb_filters::Emboss($gdimg) [function not implemented]', __FILE__, __LINE__); |
||
447 | return false; |
||
448 | } |
||
449 | |||
450 | public function Flip(&$gdimg, $x = false, $y = false) |
||
451 | { |
||
452 | if (!$x && !$y) { |
||
453 | return false; |
||
454 | } |
||
455 | if ($tempImage = phpthumb_functions::ImageCreateFunction(imagesx($gdimg), imagesy($gdimg))) { |
||
456 | if ($x) { |
||
457 | imagecopy($tempImage, $gdimg, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg)); |
||
458 | for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) { |
||
459 | imagecopy($gdimg, $tempImage, imagesx($gdimg) - 1 - $x, 0, $x, 0, 1, imagesy($gdimg)); |
||
460 | } |
||
461 | } |
||
462 | if ($y) { |
||
463 | imagecopy($tempImage, $gdimg, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg)); |
||
464 | for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) { |
||
465 | imagecopy($gdimg, $tempImage, 0, imagesy($gdimg) - 1 - $y, 0, $y, imagesx($gdimg), 1); |
||
466 | } |
||
467 | } |
||
468 | imagedestroy($tempImage); |
||
469 | } |
||
470 | return true; |
||
471 | } |
||
472 | |||
473 | public function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) |
||
474 | { |
||
475 | $frame_width = ($frame_width ? $frame_width : 5); |
||
476 | $edge_width = ($edge_width ? $edge_width : 1); |
||
477 | $hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC'); |
||
478 | $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF'); |
||
479 | $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000'); |
||
480 | |||
481 | $color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame); |
||
482 | $color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1); |
||
483 | $color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2); |
||
484 | for ($i = 0; $i < $edge_width; $i++) { |
||
485 | // outer bevel |
||
486 | imageline($gdimg, $i, $i, $i, imagesy($gdimg) - $i, $color1); // left |
||
487 | imageline($gdimg, $i, $i, imagesx($gdimg) - $i, $i, $color1); // top |
||
488 | imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i, imagesx($gdimg) - $i, $i, $color2); // right |
||
489 | imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i, $i, imagesy($gdimg) - $i, $color2); // bottom |
||
490 | } |
||
491 | for ($i = 0; $i < $frame_width; $i++) { |
||
492 | // actual frame |
||
493 | imagerectangle($gdimg, $edge_width + $i, $edge_width + $i, imagesx($gdimg) - $edge_width - $i, imagesy($gdimg) - $edge_width - $i, $color_frame); |
||
494 | } |
||
495 | for ($i = 0; $i < $edge_width; $i++) { |
||
496 | // inner bevel |
||
497 | 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 |
||
498 | 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 |
||
499 | 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 |
||
500 | 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 |
||
501 | } |
||
502 | return true; |
||
503 | } |
||
504 | |||
505 | public function Gamma(&$gdimg, $amount) |
||
506 | { |
||
507 | if (number_format($amount, 4) == '1.0000') { |
||
508 | return true; |
||
509 | } |
||
510 | return imagegammacorrect($gdimg, 1.0, $amount); |
||
511 | } |
||
512 | |||
513 | public function Grayscale(&$gdimg) |
||
514 | { |
||
515 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
516 | if (imagefilter($gdimg, IMG_FILTER_GRAYSCALE)) { |
||
517 | return true; |
||
518 | } |
||
519 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__); |
||
520 | // fall through and try it the hard way |
||
521 | } |
||
522 | return $this->Colorize($gdimg, 100, 'gray'); |
||
523 | } |
||
524 | |||
525 | public function HistogramAnalysis(&$gdimg, $calculateGray = false) |
||
526 | { |
||
527 | $ImageSX = imagesx($gdimg); |
||
528 | $ImageSY = imagesy($gdimg); |
||
529 | $Analysis = []; |
||
530 | for ($x = 0; $x < $ImageSX; $x++) { |
||
531 | for ($y = 0; $y < $ImageSY; $y++) { |
||
532 | $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
533 | @$Analysis['red'][$OriginalPixel['red']]++; |
||
534 | @$Analysis['green'][$OriginalPixel['green']]++; |
||
535 | @$Analysis['blue'][$OriginalPixel['blue']]++; |
||
536 | @$Analysis['alpha'][$OriginalPixel['alpha']]++; |
||
537 | if ($calculateGray) { |
||
538 | $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel); |
||
539 | @$Analysis['gray'][$GrayPixel['red']]++; |
||
540 | } |
||
541 | } |
||
542 | } |
||
543 | $keys = ['red', 'green', 'blue', 'alpha']; |
||
544 | if ($calculateGray) { |
||
545 | $keys[] = 'gray'; |
||
546 | } |
||
547 | foreach ($keys as $dummy => $key) { |
||
548 | ksort($Analysis[$key]); |
||
549 | } |
||
550 | return $Analysis; |
||
551 | } |
||
552 | |||
553 | public function HistogramStretch(&$gdimg, $band = '*', $method = 0, $threshold = 0.1) |
||
554 | { |
||
555 | // equivalent of "Auto Contrast" in Adobe Photoshop |
||
556 | // method 0 stretches according to RGB colors. Gives a more conservative stretch. |
||
557 | // 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 |
||
558 | $Analysis = $this->HistogramAnalysis($gdimg, true); |
||
559 | $keys = ['r' => 'red', 'g' => 'green', 'b' => 'blue', 'a' => 'alpha', '*' => ($method == 0) ? 'all' : 'gray']; |
||
560 | $band = $band[0]; |
||
561 | if (!isset($keys[$band])) { |
||
562 | return false; |
||
563 | } |
||
564 | $key = $keys[$band]; |
||
565 | |||
566 | // If the absolute brightest and darkest pixels are used then one random |
||
567 | // pixel in the image could throw off the whole system. Instead, count up/down |
||
568 | // from the limit and allow <threshold> (default = 0.1%) of brightest/darkest |
||
569 | // pixels to be clipped to min/max |
||
570 | $threshold = (float)$threshold / 100; |
||
571 | $clip_threshold = imagesx($gdimg) * imagesx($gdimg) * $threshold; |
||
572 | |||
573 | $countsum = 0; |
||
574 | $range_min = 0; |
||
575 | for ($i = 0; $i <= 255; $i++) { |
||
576 | if ($method == 0) { |
||
577 | $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]); |
||
578 | } else { |
||
579 | $countsum += @$Analysis[$key][$i]; |
||
580 | } |
||
581 | if ($countsum >= $clip_threshold) { |
||
582 | $range_min = $i - 1; |
||
583 | break; |
||
584 | } |
||
585 | } |
||
586 | $range_min = max($range_min, 0); |
||
587 | |||
588 | $countsum = 0; |
||
589 | $range_max = 255; |
||
590 | for ($i = 255; $i >= 0; $i--) { |
||
591 | if ($method == 0) { |
||
592 | $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]); |
||
593 | } else { |
||
594 | $countsum += @$Analysis[$key][$i]; |
||
595 | } |
||
596 | if ($countsum >= $clip_threshold) { |
||
597 | $range_max = $i + 1; |
||
598 | break; |
||
599 | } |
||
600 | } |
||
601 | $range_max = min($range_max, 255); |
||
602 | |||
603 | $range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min))); |
||
604 | if (($range_min == 0) && ($range_max == 255)) { |
||
605 | // no adjustment necessary - don't waste CPU time! |
||
606 | return true; |
||
607 | } |
||
608 | |||
609 | $ImageSX = imagesx($gdimg); |
||
610 | $ImageSY = imagesy($gdimg); |
||
611 | for ($x = 0; $x < $ImageSX; $x++) { |
||
612 | for ($y = 0; $y < $ImageSY; $y++) { |
||
613 | $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
614 | if ($band == '*') { |
||
615 | $new['red'] = min(255, max(0, ($OriginalPixel['red'] - $range_min) * $range_scale)); |
||
616 | $new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale)); |
||
617 | $new['blue'] = min(255, max(0, ($OriginalPixel['blue'] - $range_min) * $range_scale)); |
||
618 | $new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale)); |
||
619 | } else { |
||
620 | $new = $OriginalPixel; |
||
621 | $new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale)); |
||
622 | } |
||
623 | $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']); |
||
624 | imagesetpixel($gdimg, $x, $y, $newColor); |
||
625 | } |
||
626 | } |
||
627 | |||
628 | return true; |
||
629 | } |
||
630 | |||
631 | public function HistogramOverlay(&$gdimg, $bands = '*', $colors = '', $width = 0.25, $height = 0.25, $alignment = 'BR', $opacity = 50, $margin_x = 5, $margin_y = null) |
||
632 | { |
||
633 | $margin_y = (null === $margin_y ? $margin_x : $margin_y); |
||
634 | |||
635 | $Analysis = $this->HistogramAnalysis($gdimg, true); |
||
636 | $histW = round(($width > 1) ? min($width, imagesx($gdimg)) : imagesx($gdimg) * $width); |
||
637 | $histH = round(($width > 1) ? min($width, imagesx($gdimg)) : imagesx($gdimg) * $width); |
||
638 | if ($gdHist = imagecreatetruecolor($histW, $histH)) { |
||
639 | $color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127); |
||
640 | imagefilledrectangle($gdHist, 0, 0, $histW, $histH, $color_back); |
||
641 | imagealphablending($gdHist, false); |
||
642 | imagesavealpha($gdHist, true); |
||
643 | |||
644 | $HistogramTempWidth = 256; |
||
645 | $HistogramTempHeight = 100; |
||
646 | if ($gdHistTemp = imagecreatetruecolor($HistogramTempWidth, $HistogramTempHeight)) { |
||
647 | $color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127); |
||
648 | imagealphablending($gdHistTemp, false); |
||
649 | imagesavealpha($gdHistTemp, true); |
||
650 | imagefilledrectangle($gdHistTemp, 0, 0, imagesx($gdHistTemp), imagesy($gdHistTemp), $color_back_temp); |
||
651 | |||
652 | $DefaultColors = ['r' => 'FF0000', 'g' => '00FF00', 'b' => '0000FF', 'a' => '999999', '*' => 'FFFFFF']; |
||
653 | $Colors = explode(';', $colors); |
||
654 | $BandsToGraph = array_unique(preg_split('##', $bands)); |
||
655 | $keys = ['r' => 'red', 'g' => 'green', 'b' => 'blue', 'a' => 'alpha', '*' => 'gray']; |
||
656 | foreach ($BandsToGraph as $key => $band) { |
||
657 | if (!isset($keys[$band])) { |
||
658 | continue; |
||
659 | } |
||
660 | $PeakValue = max($Analysis[$keys[$band]]); |
||
661 | $thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]); |
||
662 | for ($x = 0; $x < $HistogramTempWidth; $x++) { |
||
663 | imageline($gdHistTemp, $x, $HistogramTempHeight - 1, $x, $HistogramTempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $HistogramTempHeight), $thisColor); |
||
664 | } |
||
665 | imageline($gdHistTemp, 0, $HistogramTempHeight - 1, $HistogramTempWidth - 1, $HistogramTempHeight - 1, $thisColor); |
||
666 | imageline($gdHistTemp, 0, $HistogramTempHeight - 2, $HistogramTempWidth - 1, $HistogramTempHeight - 2, $thisColor); |
||
667 | } |
||
668 | imagecopyresampled($gdHist, $gdHistTemp, 0, 0, 0, 0, imagesx($gdHist), imagesy($gdHist), imagesx($gdHistTemp), imagesy($gdHistTemp)); |
||
669 | imagedestroy($gdHistTemp); |
||
670 | } else { |
||
671 | return false; |
||
672 | } |
||
673 | |||
674 | $this->WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin_x, $margin_y); |
||
675 | imagedestroy($gdHist); |
||
676 | return true; |
||
677 | } |
||
678 | return false; |
||
679 | } |
||
680 | |||
681 | public function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) |
||
682 | { |
||
683 | $border_width = ($border_width ? $border_width : 1); |
||
684 | $radius_x = ($radius_x ? $radius_x : 0); |
||
685 | $radius_y = ($radius_y ? $radius_y : 0); |
||
686 | |||
687 | $output_width = imagesx($gdimg); |
||
688 | $output_height = imagesy($gdimg); |
||
689 | |||
690 | [$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)); |
||
691 | $offset_x = ($radius_x ? $output_width - $new_width - $radius_x : 0); |
||
692 | |||
693 | if ($gd_border_canvas = phpthumb_functions::ImageCreateFunction($output_width, $output_height)) { |
||
694 | imagesavealpha($gd_border_canvas, true); |
||
695 | imagealphablending($gd_border_canvas, false); |
||
696 | $color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gd_border_canvas, 255, 255, 255, 127); |
||
697 | imagefilledrectangle($gd_border_canvas, 0, 0, $output_width, $output_height, $color_background); |
||
698 | |||
699 | $color_border = phpthumb_functions::ImageHexColorAllocate($gd_border_canvas, (phpthumb_functions::IsHexColor($hexcolor_border) ? $hexcolor_border : '000000')); |
||
700 | |||
701 | for ($i = 0; $i < $border_width; $i++) { |
||
702 | imageline($gd_border_canvas, floor($offset_x / 2) + $radius_x, $i, $output_width - $radius_x - ceil($offset_x / 2), $i, $color_border); // top |
||
703 | 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 |
||
704 | imageline($gd_border_canvas, floor($offset_x / 2) + $i, $radius_y, floor($offset_x / 2) + $i, $output_height - $radius_y, $color_border); // left |
||
705 | 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 |
||
706 | } |
||
707 | |||
708 | if ($radius_x && $radius_y) { |
||
709 | // PHP bug: imagearc() with thicknesses > 1 give bad/undesirable/unpredicatable results |
||
710 | // Solution: Draw multiple 1px arcs side-by-side. |
||
711 | |||
712 | // Problem: parallel arcs give strange/ugly antialiasing problems |
||
713 | // Solution: draw non-parallel arcs, from one side of the line thickness at the start angle |
||
714 | // to the opposite edge of the line thickness at the terminating angle |
||
715 | for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) { |
||
716 | 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 |
||
717 | 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 |
||
718 | 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 |
||
719 | 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 |
||
720 | } |
||
721 | if ($border_width > 1) { |
||
722 | for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) { |
||
723 | 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 |
||
724 | 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 |
||
725 | 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 |
||
726 | 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 |
||
727 | } |
||
728 | } |
||
729 | } |
||
730 | $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); |
||
731 | |||
732 | imagedestroy($gdimg); |
||
733 | $gdimg = phpthumb_functions::ImageCreateFunction($output_width, $output_height); |
||
734 | imagesavealpha($gdimg, true); |
||
735 | imagealphablending($gdimg, false); |
||
736 | $gdimg_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 255, 255, 255, 127); |
||
737 | imagefilledrectangle($gdimg, 0, 0, $output_width, $output_height, $gdimg_color_background); |
||
738 | |||
739 | imagecopy($gdimg, $gd_border_canvas, 0, 0, 0, 0, $output_width, $output_height); |
||
740 | imagedestroy($gd_border_canvas); |
||
741 | return true; |
||
742 | } else { |
||
743 | $this->DebugMessage('FAILED: $gd_border_canvas = phpthumb_functions::ImageCreateFunction(' . $output_width . ', ' . $output_height . ')', __FILE__, __LINE__); |
||
744 | } |
||
745 | return false; |
||
746 | } |
||
747 | |||
748 | public static function ImprovedImageRotate(&$gdimg_source, $rotate_angle = 0, $config_background_hexcolor = 'FFFFFF', $bg = null, &$phpThumbObject) |
||
749 | { |
||
750 | while ($rotate_angle < 0) { |
||
751 | $rotate_angle += 360; |
||
752 | } |
||
753 | $rotate_angle %= 360; |
||
754 | if ($rotate_angle != 0) { |
||
755 | $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_source, $config_background_hexcolor); |
||
756 | |||
757 | if ((phpthumb_functions::gd_version() >= 2) && !$bg && ($rotate_angle % 90)) { |
||
758 | //$this->DebugMessage('Using alpha rotate', __FILE__, __LINE__); |
||
759 | if ($gdimg_rotate_mask = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_source), imagesy($gdimg_source))) { |
||
760 | $color_mask = []; |
||
761 | for ($i = 0; $i <= 255; $i++) { |
||
762 | $color_mask[$i] = imagecolorallocate($gdimg_rotate_mask, $i, $i, $i); |
||
763 | } |
||
764 | imagefilledrectangle($gdimg_rotate_mask, 0, 0, imagesx($gdimg_rotate_mask), imagesy($gdimg_rotate_mask), $color_mask[255]); |
||
765 | $imageX = imagesx($gdimg_source); |
||
766 | $imageY = imagesy($gdimg_source); |
||
767 | for ($x = 0; $x < $imageX; $x++) { |
||
768 | for ($y = 0; $y < $imageY; $y++) { |
||
769 | $pixelcolor = phpthumb_functions::GetPixelColor($gdimg_source, $x, $y); |
||
770 | imagesetpixel($gdimg_rotate_mask, $x, $y, $color_mask[255 - round($pixelcolor['alpha'] * 255 / 127)]); |
||
771 | } |
||
772 | } |
||
773 | $gdimg_rotate_mask = imagerotate($gdimg_rotate_mask, $rotate_angle, $color_mask[0]); |
||
774 | $gdimg_source = imagerotate($gdimg_source, $rotate_angle, $background_color); |
||
775 | |||
776 | imagealphablending($gdimg_source, false); |
||
777 | imagesavealpha($gdimg_source, true); |
||
778 | //$this->is_alpha = true; |
||
779 | $phpThumbFilters = new self(); |
||
780 | //$phpThumbFilters->phpThumbObject = $this; |
||
781 | $phpThumbFilters->phpThumbObject = $phpThumbObject; |
||
782 | $phpThumbFilters->ApplyMask($gdimg_rotate_mask, $gdimg_source); |
||
783 | |||
784 | imagedestroy($gdimg_rotate_mask); |
||
785 | } else { |
||
786 | //$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__); |
||
787 | } |
||
788 | } else { |
||
789 | if (phpthumb_functions::gd_version() < 2) { |
||
790 | //$this->DebugMessage('Using non-alpha rotate because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||
791 | } elseif ($bg) { |
||
792 | //$this->DebugMessage('Using non-alpha rotate because $this->bg is "'.$bg.'"', __FILE__, __LINE__); |
||
793 | } elseif ($rotate_angle % 90) { |
||
794 | //$this->DebugMessage('Using non-alpha rotate because ($rotate_angle % 90) = "'.($rotate_angle % 90).'"', __FILE__, __LINE__); |
||
795 | } else { |
||
796 | //$this->DebugMessage('Using non-alpha rotate because $this->thumbnailFormat is "'.$this->thumbnailFormat.'"', __FILE__, __LINE__); |
||
797 | } |
||
798 | |||
799 | if (imagecolortransparent($gdimg_source) >= 0) { |
||
800 | // imagerotate() forgets all about an image's transparency and sets the transparent color to black |
||
801 | // To compensate, flood-fill the transparent color of the source image with the specified background color first |
||
802 | // then rotate and the colors should match |
||
803 | |||
804 | if (!function_exists('imageistruecolor') || !imageistruecolor($gdimg_source)) { |
||
805 | // convert paletted image to true-color before rotating to prevent nasty aliasing artifacts |
||
806 | |||
807 | //$this->source_width = imagesx($gdimg_source); |
||
808 | //$this->source_height = imagesy($gdimg_source); |
||
809 | $gdimg_newsrc = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_source), imagesy($gdimg_source)); |
||
810 | $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor); |
||
811 | imagefilledrectangle($gdimg_newsrc, 0, 0, imagesx($gdimg_source), imagesy($gdimg_source), phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor)); |
||
812 | imagecopy($gdimg_newsrc, $gdimg_source, 0, 0, 0, 0, imagesx($gdimg_source), imagesy($gdimg_source)); |
||
813 | imagedestroy($gdimg_source); |
||
814 | unset($gdimg_source); |
||
815 | $gdimg_source = $gdimg_newsrc; |
||
816 | unset($gdimg_newsrc); |
||
817 | } else { |
||
818 | imagecolorset( |
||
819 | $gdimg_source, |
||
820 | imagecolortransparent($gdimg_source), |
||
821 | hexdec(substr($config_background_hexcolor, 0, 2)), |
||
822 | hexdec(substr($config_background_hexcolor, 2, 2)), |
||
823 | hexdec(substr($config_background_hexcolor, 4, 2)) |
||
824 | ); |
||
825 | |||
826 | imagecolortransparent($gdimg_source, -1); |
||
827 | } |
||
828 | } |
||
829 | |||
830 | $gdimg_source = imagerotate($gdimg_source, $rotate_angle, $background_color); |
||
831 | } |
||
832 | } |
||
833 | return true; |
||
834 | } |
||
835 | |||
836 | public function MeanRemoval(&$gdimg) |
||
848 | } |
||
849 | |||
850 | public function Negative(&$gdimg) |
||
851 | { |
||
852 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
853 | if (imagefilter($gdimg, IMG_FILTER_NEGATE)) { |
||
854 | return true; |
||
855 | } |
||
856 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_NEGATE)', __FILE__, __LINE__); |
||
857 | // fall through and try it the hard way |
||
858 | } |
||
859 | $ImageSX = imagesx($gdimg); |
||
860 | $ImageSY = imagesy($gdimg); |
||
861 | for ($x = 0; $x < $ImageSX; $x++) { |
||
862 | for ($y = 0; $y < $ImageSY; $y++) { |
||
863 | $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
864 | $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, ~$currentPixel['red'] & 0xFF, ~$currentPixel['green'] & 0xFF, ~$currentPixel['blue'] & 0xFF, $currentPixel['alpha']); |
||
865 | imagesetpixel($gdimg, $x, $y, $newColor); |
||
866 | } |
||
867 | } |
||
868 | return true; |
||
869 | } |
||
870 | |||
871 | public function RoundedImageCorners(&$gdimg, $radius_x, $radius_y) |
||
872 | { |
||
873 | // generate mask at twice desired resolution and downsample afterwards for easy antialiasing |
||
874 | // mask is generated as a white double-size ellipse on a triple-size black background and copy-paste-resampled |
||
875 | // onto a correct-size mask image as 4 corners due to errors when the entire mask is resampled at once (gray edges) |
||
876 | if ($gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction($radius_x * 6, $radius_y * 6)) { |
||
877 | if ($gdimg_cornermask = phpthumb_functions::ImageCreateFunction(imagesx($gdimg), imagesy($gdimg))) { |
||
878 | $color_transparent = imagecolorallocate($gdimg_cornermask_triple, 255, 255, 255); |
||
879 | imagefilledellipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent); |
||
880 | |||
881 | imagefilledrectangle($gdimg_cornermask, 0, 0, imagesx($gdimg), imagesy($gdimg), $color_transparent); |
||
882 | |||
883 | imagecopyresampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, 0, $radius_x, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2); |
||
884 | 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); |
||
885 | 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); |
||
886 | 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); |
||
887 | |||
888 | $this->ApplyMask($gdimg_cornermask, $gdimg); |
||
889 | imagedestroy($gdimg_cornermask); |
||
890 | $this->DebugMessage('RoundedImageCorners(' . $radius_x . ', ' . $radius_y . ') succeeded', __FILE__, __LINE__); |
||
891 | return true; |
||
892 | } else { |
||
893 | $this->DebugMessage('FAILED: $gdimg_cornermask = phpthumb_functions::ImageCreateFunction(' . imagesx($gdimg) . ', ' . imagesy($gdimg) . ')', __FILE__, __LINE__); |
||
894 | } |
||
895 | imagedestroy($gdimg_cornermask_triple); |
||
896 | } else { |
||
897 | $this->DebugMessage('FAILED: $gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction(' . ($radius_x * 6) . ', ' . ($radius_y * 6) . ')', __FILE__, __LINE__); |
||
898 | } |
||
899 | return false; |
||
900 | } |
||
901 | |||
902 | public function Saturation(&$gdimg, $amount, $color = '') |
||
903 | { |
||
904 | if ($amount == 0) { |
||
905 | return true; |
||
906 | } elseif ($amount > 0) { |
||
907 | $amount = 0 - $amount; |
||
908 | } else { |
||
909 | $amount = abs($amount); |
||
910 | } |
||
911 | return $this->Desaturate($gdimg, $amount, $color); |
||
912 | } |
||
913 | |||
914 | public function Sepia(&$gdimg, $amount, $targetColor) |
||
915 | { |
||
916 | $amount = (is_numeric($amount) ? max(0, min(100, $amount)) : 50); |
||
917 | $amountPct = $amount / 100; |
||
918 | $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'A28065'); |
||
919 | |||
920 | if ($amount == 0) { |
||
921 | return true; |
||
922 | } |
||
923 | |||
924 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
925 | if (imagefilter($gdimg, IMG_FILTER_GRAYSCALE)) { |
||
926 | $r = round($amountPct * hexdec(substr($targetColor, 0, 2))); |
||
927 | $g = round($amountPct * hexdec(substr($targetColor, 2, 2))); |
||
928 | $b = round($amountPct * hexdec(substr($targetColor, 4, 2))); |
||
929 | if (imagefilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) { |
||
930 | return true; |
||
931 | } |
||
932 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__); |
||
933 | // fall through and try it the hard way |
||
934 | |||
935 | } else { |
||
936 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__); |
||
937 | // fall through and try it the hard way |
||
938 | |||
939 | } |
||
940 | } |
||
941 | |||
942 | $TargetPixel['red'] = hexdec(substr($targetColor, 0, 2)); |
||
943 | $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2)); |
||
944 | $TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2)); |
||
945 | |||
946 | $ImageSX = imagesx($gdimg); |
||
947 | $ImageSY = imagesy($gdimg); |
||
948 | for ($x = 0; $x < $ImageSX; $x++) { |
||
949 | for ($y = 0; $y < $ImageSY; $y++) { |
||
950 | $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
951 | $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel); |
||
952 | |||
953 | // http://www.gimpguru.org/Tutorials/SepiaToning/ |
||
954 | // "In the traditional sepia toning process, the tinting occurs most in |
||
955 | // the mid-tones: the lighter and darker areas appear to be closer to B&W." |
||
956 | $SepiaAmount = ((128 - abs($GrayPixel['red'] - 128)) / 128) * $amountPct; |
||
957 | |||
958 | $NewPixel = []; |
||
959 | foreach ($TargetPixel as $key => $value) { |
||
960 | $NewPixel[$key] = round(max(0, min(255, $GrayPixel[$key] * (1 - $SepiaAmount) + ($TargetPixel[$key] * $SepiaAmount)))); |
||
961 | } |
||
962 | $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']); |
||
963 | imagesetpixel($gdimg, $x, $y, $newColor); |
||
964 | } |
||
965 | } |
||
966 | return true; |
||
967 | } |
||
968 | |||
969 | public function Smooth(&$gdimg, $amount = 6) |
||
970 | { |
||
971 | $amount = min(25, max(0, $amount)); |
||
972 | if ($amount == 0) { |
||
973 | return true; |
||
974 | } |
||
975 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||
976 | if (imagefilter($gdimg, IMG_FILTER_SMOOTH, $amount)) { |
||
977 | return true; |
||
978 | } |
||
979 | $this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_SMOOTH, ' . $amount . ')', __FILE__, __LINE__); |
||
980 | // fall through and try it the hard way |
||
981 | } |
||
982 | // currently not implemented "the hard way" |
||
983 | $this->DebugMessage('FAILED: phpthumb_filters::Smooth($gdimg, ' . $amount . ') [function not implemented]', __FILE__, __LINE__); |
||
984 | return false; |
||
985 | } |
||
986 | |||
987 | public function SourceTransparentColorMask(&$gdimg, $hexcolor, $min_limit = 5, $max_limit = 10) |
||
988 | { |
||
989 | $width = imagesx($gdimg); |
||
990 | $height = imagesy($gdimg); |
||
991 | if ($gdimg_mask = imagecreatetruecolor($width, $height)) { |
||
992 | $R = hexdec(substr($hexcolor, 0, 2)); |
||
993 | $G = hexdec(substr($hexcolor, 2, 2)); |
||
994 | $B = hexdec(substr($hexcolor, 4, 2)); |
||
995 | $targetPixel = ['red' => $R, 'green' => $G, 'blue' => $B]; |
||
996 | $cutoffRange = $max_limit - $min_limit; |
||
997 | for ($x = 0; $x < $width; $x++) { |
||
998 | for ($y = 0; $y < $height; $y++) { |
||
999 | $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y); |
||
1000 | $colorDiff = phpthumb_functions::PixelColorDifferencePercent($currentPixel, $targetPixel); |
||
1001 | $grayLevel = min($cutoffRange, max(0, -$min_limit + $colorDiff)) * (255 / max(1, $cutoffRange)); |
||
1002 | $newColor = imagecolorallocate($gdimg_mask, $grayLevel, $grayLevel, $grayLevel); |
||
1003 | imagesetpixel($gdimg_mask, $x, $y, $newColor); |
||
1004 | } |
||
1005 | } |
||
1006 | return $gdimg_mask; |
||
1007 | } |
||
1008 | return false; |
||
1009 | } |
||
1010 | |||
1011 | public function Threshold(&$gdimg, $cutoff) |
||
1029 | } |
||
1030 | |||
1031 | public function ImageTrueColorToPalette2(&$image, $dither, $ncolors) |
||
1032 | { |
||
1033 | // http://www.php.net/manual/en/function.imagetruecolortopalette.php |
||
1034 | // zmorris at zsculpt dot com (17-Aug-2004 06:58) |
||
1035 | $width = imagesx($image); |
||
1036 | $height = imagesy($image); |
||
1037 | $image_copy = imagecreatetruecolor($width, $height); |
||
1038 | //imagecopymerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100); |
||
1039 | imagecopy($image_copy, $image, 0, 0, 0, 0, $width, $height); |
||
1040 | imagetruecolortopalette($image, $dither, $ncolors); |
||
1041 | imagecolormatch($image_copy, $image); |
||
1042 | imagedestroy($image_copy); |
||
1043 | return true; |
||
1044 | } |
||
1045 | |||
1046 | public function ReduceColorDepth(&$gdimg, $colors = 256, $dither = true) |
||
1047 | { |
||
1048 | $colors = max(min($colors, 256), 2); |
||
1049 | // imagetruecolortopalette usually makes ugly colors, the replacement is a bit better |
||
1050 | //imagetruecolortopalette($gdimg, $dither, $colors); |
||
1051 | $this->ImageTrueColorToPalette2($gdimg, $dither, $colors); |
||
1052 | return true; |
||
1053 | } |
||
1054 | |||
1055 | public function WhiteBalance(&$gdimg, $targetColor = '') |
||
1090 | } |
||
1091 | |||
1092 | public function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color = '000000', $ttffont = '', $opacity = 100, $margin = 5, $angle = 0, $bg_color = false, $bg_opacity = 0, $fillextend = '', $lineheight = 1.0) |
||
1093 | { |
||
1094 | // text watermark requested |
||
1095 | if (!$text) { |
||
1096 | return false; |
||
1097 | } |
||
1098 | imagealphablending($gdimg, true); |
||
1099 | |||
1100 | if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)(@[LCR])?$#i', $alignment, $matches)) { |
||
1101 | $originOffsetX = (int)$matches[1]; |
||
1102 | $originOffsetY = (int)$matches[2]; |
||
1393 | } |
||
1394 | |||
1395 | public function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment = '*', $opacity = 50, $margin_x = 5, $margin_y = null) |
||
1396 | { |
||
1397 | if ((is_resource($gdimg_dest) || (is_object($gdimg_dest) && $gdimg_dest instanceof \GdImage)) && (is_resource($img_watermark) || (is_object($img_watermark) && $img_watermark instanceof \GdImage))) { |
||
1510 | } |
||
1511 | } |
||
1512 |