PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/phpThumb/phpthumb.filters.php

http://github.com/Datawalke/Coordino
PHP | 1537 lines | 1176 code | 234 blank | 127 comment | 239 complexity | c1a5b1835008b193c5e43a8b46adf7df MD5 | raw file

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

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