PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/snippets/phpthumbof/phpthumb.filters.php

https://github.com/good-web-master/modx.evo.custom
PHP | 1377 lines | 1056 code | 206 blank | 115 comment | 226 complexity | a6a0c0a1b74ab1397adf83b8143f0d0a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  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. class phpthumb_filters {
  11. var $phpThumbObject = null;
  12. function phpthumb_filters() {
  13. return true;
  14. }
  15. function ApplyMask(&$gdimg_mask, &$gdimg_image) {
  16. if (phpthumb_functions::gd_version() < 2) {
  17. $this->DebugMessage('Skipping ApplyMask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  18. return false;
  19. }
  20. if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
  21. $this->DebugMessage('Using alpha ApplyMask() technique', __FILE__, __LINE__);
  22. if ($gdimg_mask_resized = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
  23. ImageCopyResampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, ImageSX($gdimg_image), ImageSY($gdimg_image), ImageSX($gdimg_mask), ImageSY($gdimg_mask));
  24. if ($gdimg_mask_blendtemp = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_image), ImageSY($gdimg_image))) {
  25. $color_background = ImageColorAllocate($gdimg_mask_blendtemp, 0, 0, 0);
  26. ImageFilledRectangle($gdimg_mask_blendtemp, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp), $color_background);
  27. ImageAlphaBlending($gdimg_mask_blendtemp, false);
  28. ImageSaveAlpha($gdimg_mask_blendtemp, true);
  29. for ($x = 0; $x < ImageSX($gdimg_image); $x++) {
  30. for ($y = 0; $y < ImageSY($gdimg_image); $y++) {
  31. //$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y);
  32. $RealPixel = phpthumb_functions::GetPixelColor($gdimg_image, $x, $y);
  33. $MaskPixel = phpthumb_functions::GrayscalePixel(phpthumb_functions::GetPixelColor($gdimg_mask_resized, $x, $y));
  34. $MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127)));
  35. $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha);
  36. ImageSetPixel($gdimg_mask_blendtemp, $x, $y, $newcolor);
  37. }
  38. }
  39. ImageAlphaBlending($gdimg_image, false);
  40. ImageSaveAlpha($gdimg_image, true);
  41. ImageCopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp));
  42. ImageDestroy($gdimg_mask_blendtemp);
  43. } else {
  44. $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  45. }
  46. ImageDestroy($gdimg_mask_resized);
  47. } else {
  48. $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  49. }
  50. } else {
  51. // alpha merging requires PHP v4.3.2+
  52. $this->DebugMessage('Skipping ApplyMask() technique because PHP is v"'.phpversion().'"', __FILE__, __LINE__);
  53. }
  54. return true;
  55. }
  56. function Bevel(&$gdimg, $width, $hexcolor1, $hexcolor2) {
  57. $width = ($width ? $width : 5);
  58. $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
  59. $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
  60. ImageAlphaBlending($gdimg, true);
  61. for ($i = 0; $i < $width; $i++) {
  62. $alpha = round(($i / $width) * 127);
  63. $color1[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1, false, $alpha);
  64. $color2[$i] = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2, false, $alpha);
  65. ImageLine($gdimg, $i, $i, $i, ImageSY($gdimg) - $i, $color1[$i]); // left
  66. ImageLine($gdimg, $i, $i, ImageSX($gdimg) - $i, $i, $color1[$i]); // top
  67. ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i, $i, $color2[$i]); // right
  68. ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, $i, ImageSY($gdimg) - $i, $color2[$i]); // bottom
  69. }
  70. return true;
  71. }
  72. function Blur(&$gdimg, $radius=0.5) {
  73. // Taken from Torstein Hřnsi's phpUnsharpMask (see phpthumb.unsharp.php)
  74. $radius = round(max(0, min($radius, 50)) * 2);
  75. if (!$radius) {
  76. return false;
  77. }
  78. $w = ImageSX($gdimg);
  79. $h = ImageSY($gdimg);
  80. if ($imgBlur = ImageCreateTrueColor($w, $h)) {
  81. // Gaussian blur matrix:
  82. // 1 2 1
  83. // 2 4 2
  84. // 1 2 1
  85. // Move copies of the image around one pixel at the time and merge them with weight
  86. // according to the matrix. The same matrix is simply repeated for higher radii.
  87. for ($i = 0; $i < $radius; $i++) {
  88. ImageCopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
  89. ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.00000); // down right
  90. ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
  91. ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.00000); // up right
  92. ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
  93. ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.00000); // right
  94. ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.00000); // up
  95. ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667); // down
  96. ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.000000); // center
  97. ImageCopy ($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
  98. }
  99. return true;
  100. }
  101. return false;
  102. }
  103. function BlurGaussian(&$gdimg) {
  104. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  105. if (ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)) {
  106. return true;
  107. }
  108. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)', __FILE__, __LINE__);
  109. // fall through and try it the hard way
  110. }
  111. $this->DebugMessage('FAILED: phpthumb_filters::BlurGaussian($gdimg) [using phpthumb_filters::Blur() instead]', __FILE__, __LINE__);
  112. return phpthumb_filters::Blur($gdimg, 0.5);
  113. }
  114. function BlurSelective(&$gdimg) {
  115. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  116. if (ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)) {
  117. return true;
  118. }
  119. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)', __FILE__, __LINE__);
  120. // fall through and try it the hard way
  121. }
  122. // currently not implemented "the hard way"
  123. $this->DebugMessage('FAILED: phpthumb_filters::BlurSelective($gdimg) [function not implemented]', __FILE__, __LINE__);
  124. return false;
  125. }
  126. function Brightness(&$gdimg, $amount=0) {
  127. if ($amount == 0) {
  128. return true;
  129. }
  130. $amount = max(-255, min(255, $amount));
  131. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  132. if (ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, $amount)) {
  133. return true;
  134. }
  135. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_BRIGHTNESS, '.$amount.')', __FILE__, __LINE__);
  136. // fall through and try it the hard way
  137. }
  138. $scaling = (255 - abs($amount)) / 255;
  139. $baseamount = (($amount > 0) ? $amount : 0);
  140. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  141. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  142. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  143. foreach ($OriginalPixel as $key => $value) {
  144. $NewPixel[$key] = round($baseamount + ($OriginalPixel[$key] * $scaling));
  145. }
  146. $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
  147. ImageSetPixel($gdimg, $x, $y, $newColor);
  148. }
  149. }
  150. return true;
  151. }
  152. function Contrast(&$gdimg, $amount=0) {
  153. if ($amount == 0) {
  154. return true;
  155. }
  156. $amount = max(-255, min(255, $amount));
  157. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  158. if (ImageFilter($gdimg, IMG_FILTER_CONTRAST, $amount)) {
  159. return true;
  160. }
  161. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_CONTRAST, '.$amount.')', __FILE__, __LINE__);
  162. // fall through and try it the hard way
  163. }
  164. if ($amount > 0) {
  165. $scaling = 1 + ($amount / 255);
  166. } else {
  167. $scaling = (255 - abs($amount)) / 255;
  168. }
  169. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  170. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  171. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  172. foreach ($OriginalPixel as $key => $value) {
  173. $NewPixel[$key] = min(255, max(0, round($OriginalPixel[$key] * $scaling)));
  174. }
  175. $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
  176. ImageSetPixel($gdimg, $x, $y, $newColor);
  177. }
  178. }
  179. }
  180. function Colorize(&$gdimg, $amount, $targetColor) {
  181. $amount = (is_numeric($amount) ? $amount : 25);
  182. $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray');
  183. if ($amount == 0) {
  184. return true;
  185. }
  186. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  187. if ($targetColor == 'gray') {
  188. $targetColor = '808080';
  189. }
  190. $r = substr($targetColor, 0, 2);
  191. $g = substr($targetColor, 2, 2);
  192. $b = substr($targetColor, 4, 2);
  193. if (ImageFilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
  194. return true;
  195. }
  196. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
  197. // fall through and try it the hard way
  198. }
  199. // overridden below for grayscale
  200. if ($targetColor != 'gray') {
  201. $TargetPixel['red'] = hexdec(substr($targetColor, 0, 2));
  202. $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
  203. $TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2));
  204. }
  205. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  206. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  207. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  208. if ($targetColor == 'gray') {
  209. $TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
  210. }
  211. foreach ($TargetPixel as $key => $value) {
  212. $NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * ($amount / 100)))));
  213. }
  214. //$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
  215. $newColor = ImageColorAllocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
  216. ImageSetPixel($gdimg, $x, $y, $newColor);
  217. }
  218. }
  219. return true;
  220. }
  221. function Crop(&$gdimg, $left=0, $right=0, $top=0, $bottom=0) {
  222. if (!$left && !$right && !$top && !$bottom) {
  223. return true;
  224. }
  225. $oldW = ImageSX($gdimg);
  226. $oldH = ImageSY($gdimg);
  227. if (($left > 0) && ($left < 1)) { $left = round($left * $oldW); }
  228. if (($right > 0) && ($right < 1)) { $right = round($right * $oldW); }
  229. if (($top > 0) && ($top < 1)) { $top = round($top * $oldH); }
  230. if (($bottom > 0) && ($bottom < 1)) { $bottom = round($bottom * $oldH); }
  231. $right = min($oldW - $left - 1, $right);
  232. $bottom = min($oldH - $top - 1, $bottom);
  233. $newW = $oldW - $left - $right;
  234. $newH = $oldH - $top - $bottom;
  235. if ($imgCropped = ImageCreateTrueColor($newW, $newH)) {
  236. ImageCopy($imgCropped, $gdimg, 0, 0, $left, $top, $newW, $newH);
  237. if ($gdimg = ImageCreateTrueColor($newW, $newH)) {
  238. ImageCopy($gdimg, $imgCropped, 0, 0, 0, 0, $newW, $newH);
  239. ImageDestroy($imgCropped);
  240. return true;
  241. }
  242. ImageDestroy($imgCropped);
  243. }
  244. return false;
  245. }
  246. function Desaturate(&$gdimg, $amount, $color='') {
  247. if ($amount == 0) {
  248. return true;
  249. }
  250. return phpthumb_filters::Colorize($gdimg, $amount, (phpthumb_functions::IsHexColor($color) ? $color : 'gray'));
  251. }
  252. function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $fade) {
  253. if (phpthumb_functions::gd_version() < 2) {
  254. return false;
  255. }
  256. $distance = ($distance ? $distance : 10);
  257. $width = ($width ? $width : 10);
  258. $hexcolor = ($hexcolor ? $hexcolor : '000000');
  259. $angle = ($angle ? $angle : 225);
  260. $fade = ($fade ? $fade : 1);
  261. $width_shadow = cos(deg2rad($angle)) * ($distance + $width);
  262. $height_shadow = sin(deg2rad($angle)) * ($distance + $width);
  263. $scaling = min(ImageSX($gdimg) / (ImageSX($gdimg) + abs($width_shadow)), ImageSY($gdimg) / (ImageSY($gdimg) + abs($height_shadow)));
  264. for ($i = 0; $i < $width; $i++) {
  265. $WidthAlpha[$i] = (abs(($width / 2) - $i) / $width) * $fade;
  266. $Offset['x'] = cos(deg2rad($angle)) * ($distance + $i);
  267. $Offset['y'] = sin(deg2rad($angle)) * ($distance + $i);
  268. }
  269. $tempImageWidth = ImageSX($gdimg) + abs($Offset['x']);
  270. $tempImageHeight = ImageSY($gdimg) + abs($Offset['y']);
  271. if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) {
  272. ImageAlphaBlending($gdimg_dropshadow_temp, false);
  273. ImageSaveAlpha($gdimg_dropshadow_temp, true);
  274. $transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127);
  275. ImageFill($gdimg_dropshadow_temp, 0, 0, $transparent1);
  276. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  277. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  278. $PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  279. }
  280. }
  281. for ($x = 0; $x < $tempImageWidth; $x++) {
  282. for ($y = 0; $y < $tempImageHeight; $y++) {
  283. //for ($i = 0; $i < $width; $i++) {
  284. for ($i = 0; $i < 1; $i++) {
  285. if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
  286. if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) {
  287. $thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']);
  288. ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. ImageAlphaBlending($gdimg_dropshadow_temp, true);
  295. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  296. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  297. if ($PixelMap[$x][$y]['alpha'] < 127) {
  298. $thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
  299. ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
  300. }
  301. }
  302. }
  303. ImageSaveAlpha($gdimg, true);
  304. ImageAlphaBlending($gdimg, false);
  305. //$this->is_alpha = true;
  306. $transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127);
  307. ImageFilledRectangle($gdimg, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $transparent2);
  308. ImageCopyResampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg_dropshadow_temp), ImageSY($gdimg_dropshadow_temp));
  309. ImageDestroy($gdimg_dropshadow_temp);
  310. }
  311. return true;
  312. }
  313. function EdgeDetect(&$gdimg) {
  314. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  315. if (ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)) {
  316. return true;
  317. }
  318. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)', __FILE__, __LINE__);
  319. // fall through and try it the hard way
  320. }
  321. // currently not implemented "the hard way"
  322. $this->DebugMessage('FAILED: phpthumb_filters::EdgeDetect($gdimg) [function not implemented]', __FILE__, __LINE__);
  323. return false;
  324. }
  325. function Elipse($gdimg) {
  326. if (phpthumb_functions::gd_version() < 2) {
  327. return false;
  328. }
  329. // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
  330. if ($gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg) * 2, ImageSY($gdimg) * 2)) {
  331. if ($gdimg_elipsemask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
  332. $color_transparent = ImageColorAllocate($gdimg_elipsemask_double, 255, 255, 255);
  333. ImageFilledEllipse($gdimg_elipsemask_double, ImageSX($gdimg), ImageSY($gdimg), (ImageSX($gdimg) - 1) * 2, (ImageSY($gdimg) - 1) * 2, $color_transparent);
  334. ImageCopyResampled($gdimg_elipsemask, $gdimg_elipsemask_double, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg) * 2, ImageSY($gdimg) * 2);
  335. phpthumb_filters::ApplyMask($gdimg_elipsemask, $gdimg);
  336. ImageDestroy($gdimg_elipsemask);
  337. return true;
  338. } else {
  339. $this->DebugMessage('$gdimg_elipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
  340. }
  341. ImageDestroy($gdimg_elipsemask_double);
  342. } else {
  343. $this->DebugMessage('$gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
  344. }
  345. return false;
  346. }
  347. function Emboss(&$gdimg) {
  348. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  349. if (ImageFilter($gdimg, IMG_FILTER_EMBOSS)) {
  350. return true;
  351. }
  352. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EMBOSS)', __FILE__, __LINE__);
  353. // fall through and try it the hard way
  354. }
  355. // currently not implemented "the hard way"
  356. $this->DebugMessage('FAILED: phpthumb_filters::Emboss($gdimg) [function not implemented]', __FILE__, __LINE__);
  357. return false;
  358. }
  359. function Flip(&$gdimg, $x=false, $y=false) {
  360. if (!$x && !$y) {
  361. return false;
  362. }
  363. if ($tempImage = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
  364. if ($x) {
  365. ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
  366. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  367. ImageCopy($gdimg, $tempImage, ImageSX($gdimg) - 1 - $x, 0, $x, 0, 1, ImageSY($gdimg));
  368. }
  369. }
  370. if ($y) {
  371. ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
  372. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  373. ImageCopy($gdimg, $tempImage, 0, ImageSY($gdimg) - 1 - $y, 0, $y, ImageSX($gdimg), 1);
  374. }
  375. }
  376. ImageDestroy($tempImage);
  377. }
  378. return true;
  379. }
  380. function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) {
  381. $frame_width = ($frame_width ? $frame_width : 5);
  382. $edge_width = ($edge_width ? $edge_width : 1);
  383. $hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC');
  384. $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
  385. $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
  386. $color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame);
  387. $color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1);
  388. $color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2);
  389. for ($i = 0; $i < $edge_width; $i++) {
  390. // outer bevel
  391. ImageLine($gdimg, $i, $i, $i, ImageSY($gdimg) - $i, $color1); // left
  392. ImageLine($gdimg, $i, $i, ImageSX($gdimg) - $i, $i, $color1); // top
  393. ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i, $i, $color2); // right
  394. ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, $i, ImageSY($gdimg) - $i, $color2); // bottom
  395. }
  396. for ($i = 0; $i < $frame_width; $i++) {
  397. // actual frame
  398. ImageRectangle($gdimg, $edge_width + $i, $edge_width + $i, ImageSX($gdimg) - $edge_width - $i, ImageSY($gdimg) - $edge_width - $i, $color_frame);
  399. }
  400. for ($i = 0; $i < $edge_width; $i++) {
  401. // inner bevel
  402. 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
  403. 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
  404. 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
  405. 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
  406. }
  407. return true;
  408. }
  409. function Gamma(&$gdimg, $amount) {
  410. if (number_format($amount, 4) == '1.0000') {
  411. return true;
  412. }
  413. return ImageGammaCorrect($gdimg, 1.0, $amount);
  414. }
  415. function Grayscale(&$gdimg) {
  416. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  417. if (ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)) {
  418. return true;
  419. }
  420. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
  421. // fall through and try it the hard way
  422. }
  423. return phpthumb_filters::Colorize($gdimg, 100, 'gray');
  424. }
  425. function HistogramAnalysis(&$gdimg, $calculateGray=false) {
  426. $ImageSX = ImageSX($gdimg);
  427. $ImageSY = ImageSY($gdimg);
  428. for ($x = 0; $x < $ImageSX; $x++) {
  429. for ($y = 0; $y < $ImageSY; $y++) {
  430. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  431. @$Analysis['red'][$OriginalPixel['red']]++;
  432. @$Analysis['green'][$OriginalPixel['green']]++;
  433. @$Analysis['blue'][$OriginalPixel['blue']]++;
  434. @$Analysis['alpha'][$OriginalPixel['alpha']]++;
  435. if ($calculateGray) {
  436. $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
  437. @$Analysis['gray'][$GrayPixel['red']]++;
  438. }
  439. }
  440. }
  441. $keys = array('red', 'green', 'blue', 'alpha');
  442. if ($calculateGray) {
  443. $keys[] = 'gray';
  444. }
  445. foreach ($keys as $dummy => $key) {
  446. ksort($Analysis[$key]);
  447. }
  448. return $Analysis;
  449. }
  450. function HistogramStretch(&$gdimg, $band='*', $method=0, $threshold=0.1) {
  451. // equivalent of "Auto Contrast" in Adobe Photoshop
  452. // method 0 stretches according to RGB colors. Gives a more conservative stretch.
  453. // 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
  454. $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
  455. $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>(($method == 0) ? 'all' : 'gray'));
  456. $band = substr($band, 0, 1);
  457. if (!isset($keys[$band])) {
  458. return false;
  459. }
  460. $key = $keys[$band];
  461. // If the absolute brightest and darkest pixels are used then one random
  462. // pixel in the image could throw off the whole system. Instead, count up/down
  463. // from the limit and allow <threshold> (default = 0.1%) of brightest/darkest
  464. // pixels to be clipped to min/max
  465. $threshold = floatval($threshold) / 100;
  466. $clip_threshold = ImageSX($gdimg) * ImageSX($gdimg) * $threshold;
  467. //if ($min >= 0) {
  468. // $range_min = min($min, 255);
  469. //} else {
  470. $countsum = 0;
  471. for ($i = 0; $i <= 255; $i++) {
  472. if ($method == 0) {
  473. $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
  474. } else {
  475. $countsum += @$Analysis[$key][$i];
  476. }
  477. if ($countsum >= $clip_threshold) {
  478. $range_min = $i - 1;
  479. break;
  480. }
  481. }
  482. $range_min = max($range_min, 0);
  483. //}
  484. //if ($max > 0) {
  485. // $range_max = max($max, 255);
  486. //} else {
  487. $countsum = 0;
  488. for ($i = 255; $i >= 0; $i--) {
  489. if ($method == 0) {
  490. $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
  491. } else {
  492. $countsum += @$Analysis[$key][$i];
  493. }
  494. if ($countsum >= $clip_threshold) {
  495. $range_max = $i + 1;
  496. break;
  497. }
  498. }
  499. $range_max = min($range_max, 255);
  500. //}
  501. $range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min)));
  502. if (($range_min == 0) && ($range_max == 255)) {
  503. // no adjustment neccesary - don't waste CPU time!
  504. return true;
  505. }
  506. $ImageSX = ImageSX($gdimg);
  507. $ImageSY = ImageSY($gdimg);
  508. for ($x = 0; $x < $ImageSX; $x++) {
  509. for ($y = 0; $y < $ImageSY; $y++) {
  510. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  511. if ($band == '*') {
  512. $new['red'] = min(255, max(0, ($OriginalPixel['red'] - $range_min) * $range_scale));
  513. $new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale));
  514. $new['blue'] = min(255, max(0, ($OriginalPixel['blue'] - $range_min) * $range_scale));
  515. $new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale));
  516. } else {
  517. $new = $OriginalPixel;
  518. $new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale));
  519. }
  520. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']);
  521. ImageSetPixel($gdimg, $x, $y, $newColor);
  522. }
  523. }
  524. return true;
  525. }
  526. function HistogramOverlay(&$gdimg, $bands='*', $colors='', $width=0.25, $height=0.25, $alignment='BR', $opacity=50, $margin_x=5, $margin_y=null) {
  527. $margin_y = (is_null($margin_y) ? $margin_x : $margin_y);
  528. $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
  529. $histW = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
  530. $histH = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
  531. if ($gdHist = ImageCreateTrueColor($histW, $histH)) {
  532. $color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127);
  533. ImageFilledRectangle($gdHist, 0, 0, $histW, $histH, $color_back);
  534. ImageAlphaBlending($gdHist, false);
  535. ImageSaveAlpha($gdHist, true);
  536. $HistogramTempWidth = 256;
  537. $HistogramTempHeight = 100;
  538. if ($gdHistTemp = ImageCreateTrueColor($HistogramTempWidth, $HistogramTempHeight)) {
  539. $color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127);
  540. ImageAlphaBlending($gdHistTemp, false);
  541. ImageSaveAlpha($gdHistTemp, true);
  542. ImageFilledRectangle($gdHistTemp, 0, 0, ImageSX($gdHistTemp), ImageSY($gdHistTemp), $color_back_temp);
  543. $DefaultColors = array('r'=>'FF0000', 'g'=>'00FF00', 'b'=>'0000FF', 'a'=>'999999', '*'=>'FFFFFF');
  544. $Colors = explode(';', $colors);
  545. $BandsToGraph = array_unique(preg_split('//', $bands));
  546. $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
  547. foreach ($BandsToGraph as $key => $band) {
  548. if (!isset($keys[$band])) {
  549. continue;
  550. }
  551. $PeakValue = max($Analysis[$keys[$band]]);
  552. $thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]);
  553. for ($x = 0; $x < $HistogramTempWidth; $x++) {
  554. ImageLine($gdHistTemp, $x, $HistogramTempHeight - 1, $x, $HistogramTempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $HistogramTempHeight), $thisColor);
  555. }
  556. ImageLine($gdHistTemp, 0, $HistogramTempHeight - 1, $HistogramTempWidth - 1, $HistogramTempHeight - 1, $thisColor);
  557. ImageLine($gdHistTemp, 0, $HistogramTempHeight - 2, $HistogramTempWidth - 1, $HistogramTempHeight - 2, $thisColor);
  558. }
  559. ImageCopyResampled($gdHist, $gdHistTemp, 0, 0, 0, 0, ImageSX($gdHist), ImageSY($gdHist), ImageSX($gdHistTemp), ImageSY($gdHistTemp));
  560. ImageDestroy($gdHistTemp);
  561. } else {
  562. return false;
  563. }
  564. phpthumb_filters::WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin_x, $margin_y);
  565. ImageDestroy($gdHist);
  566. return true;
  567. }
  568. return false;
  569. }
  570. function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) {
  571. $border_width = ($border_width ? $border_width : 1);
  572. $radius_x = ($radius_x ? $radius_x : 0);
  573. $radius_y = ($radius_y ? $radius_y : 0);
  574. $output_width = ImageSX($gdimg);
  575. $output_height = ImageSY($gdimg);
  576. 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));
  577. $offset_x = ($radius_x ? $output_width - $new_width - $radius_x : 0);
  578. $offset_y = ($radius_y ? $output_height - $new_height - $radius_y : 0);
  579. //header('Content-Type: image/png');
  580. //ImagePNG($gdimg);
  581. //exit;
  582. if ($gd_border_canvas = phpthumb_functions::ImageCreateFunction($output_width, $output_height)) {
  583. ImageSaveAlpha($gd_border_canvas, true);
  584. ImageAlphaBlending($gd_border_canvas, false);
  585. $color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gd_border_canvas, 255, 255, 255, 127);
  586. ImageFilledRectangle($gd_border_canvas, 0, 0, $output_width, $output_height, $color_background);
  587. $color_border = phpthumb_functions::ImageHexColorAllocate($gd_border_canvas, (phpthumb_functions::IsHexColor($hexcolor_border) ? $hexcolor_border : '000000'));
  588. for ($i = 0; $i < $border_width; $i++) {
  589. ImageLine($gd_border_canvas, floor($offset_x / 2) + $radius_x, $i, $output_width - $radius_x - ceil($offset_x / 2), $i, $color_border); // top
  590. 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
  591. ImageLine($gd_border_canvas, floor($offset_x / 2) + $i, $radius_y, floor($offset_x / 2) + $i, $output_height - $radius_y, $color_border); // left
  592. 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
  593. }
  594. if ($radius_x && $radius_y) {
  595. // PHP bug: ImageArc() with thicknesses > 1 give bad/undesirable/unpredicatable results
  596. // Solution: Draw multiple 1px arcs side-by-side.
  597. // Problem: parallel arcs give strange/ugly antialiasing problems
  598. // Solution: draw non-parallel arcs, from one side of the line thickness at the start angle
  599. // to the opposite edge of the line thickness at the terminating angle
  600. for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
  601. 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
  602. 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
  603. 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
  604. 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
  605. }
  606. if ($border_width > 1) {
  607. for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
  608. 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
  609. 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
  610. 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
  611. 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
  612. }
  613. }
  614. }
  615. $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);
  616. ImageDestroy($gdimg);
  617. $gdimg = phpthumb_functions::ImageCreateFunction($output_width, $output_height);
  618. ImageSaveAlpha($gdimg, true);
  619. ImageAlphaBlending($gdimg, false);
  620. $gdimg_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 255, 255, 255, 127);
  621. ImageFilledRectangle($gdimg, 0, 0, $output_width, $output_height, $gdimg_color_background);
  622. ImageCopy($gdimg, $gd_border_canvas, 0, 0, 0, 0, $output_width, $output_height);
  623. //$gdimg = $gd_border_canvas;
  624. ImageDestroy($gd_border_canvas);
  625. return true;
  626. } else {
  627. $this->DebugMessage('FAILED: $gd_border_canvas = phpthumb_functions::ImageCreateFunction('.$output_width.', '.$output_height.')', __FILE__, __LINE__);
  628. }
  629. return false;
  630. }
  631. function ImprovedImageRotate(&$gdimg_source, $rotate_angle=0, $config_background_hexcolor='FFFFFF', $bg=null) {
  632. while ($rotate_angle < 0) {
  633. $rotate_angle += 360;
  634. }
  635. $rotate_angle = $rotate_angle % 360;
  636. if ($rotate_angle != 0) {
  637. $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_source, $config_background_hexcolor);
  638. if ((phpthumb_functions::gd_version() >= 2) && !$bg && ($rotate_angle % 90)) {
  639. //$this->DebugMessage('Using alpha rotate', __FILE__, __LINE__);
  640. if ($gdimg_rotate_mask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_source), ImageSY($gdimg_source))) {
  641. for ($i = 0; $i <= 255; $i++) {
  642. $color_mask[$i] = ImageColorAllocate($gdimg_rotate_mask, $i, $i, $i);
  643. }
  644. ImageFilledRectangle($gdimg_rotate_mask, 0, 0, ImageSX($gdimg_rotate_mask), ImageSY($gdimg_rotate_mask), $color_mask[255]);
  645. $imageX = ImageSX($gdimg_source);
  646. $imageY = ImageSY($gdimg_source);
  647. for ($x = 0; $x < $imageX; $x++) {
  648. for ($y = 0; $y < $imageY; $y++) {
  649. $pixelcolor = phpthumb_functions::GetPixelColor($gdimg_source, $x, $y);
  650. ImageSetPixel($gdimg_rotate_mask, $x, $y, $color_mask[255 - round($pixelcolor['alpha'] * 255 / 127)]);
  651. }
  652. }
  653. $gdimg_rotate_mask = ImageRotate($gdimg_rotate_mask, $rotate_angle, $color_mask[0]);
  654. $gdimg_source = ImageRotate($gdimg_source, $rotate_angle, $background_color);
  655. ImageAlphaBlending($gdimg_source, false);
  656. ImageSaveAlpha($gdimg_source, true);
  657. //$this->is_alpha = true;
  658. $phpThumbFilters = new phpthumb_filters();
  659. $phpThumbFilters->phpThumbObject = $this;
  660. $phpThumbFilters->ApplyMask($gdimg_rotate_mask, $gdimg_source);
  661. ImageDestroy($gdimg_rotate_mask);
  662. } else {
  663. //$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  664. }
  665. } else {
  666. if (phpthumb_functions::gd_version() < 2) {
  667. //$this->DebugMessage('Using non-alpha rotate because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  668. } elseif ($bg) {
  669. //$this->DebugMessage('Using non-alpha rotate because $this->bg is "'.$bg.'"', __FILE__, __LINE__);
  670. } elseif ($rotate_angle % 90) {
  671. //$this->DebugMessage('Using non-alpha rotate because ($rotate_angle % 90) = "'.($rotate_angle % 90).'"', __FILE__, __LINE__);
  672. } else {
  673. //$this->DebugMessage('Using non-alpha rotate because $this->thumbnailFormat is "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
  674. }
  675. if (ImageColorTransparent($gdimg_source) >= 0) {
  676. // ImageRotate() forgets all about an image's transparency and sets the transparent color to black
  677. // To compensate, flood-fill the transparent color of the source image with the specified background color first
  678. // then rotate and the colors should match
  679. if (!function_exists('ImageIsTrueColor') || !ImageIsTrueColor($gdimg_source)) {
  680. // convert paletted image to true-color before rotating to prevent nasty aliasing artifacts
  681. //$this->source_width = ImageSX($gdimg_source);
  682. //$this->source_height = ImageSY($gdimg_source);
  683. $gdimg_newsrc = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_source), ImageSY($gdimg_source));
  684. $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor);
  685. ImageFilledRectangle($gdimg_newsrc, 0, 0, ImageSX($gdimg_source), ImageSY($gdimg_source), phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor));
  686. ImageCopy($gdimg_newsrc, $gdimg_source, 0, 0, 0, 0, ImageSX($gdimg_source), ImageSY($gdimg_source));
  687. ImageDestroy($gdimg_source);
  688. unset($gdimg_source);
  689. $gdimg_source = $gdimg_newsrc;
  690. unset($gdimg_newsrc);
  691. } else {
  692. ImageColorSet(
  693. $gdimg_source,
  694. ImageColorTransparent($gdimg_source),
  695. hexdec(substr($config_background_hexcolor, 0, 2)),
  696. hexdec(substr($config_background_hexcolor, 2, 2)),
  697. hexdec(substr($config_background_hexcolor, 4, 2)));
  698. ImageColorTransparent($gdimg_source, -1);
  699. }
  700. }
  701. $gdimg_source = ImageRotate($gdimg_source, $rotate_angle, $background_color);
  702. }
  703. }
  704. return true;
  705. }
  706. function MeanRemoval(&$gdimg) {
  707. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  708. if (ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)) {
  709. return true;
  710. }
  711. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)', __FILE__, __LINE__);
  712. // fall through and try it the hard way
  713. }
  714. // currently not implemented "the hard way"
  715. $this->DebugMessage('FAILED: phpthumb_filters::MeanRemoval($gdimg) [function not implemented]', __FILE__, __LINE__);
  716. return false;
  717. }
  718. function Negative(&$gdimg) {
  719. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  720. if (ImageFilter($gdimg, IMG_FILTER_NEGATE)) {
  721. return true;
  722. }
  723. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_NEGATE)', __FILE__, __LINE__);
  724. // fall through and try it the hard way
  725. }
  726. $ImageSX = ImageSX($gdimg);
  727. $ImageSY = ImageSY($gdimg);
  728. for ($x = 0; $x < $ImageSX; $x++) {
  729. for ($y = 0; $y < $ImageSY; $y++) {
  730. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  731. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, (~$currentPixel['red'] & 0xFF), (~$currentPixel['green'] & 0xFF), (~$currentPixel['blue'] & 0xFF), $currentPixel['alpha']);
  732. ImageSetPixel($gdimg, $x, $y, $newColor);
  733. }
  734. }
  735. return true;
  736. }
  737. function RoundedImageCorners(&$gdimg, $radius_x, $radius_y) {
  738. // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
  739. // mask is generated as a white double-size elipse on a triple-size black background and copy-paste-resampled
  740. // onto a correct-size mask image as 4 corners due to errors when the entire mask is resampled at once (gray edges)
  741. if ($gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction($radius_x * 6, $radius_y * 6)) {
  742. if ($gdimg_cornermask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
  743. $color_transparent = ImageColorAllocate($gdimg_cornermask_triple, 255, 255, 255);
  744. ImageFilledEllipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent);
  745. ImageFilledRectangle($gdimg_cornermask, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $color_transparent);
  746. ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, 0, $radius_x, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
  747. 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);
  748. 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);
  749. 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);
  750. phpthumb_filters::ApplyMask($gdimg_cornermask, $gdimg);
  751. ImageDestroy($gdimg_cornermask);
  752. $this->DebugMessage('RoundedImageCorners('.$radius_x.', '.$radius_y.') succeeded', __FILE__, __LINE__);
  753. return true;
  754. } else {
  755. $this->DebugMessage('FAILED: $gdimg_cornermask = phpthumb_functions::ImageCreateFunction('.ImageSX($gdimg).', '.ImageSY($gdimg).')', __FILE__, __LINE__);
  756. }
  757. ImageDestroy($gdimg_cornermask_triple);
  758. } else {
  759. $this->DebugMessage('FAILED: $gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction('.($radius_x * 6).', '.($radius_y * 6).')', __FILE__, __LINE__);
  760. }
  761. return false;
  762. }
  763. function Saturation(&$gdimg, $amount, $color='') {
  764. if ($amount == 0) {
  765. return true;
  766. } elseif ($amount > 0) {
  767. $amount = 0 - $amount;
  768. } else {
  769. $amount = abs($amount);
  770. }
  771. return phpthumb_filters::Desaturate($gdimg, $amount, $color);
  772. }
  773. function Sepia(&$gdimg, $amount, $targetColor) {
  774. $amount = (is_numeric($amount) ? max(0, min(100, $amount)) : 50);
  775. $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'A28065');
  776. if ($amount == 0) {
  777. return true;
  778. }
  779. $TargetPixel['red'] = hexdec(substr($targetColor, 0, 2));
  780. $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
  781. $TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2));
  782. $ImageSX = ImageSX($gdimg);
  783. $ImageSY = ImageSY($gdimg);
  784. for ($x = 0; $x < $ImageSX; $x++) {
  785. for ($y = 0; $y < $ImageSY; $y++) {
  786. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  787. $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
  788. // http://www.gimpguru.org/Tutorials/SepiaToning/
  789. // "In the traditional sepia toning process, the tinting occurs most in
  790. // the mid-tones: the lighter and darker areas appear to be closer to B&W."
  791. $SepiaAmount = ((128 - abs($GrayPixel['red'] - 128)) / 128) * ($amount / 100);
  792. foreach ($TargetPixel as $key => $value) {
  793. $NewPixel[$key] = round(max(0, min(255, $GrayPixel[$key] * (1 - $SepiaAmount) + ($TargetPixel[$key] * $SepiaAmount))));
  794. }
  795. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
  796. ImageSetPixel($gdimg, $x, $y, $newColor);
  797. }
  798. }
  799. return true;
  800. }
  801. function Smooth(&$gdimg, $amount=6) {
  802. $amount = min(25, max(0, $amount));
  803. if ($amount == 0) {
  804. return true;
  805. }
  806. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  807. if (ImageFilter($gdimg, IMG_FILTER_SMOOTH, $amount)) {
  808. return true;
  809. }
  810. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SMOOTH, '.$amount.')', __FILE__, __LINE__);
  811. // fall through and try it the hard way
  812. }
  813. // currently not implemented "the hard way"
  814. $this->DebugMessage('FAILED: phpthumb_filters::Smooth($gdimg, '.$amount.') [function not implemented]', __FILE__, __LINE__);
  815. return false;
  816. }
  817. function Threshold(&$gdimg, $cutoff) {
  818. $cutoff = min(255, max(0, ($cutoff ? $cutoff : 128)));
  819. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  820. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  821. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  822. $grayPixel = phpthumb_functions::GrayscalePixel($currentPixel);
  823. if ($grayPixel['red'] < $cutoff) {
  824. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0x00, 0x00, 0x00, $currentPixel['alpha']);
  825. } else {
  826. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0xFF, 0xFF, 0xFF, $currentPixel['alpha']);
  827. }
  828. ImageSetPixel($gdimg, $x, $y, $newColor);
  829. }
  830. }
  831. return true;
  832. }
  833. function ImageTrueColorToPalette2(&$image, $dither, $ncolors) {
  834. // http://www.php.net/manual/en/function.imagetruecolortopalette.php
  835. // zmorris at zsculpt dot com (17-Aug-2004 06:58)
  836. $width = ImageSX($image);
  837. $height = ImageSY($image);
  838. $image_copy = ImageCreateTrueColor($width, $height);
  839. //ImageCopyMerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100);
  840. ImageCopy($image_copy, $image, 0, 0, 0, 0, $width, $height);
  841. ImageTrueColorToPalette($image, $dither, $ncolors);
  842. ImageColorMatch($image_copy, $image);
  843. ImageDestroy($image_copy);
  844. return true;
  845. }
  846. function ReduceColorDepth(&$gdimg, $colors=256, $dither=true) {
  847. $colors = max(min($colors, 256), 2);
  848. // ImageTrueColorToPalette usually makes ugly colors, the replacement is a bit better
  849. //ImageTrueColorToPalette($gdimg, $dither, $colors);
  850. phpthumb_filters::ImageTrueColorToPalette2($gdimg, $dither, $colors);
  851. return true;
  852. }
  853. function WhiteBalance(&$gdimg, $targetColor='') {
  854. if (phpthumb_functions::IsHexColor($targetColor)) {
  855. $targetPixel = array(
  856. 'red' => hexdec(substr($targetColor, 0, 2)),
  857. 'green' => hexdec(substr($targetColor, 2, 2)),
  858. 'blue' => hexdec(substr($targetColor, 4, 2))
  859. );
  860. } else {
  861. $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, false);
  862. $targetPixel = array(
  863. 'red' => max(array_keys($Analysis['red'])),
  864. 'green' => max(array_keys($Analysis['green'])),
  865. 'blue' => max(array_keys($Analysis['blue']))
  866. );
  867. }
  868. $grayValue = phpthumb_functions::GrayscaleValue($targetPixel['red'], $targetPixel['green'], $targetPixel['blue']);
  869. $scaleR = $grayValue / $targetPixel['red'];
  870. $scaleG = $grayValue / $targetPixel['green'];
  871. $scaleB = $grayValue / $targetPixel['blue'];
  872. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  873. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  874. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  875. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe(
  876. $gdimg,
  877. max(0, min(255, round($currentPixel['red'] * $scaleR))),
  878. max(0, min(255, round($currentPixel['green'] * $scaleG))),
  879. max(0, min(255, round($currentPixel['blue'] * $scaleB))),
  880. $currentPixel['alpha']
  881. );
  882. ImageSetPixel($gdimg, $x, $y, $newColor);
  883. }
  884. }
  885. return true;
  886. }
  887. function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color='000000', $ttffont='', $opacity=100, $margin=5, $angle=0, $bg_color=false, $bg_opacity=0, $fillextend='') {
  888. // text watermark requested
  889. if (!$text) {
  890. return false;
  891. }
  892. ImageAlphaBlending($gdimg, true);
  893. $metaTextArray = array(
  894. '^Fb' => $this->phpThumbObject->getimagesizeinfo['filesize'],
  895. '^Fk' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024),
  896. '^Fm' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576),
  897. '^X' => $this->phpThumbObject->getimagesizeinfo[0],
  898. '^Y' => $this->phpThumbObject->getimagesizeinfo[1],
  899. '^x' => ImageSX($gdimg),
  900. '^y' => ImageSY($gdimg),
  901. '^^' => '^',
  902. );
  903. $text = strtr($text, $metaTextArray);
  904. $text = str_replace("\r\n", "\n", $text);
  905. $text = str_replace("\r", "\n", $text);
  906. $textlines = explode("\n", $text);
  907. if (@is_readable($ttffont) && is_file($ttffont)) {
  908. $opacity = 100 - intval(max(min($opacity, 100), 0));
  909. $this->DebugMessage('Using TTF font "'.$ttffont.'"', __FILE__, __LINE__);
  910. $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
  911. $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
  912. $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
  913. //$text_width = round($max_x - $min_x + ($size * 0.5));
  914. $text_width = round($max_x - $min_x);
  915. $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
  916. $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
  917. //$text_height = round($max_y - $min_y + ($size * 0.5));
  918. $text_height = round($max_y - $min_y);
  919. $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'jH');
  920. $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
  921. $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
  922. $char_height = round($char_max_y - $char_min_y);
  923. switch ($alignment) {
  924. case 'T':
  925. $text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
  926. $text_origin_y = $char_height + $margin;
  927. break;
  928. case 'B':

Large files files are truncated, but you can click here to view the full file