PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/Vendor/phpThumb/phpthumb.filters.php

https://bitbucket.org/aaoliveira/cakephp-phpthumb
PHP | 1546 lines | 1182 code | 235 blank | 129 comment | 241 complexity | 900f2354095ab41bfde680d305f4f382 MD5 | raw 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, $alpha) {
  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) % 360;
  263. $alpha = max(0, min(100, ($alpha ? $alpha : 100)));
  264. if ($alpha <= 0) {
  265. // invisible shadow, nothing to do
  266. return true;
  267. }
  268. if ($distance <= 0) {
  269. // shadow completely obscured by source image, nothing to do
  270. return true;
  271. }
  272. $width_shadow = cos(deg2rad($angle)) * ($distance + $width);
  273. $height_shadow = sin(deg2rad($angle)) * ($distance + $width);
  274. $scaling = min(ImageSX($gdimg) / (ImageSX($gdimg) + abs($width_shadow)), ImageSY($gdimg) / (ImageSY($gdimg) + abs($height_shadow)));
  275. for ($i = 0; $i < $width; $i++) {
  276. $WidthAlpha[$i] = (abs(($width / 2) - $i) / $width);
  277. $Offset['x'] = cos(deg2rad($angle)) * ($distance + $i);
  278. $Offset['y'] = sin(deg2rad($angle)) * ($distance + $i);
  279. }
  280. $tempImageWidth = ImageSX($gdimg) + abs($Offset['x']);
  281. $tempImageHeight = ImageSY($gdimg) + abs($Offset['y']);
  282. if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) {
  283. ImageAlphaBlending($gdimg_dropshadow_temp, false);
  284. ImageSaveAlpha($gdimg_dropshadow_temp, true);
  285. $transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127);
  286. ImageFill($gdimg_dropshadow_temp, 0, 0, $transparent1);
  287. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  288. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  289. $PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  290. }
  291. }
  292. for ($x = 0; $x < $tempImageWidth; $x++) {
  293. for ($y = 0; $y < $tempImageHeight; $y++) {
  294. //for ($i = 0; $i < $width; $i++) {
  295. for ($i = 0; $i < 1; $i++) {
  296. if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
  297. if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) {
  298. $thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']);
  299. ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
  300. }
  301. }
  302. }
  303. }
  304. }
  305. ImageAlphaBlending($gdimg_dropshadow_temp, true);
  306. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  307. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  308. if ($PixelMap[$x][$y]['alpha'] < 127) {
  309. $thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
  310. ImageSetPixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
  311. }
  312. }
  313. }
  314. ImageSaveAlpha($gdimg, true);
  315. ImageAlphaBlending($gdimg, false);
  316. //$this->is_alpha = true;
  317. $transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127);
  318. ImageFilledRectangle($gdimg, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $transparent2);
  319. ImageCopyResampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg_dropshadow_temp), ImageSY($gdimg_dropshadow_temp));
  320. ImageDestroy($gdimg_dropshadow_temp);
  321. }
  322. return true;
  323. }
  324. function EdgeDetect(&$gdimg) {
  325. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  326. if (ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)) {
  327. return true;
  328. }
  329. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EDGEDETECT)', __FILE__, __LINE__);
  330. // fall through and try it the hard way
  331. }
  332. // currently not implemented "the hard way"
  333. $this->DebugMessage('FAILED: phpthumb_filters::EdgeDetect($gdimg) [function not implemented]', __FILE__, __LINE__);
  334. return false;
  335. }
  336. function Elipse($gdimg) {
  337. if (phpthumb_functions::gd_version() < 2) {
  338. return false;
  339. }
  340. // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
  341. if ($gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg) * 2, ImageSY($gdimg) * 2)) {
  342. if ($gdimg_elipsemask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
  343. $color_transparent = ImageColorAllocate($gdimg_elipsemask_double, 255, 255, 255);
  344. ImageFilledEllipse($gdimg_elipsemask_double, ImageSX($gdimg), ImageSY($gdimg), (ImageSX($gdimg) - 1) * 2, (ImageSY($gdimg) - 1) * 2, $color_transparent);
  345. ImageCopyResampled($gdimg_elipsemask, $gdimg_elipsemask_double, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg), ImageSX($gdimg) * 2, ImageSY($gdimg) * 2);
  346. phpthumb_filters::ApplyMask($gdimg_elipsemask, $gdimg);
  347. ImageDestroy($gdimg_elipsemask);
  348. return true;
  349. } else {
  350. $this->DebugMessage('$gdimg_elipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
  351. }
  352. ImageDestroy($gdimg_elipsemask_double);
  353. } else {
  354. $this->DebugMessage('$gdimg_elipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
  355. }
  356. return false;
  357. }
  358. function Emboss(&$gdimg) {
  359. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  360. if (ImageFilter($gdimg, IMG_FILTER_EMBOSS)) {
  361. return true;
  362. }
  363. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_EMBOSS)', __FILE__, __LINE__);
  364. // fall through and try it the hard way
  365. }
  366. // currently not implemented "the hard way"
  367. $this->DebugMessage('FAILED: phpthumb_filters::Emboss($gdimg) [function not implemented]', __FILE__, __LINE__);
  368. return false;
  369. }
  370. function Flip(&$gdimg, $x=false, $y=false) {
  371. if (!$x && !$y) {
  372. return false;
  373. }
  374. if ($tempImage = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
  375. if ($x) {
  376. ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
  377. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  378. ImageCopy($gdimg, $tempImage, ImageSX($gdimg) - 1 - $x, 0, $x, 0, 1, ImageSY($gdimg));
  379. }
  380. }
  381. if ($y) {
  382. ImageCopy($tempImage, $gdimg, 0, 0, 0, 0, ImageSX($gdimg), ImageSY($gdimg));
  383. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  384. ImageCopy($gdimg, $tempImage, 0, ImageSY($gdimg) - 1 - $y, 0, $y, ImageSX($gdimg), 1);
  385. }
  386. }
  387. ImageDestroy($tempImage);
  388. }
  389. return true;
  390. }
  391. function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) {
  392. $frame_width = ($frame_width ? $frame_width : 5);
  393. $edge_width = ($edge_width ? $edge_width : 1);
  394. $hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC');
  395. $hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
  396. $hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
  397. $color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame);
  398. $color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1);
  399. $color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2);
  400. for ($i = 0; $i < $edge_width; $i++) {
  401. // outer bevel
  402. ImageLine($gdimg, $i, $i, $i, ImageSY($gdimg) - $i, $color1); // left
  403. ImageLine($gdimg, $i, $i, ImageSX($gdimg) - $i, $i, $color1); // top
  404. ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, ImageSX($gdimg) - $i, $i, $color2); // right
  405. ImageLine($gdimg, ImageSX($gdimg) - $i, ImageSY($gdimg) - $i, $i, ImageSY($gdimg) - $i, $color2); // bottom
  406. }
  407. for ($i = 0; $i < $frame_width; $i++) {
  408. // actual frame
  409. ImageRectangle($gdimg, $edge_width + $i, $edge_width + $i, ImageSX($gdimg) - $edge_width - $i, ImageSY($gdimg) - $edge_width - $i, $color_frame);
  410. }
  411. for ($i = 0; $i < $edge_width; $i++) {
  412. // inner bevel
  413. 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
  414. 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
  415. 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
  416. 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
  417. }
  418. return true;
  419. }
  420. function Gamma(&$gdimg, $amount) {
  421. if (number_format($amount, 4) == '1.0000') {
  422. return true;
  423. }
  424. return ImageGammaCorrect($gdimg, 1.0, $amount);
  425. }
  426. function Grayscale(&$gdimg) {
  427. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  428. if (ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)) {
  429. return true;
  430. }
  431. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
  432. // fall through and try it the hard way
  433. }
  434. return phpthumb_filters::Colorize($gdimg, 100, 'gray');
  435. }
  436. function HistogramAnalysis(&$gdimg, $calculateGray=false) {
  437. $ImageSX = ImageSX($gdimg);
  438. $ImageSY = ImageSY($gdimg);
  439. for ($x = 0; $x < $ImageSX; $x++) {
  440. for ($y = 0; $y < $ImageSY; $y++) {
  441. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  442. @$Analysis['red'][$OriginalPixel['red']]++;
  443. @$Analysis['green'][$OriginalPixel['green']]++;
  444. @$Analysis['blue'][$OriginalPixel['blue']]++;
  445. @$Analysis['alpha'][$OriginalPixel['alpha']]++;
  446. if ($calculateGray) {
  447. $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
  448. @$Analysis['gray'][$GrayPixel['red']]++;
  449. }
  450. }
  451. }
  452. $keys = array('red', 'green', 'blue', 'alpha');
  453. if ($calculateGray) {
  454. $keys[] = 'gray';
  455. }
  456. foreach ($keys as $dummy => $key) {
  457. ksort($Analysis[$key]);
  458. }
  459. return $Analysis;
  460. }
  461. function HistogramStretch(&$gdimg, $band='*', $method=0, $threshold=0.1) {
  462. // equivalent of "Auto Contrast" in Adobe Photoshop
  463. // method 0 stretches according to RGB colors. Gives a more conservative stretch.
  464. // 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
  465. $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
  466. $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>(($method == 0) ? 'all' : 'gray'));
  467. $band = substr($band, 0, 1);
  468. if (!isset($keys[$band])) {
  469. return false;
  470. }
  471. $key = $keys[$band];
  472. // If the absolute brightest and darkest pixels are used then one random
  473. // pixel in the image could throw off the whole system. Instead, count up/down
  474. // from the limit and allow <threshold> (default = 0.1%) of brightest/darkest
  475. // pixels to be clipped to min/max
  476. $threshold = floatval($threshold) / 100;
  477. $clip_threshold = ImageSX($gdimg) * ImageSX($gdimg) * $threshold;
  478. //if ($min >= 0) {
  479. // $range_min = min($min, 255);
  480. //} else {
  481. $countsum = 0;
  482. for ($i = 0; $i <= 255; $i++) {
  483. if ($method == 0) {
  484. $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
  485. } else {
  486. $countsum += @$Analysis[$key][$i];
  487. }
  488. if ($countsum >= $clip_threshold) {
  489. $range_min = $i - 1;
  490. break;
  491. }
  492. }
  493. $range_min = max($range_min, 0);
  494. //}
  495. //if ($max > 0) {
  496. // $range_max = max($max, 255);
  497. //} else {
  498. $countsum = 0;
  499. for ($i = 255; $i >= 0; $i--) {
  500. if ($method == 0) {
  501. $countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
  502. } else {
  503. $countsum += @$Analysis[$key][$i];
  504. }
  505. if ($countsum >= $clip_threshold) {
  506. $range_max = $i + 1;
  507. break;
  508. }
  509. }
  510. $range_max = min($range_max, 255);
  511. //}
  512. $range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min)));
  513. if (($range_min == 0) && ($range_max == 255)) {
  514. // no adjustment neccesary - don't waste CPU time!
  515. return true;
  516. }
  517. $ImageSX = ImageSX($gdimg);
  518. $ImageSY = ImageSY($gdimg);
  519. for ($x = 0; $x < $ImageSX; $x++) {
  520. for ($y = 0; $y < $ImageSY; $y++) {
  521. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  522. if ($band == '*') {
  523. $new['red'] = min(255, max(0, ($OriginalPixel['red'] - $range_min) * $range_scale));
  524. $new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale));
  525. $new['blue'] = min(255, max(0, ($OriginalPixel['blue'] - $range_min) * $range_scale));
  526. $new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale));
  527. } else {
  528. $new = $OriginalPixel;
  529. $new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale));
  530. }
  531. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']);
  532. ImageSetPixel($gdimg, $x, $y, $newColor);
  533. }
  534. }
  535. return true;
  536. }
  537. function HistogramOverlay(&$gdimg, $bands='*', $colors='', $width=0.25, $height=0.25, $alignment='BR', $opacity=50, $margin_x=5, $margin_y=null) {
  538. $margin_y = (is_null($margin_y) ? $margin_x : $margin_y);
  539. $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, true);
  540. $histW = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
  541. $histH = round(($width > 1) ? min($width, ImageSX($gdimg)) : ImageSX($gdimg) * $width);
  542. if ($gdHist = ImageCreateTrueColor($histW, $histH)) {
  543. $color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127);
  544. ImageFilledRectangle($gdHist, 0, 0, $histW, $histH, $color_back);
  545. ImageAlphaBlending($gdHist, false);
  546. ImageSaveAlpha($gdHist, true);
  547. $HistogramTempWidth = 256;
  548. $HistogramTempHeight = 100;
  549. if ($gdHistTemp = ImageCreateTrueColor($HistogramTempWidth, $HistogramTempHeight)) {
  550. $color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127);
  551. ImageAlphaBlending($gdHistTemp, false);
  552. ImageSaveAlpha($gdHistTemp, true);
  553. ImageFilledRectangle($gdHistTemp, 0, 0, ImageSX($gdHistTemp), ImageSY($gdHistTemp), $color_back_temp);
  554. $DefaultColors = array('r'=>'FF0000', 'g'=>'00FF00', 'b'=>'0000FF', 'a'=>'999999', '*'=>'FFFFFF');
  555. $Colors = explode(';', $colors);
  556. $BandsToGraph = array_unique(preg_split('##', $bands));
  557. $keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
  558. foreach ($BandsToGraph as $key => $band) {
  559. if (!isset($keys[$band])) {
  560. continue;
  561. }
  562. $PeakValue = max($Analysis[$keys[$band]]);
  563. $thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]);
  564. for ($x = 0; $x < $HistogramTempWidth; $x++) {
  565. ImageLine($gdHistTemp, $x, $HistogramTempHeight - 1, $x, $HistogramTempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $HistogramTempHeight), $thisColor);
  566. }
  567. ImageLine($gdHistTemp, 0, $HistogramTempHeight - 1, $HistogramTempWidth - 1, $HistogramTempHeight - 1, $thisColor);
  568. ImageLine($gdHistTemp, 0, $HistogramTempHeight - 2, $HistogramTempWidth - 1, $HistogramTempHeight - 2, $thisColor);
  569. }
  570. ImageCopyResampled($gdHist, $gdHistTemp, 0, 0, 0, 0, ImageSX($gdHist), ImageSY($gdHist), ImageSX($gdHistTemp), ImageSY($gdHistTemp));
  571. ImageDestroy($gdHistTemp);
  572. } else {
  573. return false;
  574. }
  575. phpthumb_filters::WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin_x, $margin_y);
  576. ImageDestroy($gdHist);
  577. return true;
  578. }
  579. return false;
  580. }
  581. function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) {
  582. $border_width = ($border_width ? $border_width : 1);
  583. $radius_x = ($radius_x ? $radius_x : 0);
  584. $radius_y = ($radius_y ? $radius_y : 0);
  585. $output_width = ImageSX($gdimg);
  586. $output_height = ImageSY($gdimg);
  587. 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));
  588. $offset_x = ($radius_x ? $output_width - $new_width - $radius_x : 0);
  589. $offset_y = ($radius_y ? $output_height - $new_height - $radius_y : 0);
  590. //header('Content-Type: image/png');
  591. //ImagePNG($gdimg);
  592. //exit;
  593. if ($gd_border_canvas = phpthumb_functions::ImageCreateFunction($output_width, $output_height)) {
  594. ImageSaveAlpha($gd_border_canvas, true);
  595. ImageAlphaBlending($gd_border_canvas, false);
  596. $color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gd_border_canvas, 255, 255, 255, 127);
  597. ImageFilledRectangle($gd_border_canvas, 0, 0, $output_width, $output_height, $color_background);
  598. $color_border = phpthumb_functions::ImageHexColorAllocate($gd_border_canvas, (phpthumb_functions::IsHexColor($hexcolor_border) ? $hexcolor_border : '000000'));
  599. for ($i = 0; $i < $border_width; $i++) {
  600. ImageLine($gd_border_canvas, floor($offset_x / 2) + $radius_x, $i, $output_width - $radius_x - ceil($offset_x / 2), $i, $color_border); // top
  601. 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
  602. ImageLine($gd_border_canvas, floor($offset_x / 2) + $i, $radius_y, floor($offset_x / 2) + $i, $output_height - $radius_y, $color_border); // left
  603. 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
  604. }
  605. if ($radius_x && $radius_y) {
  606. // PHP bug: ImageArc() with thicknesses > 1 give bad/undesirable/unpredicatable results
  607. // Solution: Draw multiple 1px arcs side-by-side.
  608. // Problem: parallel arcs give strange/ugly antialiasing problems
  609. // Solution: draw non-parallel arcs, from one side of the line thickness at the start angle
  610. // to the opposite edge of the line thickness at the terminating angle
  611. for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
  612. 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
  613. 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
  614. 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
  615. 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
  616. }
  617. if ($border_width > 1) {
  618. for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
  619. 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
  620. 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
  621. 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
  622. 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
  623. }
  624. }
  625. }
  626. $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);
  627. ImageDestroy($gdimg);
  628. $gdimg = phpthumb_functions::ImageCreateFunction($output_width, $output_height);
  629. ImageSaveAlpha($gdimg, true);
  630. ImageAlphaBlending($gdimg, false);
  631. $gdimg_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 255, 255, 255, 127);
  632. ImageFilledRectangle($gdimg, 0, 0, $output_width, $output_height, $gdimg_color_background);
  633. ImageCopy($gdimg, $gd_border_canvas, 0, 0, 0, 0, $output_width, $output_height);
  634. //$gdimg = $gd_border_canvas;
  635. ImageDestroy($gd_border_canvas);
  636. return true;
  637. } else {
  638. $this->DebugMessage('FAILED: $gd_border_canvas = phpthumb_functions::ImageCreateFunction('.$output_width.', '.$output_height.')', __FILE__, __LINE__);
  639. }
  640. return false;
  641. }
  642. function ImprovedImageRotate(&$gdimg_source, $rotate_angle=0, $config_background_hexcolor='FFFFFF', $bg=null) {
  643. while ($rotate_angle < 0) {
  644. $rotate_angle += 360;
  645. }
  646. $rotate_angle = $rotate_angle % 360;
  647. if ($rotate_angle != 0) {
  648. $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_source, $config_background_hexcolor);
  649. if ((phpthumb_functions::gd_version() >= 2) && !$bg && ($rotate_angle % 90)) {
  650. //$this->DebugMessage('Using alpha rotate', __FILE__, __LINE__);
  651. if ($gdimg_rotate_mask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_source), ImageSY($gdimg_source))) {
  652. for ($i = 0; $i <= 255; $i++) {
  653. $color_mask[$i] = ImageColorAllocate($gdimg_rotate_mask, $i, $i, $i);
  654. }
  655. ImageFilledRectangle($gdimg_rotate_mask, 0, 0, ImageSX($gdimg_rotate_mask), ImageSY($gdimg_rotate_mask), $color_mask[255]);
  656. $imageX = ImageSX($gdimg_source);
  657. $imageY = ImageSY($gdimg_source);
  658. for ($x = 0; $x < $imageX; $x++) {
  659. for ($y = 0; $y < $imageY; $y++) {
  660. $pixelcolor = phpthumb_functions::GetPixelColor($gdimg_source, $x, $y);
  661. ImageSetPixel($gdimg_rotate_mask, $x, $y, $color_mask[255 - round($pixelcolor['alpha'] * 255 / 127)]);
  662. }
  663. }
  664. $gdimg_rotate_mask = ImageRotate($gdimg_rotate_mask, $rotate_angle, $color_mask[0]);
  665. $gdimg_source = ImageRotate($gdimg_source, $rotate_angle, $background_color);
  666. ImageAlphaBlending($gdimg_source, false);
  667. ImageSaveAlpha($gdimg_source, true);
  668. //$this->is_alpha = true;
  669. $phpThumbFilters = new phpthumb_filters();
  670. $phpThumbFilters->phpThumbObject = $this;
  671. $phpThumbFilters->ApplyMask($gdimg_rotate_mask, $gdimg_source);
  672. ImageDestroy($gdimg_rotate_mask);
  673. } else {
  674. //$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  675. }
  676. } else {
  677. if (phpthumb_functions::gd_version() < 2) {
  678. //$this->DebugMessage('Using non-alpha rotate because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  679. } elseif ($bg) {
  680. //$this->DebugMessage('Using non-alpha rotate because $this->bg is "'.$bg.'"', __FILE__, __LINE__);
  681. } elseif ($rotate_angle % 90) {
  682. //$this->DebugMessage('Using non-alpha rotate because ($rotate_angle % 90) = "'.($rotate_angle % 90).'"', __FILE__, __LINE__);
  683. } else {
  684. //$this->DebugMessage('Using non-alpha rotate because $this->thumbnailFormat is "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
  685. }
  686. if (ImageColorTransparent($gdimg_source) >= 0) {
  687. // ImageRotate() forgets all about an image's transparency and sets the transparent color to black
  688. // To compensate, flood-fill the transparent color of the source image with the specified background color first
  689. // then rotate and the colors should match
  690. if (!function_exists('ImageIsTrueColor') || !ImageIsTrueColor($gdimg_source)) {
  691. // convert paletted image to true-color before rotating to prevent nasty aliasing artifacts
  692. //$this->source_width = ImageSX($gdimg_source);
  693. //$this->source_height = ImageSY($gdimg_source);
  694. $gdimg_newsrc = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg_source), ImageSY($gdimg_source));
  695. $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor);
  696. ImageFilledRectangle($gdimg_newsrc, 0, 0, ImageSX($gdimg_source), ImageSY($gdimg_source), phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor));
  697. ImageCopy($gdimg_newsrc, $gdimg_source, 0, 0, 0, 0, ImageSX($gdimg_source), ImageSY($gdimg_source));
  698. ImageDestroy($gdimg_source);
  699. unset($gdimg_source);
  700. $gdimg_source = $gdimg_newsrc;
  701. unset($gdimg_newsrc);
  702. } else {
  703. ImageColorSet(
  704. $gdimg_source,
  705. ImageColorTransparent($gdimg_source),
  706. hexdec(substr($config_background_hexcolor, 0, 2)),
  707. hexdec(substr($config_background_hexcolor, 2, 2)),
  708. hexdec(substr($config_background_hexcolor, 4, 2)));
  709. ImageColorTransparent($gdimg_source, -1);
  710. }
  711. }
  712. $gdimg_source = ImageRotate($gdimg_source, $rotate_angle, $background_color);
  713. }
  714. }
  715. return true;
  716. }
  717. function MeanRemoval(&$gdimg) {
  718. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  719. if (ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)) {
  720. return true;
  721. }
  722. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_MEAN_REMOVAL)', __FILE__, __LINE__);
  723. // fall through and try it the hard way
  724. }
  725. // currently not implemented "the hard way"
  726. $this->DebugMessage('FAILED: phpthumb_filters::MeanRemoval($gdimg) [function not implemented]', __FILE__, __LINE__);
  727. return false;
  728. }
  729. function Negative(&$gdimg) {
  730. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  731. if (ImageFilter($gdimg, IMG_FILTER_NEGATE)) {
  732. return true;
  733. }
  734. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_NEGATE)', __FILE__, __LINE__);
  735. // fall through and try it the hard way
  736. }
  737. $ImageSX = ImageSX($gdimg);
  738. $ImageSY = ImageSY($gdimg);
  739. for ($x = 0; $x < $ImageSX; $x++) {
  740. for ($y = 0; $y < $ImageSY; $y++) {
  741. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  742. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, (~$currentPixel['red'] & 0xFF), (~$currentPixel['green'] & 0xFF), (~$currentPixel['blue'] & 0xFF), $currentPixel['alpha']);
  743. ImageSetPixel($gdimg, $x, $y, $newColor);
  744. }
  745. }
  746. return true;
  747. }
  748. function RoundedImageCorners(&$gdimg, $radius_x, $radius_y) {
  749. // generate mask at twice desired resolution and downsample afterwards for easy antialiasing
  750. // mask is generated as a white double-size elipse on a triple-size black background and copy-paste-resampled
  751. // onto a correct-size mask image as 4 corners due to errors when the entire mask is resampled at once (gray edges)
  752. if ($gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction($radius_x * 6, $radius_y * 6)) {
  753. if ($gdimg_cornermask = phpthumb_functions::ImageCreateFunction(ImageSX($gdimg), ImageSY($gdimg))) {
  754. $color_transparent = ImageColorAllocate($gdimg_cornermask_triple, 255, 255, 255);
  755. ImageFilledEllipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent);
  756. ImageFilledRectangle($gdimg_cornermask, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $color_transparent);
  757. ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, 0, $radius_x, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
  758. 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);
  759. 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);
  760. 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);
  761. phpthumb_filters::ApplyMask($gdimg_cornermask, $gdimg);
  762. ImageDestroy($gdimg_cornermask);
  763. $this->DebugMessage('RoundedImageCorners('.$radius_x.', '.$radius_y.') succeeded', __FILE__, __LINE__);
  764. return true;
  765. } else {
  766. $this->DebugMessage('FAILED: $gdimg_cornermask = phpthumb_functions::ImageCreateFunction('.ImageSX($gdimg).', '.ImageSY($gdimg).')', __FILE__, __LINE__);
  767. }
  768. ImageDestroy($gdimg_cornermask_triple);
  769. } else {
  770. $this->DebugMessage('FAILED: $gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction('.($radius_x * 6).', '.($radius_y * 6).')', __FILE__, __LINE__);
  771. }
  772. return false;
  773. }
  774. function Saturation(&$gdimg, $amount, $color='') {
  775. if ($amount == 0) {
  776. return true;
  777. } elseif ($amount > 0) {
  778. $amount = 0 - $amount;
  779. } else {
  780. $amount = abs($amount);
  781. }
  782. return phpthumb_filters::Desaturate($gdimg, $amount, $color);
  783. }
  784. function Sepia(&$gdimg, $amount, $targetColor) {
  785. $amount = (is_numeric($amount) ? max(0, min(100, $amount)) : 50);
  786. $amountPct = $amount / 100;
  787. $targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'A28065');
  788. if ($amount == 0) {
  789. return true;
  790. }
  791. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  792. if (ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)) {
  793. $r = round($amountPct * hexdec(substr($targetColor, 0, 2)));
  794. $g = round($amountPct * hexdec(substr($targetColor, 2, 2)));
  795. $b = round($amountPct * hexdec(substr($targetColor, 4, 2)));
  796. if (ImageFilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
  797. return true;
  798. }
  799. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
  800. // fall through and try it the hard way
  801. } else {
  802. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
  803. // fall through and try it the hard way
  804. }
  805. }
  806. $TargetPixel['red'] = hexdec(substr($targetColor, 0, 2));
  807. $TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
  808. $TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2));
  809. $ImageSX = ImageSX($gdimg);
  810. $ImageSY = ImageSY($gdimg);
  811. for ($x = 0; $x < $ImageSX; $x++) {
  812. for ($y = 0; $y < $ImageSY; $y++) {
  813. $OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  814. $GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
  815. // http://www.gimpguru.org/Tutorials/SepiaToning/
  816. // "In the traditional sepia toning process, the tinting occurs most in
  817. // the mid-tones: the lighter and darker areas appear to be closer to B&W."
  818. $SepiaAmount = ((128 - abs($GrayPixel['red'] - 128)) / 128) * $amountPct;
  819. foreach ($TargetPixel as $key => $value) {
  820. $NewPixel[$key] = round(max(0, min(255, $GrayPixel[$key] * (1 - $SepiaAmount) + ($TargetPixel[$key] * $SepiaAmount))));
  821. }
  822. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
  823. ImageSetPixel($gdimg, $x, $y, $newColor);
  824. }
  825. }
  826. return true;
  827. }
  828. function Smooth(&$gdimg, $amount=6) {
  829. $amount = min(25, max(0, $amount));
  830. if ($amount == 0) {
  831. return true;
  832. }
  833. if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  834. if (ImageFilter($gdimg, IMG_FILTER_SMOOTH, $amount)) {
  835. return true;
  836. }
  837. $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SMOOTH, '.$amount.')', __FILE__, __LINE__);
  838. // fall through and try it the hard way
  839. }
  840. // currently not implemented "the hard way"
  841. $this->DebugMessage('FAILED: phpthumb_filters::Smooth($gdimg, '.$amount.') [function not implemented]', __FILE__, __LINE__);
  842. return false;
  843. }
  844. function SourceTransparentColorMask(&$gdimg, $hexcolor, $min_limit=5, $max_limit=10) {
  845. $width = ImageSX($gdimg);
  846. $height = ImageSY($gdimg);
  847. if ($gdimg_mask = ImageCreateTrueColor($width, $height)) {
  848. $R = hexdec(substr($hexcolor, 0, 2));
  849. $G = hexdec(substr($hexcolor, 2, 2));
  850. $B = hexdec(substr($hexcolor, 4, 2));
  851. $targetPixel = array('red'=>$R, 'green'=>$G, 'blue'=>$B);
  852. $cutoffRange = $max_limit - $min_limit;
  853. for ($x = 0; $x < $width; $x++) {
  854. for ($y = 0; $y < $height; $y++) {
  855. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  856. $colorDiff = phpthumb_functions::PixelColorDifferencePercent($currentPixel, $targetPixel);
  857. $grayLevel = min($cutoffRange, MAX(0, -$min_limit + $colorDiff)) * (255 / MAX(1, $cutoffRange));
  858. $newColor = ImageColorAllocate($gdimg_mask, $grayLevel, $grayLevel, $grayLevel);
  859. ImageSetPixel($gdimg_mask, $x, $y, $newColor);
  860. }
  861. }
  862. return $gdimg_mask;
  863. }
  864. return false;
  865. }
  866. function Threshold(&$gdimg, $cutoff) {
  867. $width = ImageSX($gdimg);
  868. $height = ImageSY($gdimg);
  869. $cutoff = min(255, max(0, ($cutoff ? $cutoff : 128)));
  870. for ($x = 0; $x < $width; $x++) {
  871. for ($y = 0; $y < $height; $y++) {
  872. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  873. $grayPixel = phpthumb_functions::GrayscalePixel($currentPixel);
  874. if ($grayPixel['red'] < $cutoff) {
  875. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0x00, 0x00, 0x00, $currentPixel['alpha']);
  876. } else {
  877. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0xFF, 0xFF, 0xFF, $currentPixel['alpha']);
  878. }
  879. ImageSetPixel($gdimg, $x, $y, $newColor);
  880. }
  881. }
  882. return true;
  883. }
  884. function ImageTrueColorToPalette2(&$image, $dither, $ncolors) {
  885. // http://www.php.net/manual/en/function.imagetruecolortopalette.php
  886. // zmorris at zsculpt dot com (17-Aug-2004 06:58)
  887. $width = ImageSX($image);
  888. $height = ImageSY($image);
  889. $image_copy = ImageCreateTrueColor($width, $height);
  890. //ImageCopyMerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100);
  891. ImageCopy($image_copy, $image, 0, 0, 0, 0, $width, $height);
  892. ImageTrueColorToPalette($image, $dither, $ncolors);
  893. ImageColorMatch($image_copy, $image);
  894. ImageDestroy($image_copy);
  895. return true;
  896. }
  897. function ReduceColorDepth(&$gdimg, $colors=256, $dither=true) {
  898. $colors = max(min($colors, 256), 2);
  899. // ImageTrueColorToPalette usually makes ugly colors, the replacement is a bit better
  900. //ImageTrueColorToPalette($gdimg, $dither, $colors);
  901. phpthumb_filters::ImageTrueColorToPalette2($gdimg, $dither, $colors);
  902. return true;
  903. }
  904. function WhiteBalance(&$gdimg, $targetColor='') {
  905. if (phpthumb_functions::IsHexColor($targetColor)) {
  906. $targetPixel = array(
  907. 'red' => hexdec(substr($targetColor, 0, 2)),
  908. 'green' => hexdec(substr($targetColor, 2, 2)),
  909. 'blue' => hexdec(substr($targetColor, 4, 2))
  910. );
  911. } else {
  912. $Analysis = phpthumb_filters::HistogramAnalysis($gdimg, false);
  913. $targetPixel = array(
  914. 'red' => max(array_keys($Analysis['red'])),
  915. 'green' => max(array_keys($Analysis['green'])),
  916. 'blue' => max(array_keys($Analysis['blue']))
  917. );
  918. }
  919. $grayValue = phpthumb_functions::GrayscaleValue($targetPixel['red'], $targetPixel['green'], $targetPixel['blue']);
  920. $scaleR = $grayValue / $targetPixel['red'];
  921. $scaleG = $grayValue / $targetPixel['green'];
  922. $scaleB = $grayValue / $targetPixel['blue'];
  923. for ($x = 0; $x < ImageSX($gdimg); $x++) {
  924. for ($y = 0; $y < ImageSY($gdimg); $y++) {
  925. $currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
  926. $newColor = phpthumb_functions::ImageColorAllocateAlphaSafe(
  927. $gdimg,
  928. max(0, min(255, round($currentPixel['red'] * $scaleR))),
  929. max(0, min(255, round($currentPixel['green'] * $scaleG))),
  930. max(0, min(255, round($currentPixel['blue'] * $scaleB))),
  931. $currentPixel['alpha']
  932. );
  933. ImageSetPixel($gdimg, $x, $y, $newColor);
  934. }
  935. }
  936. return true;
  937. }
  938. function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color='000000', $ttffont='', $opacity=100, $margin=5, $angle=0, $bg_color=false, $bg_opacity=0, $fillextend='') {
  939. // text watermark requested
  940. if (!$text) {
  941. return false;
  942. }
  943. ImageAlphaBlending($gdimg, true);
  944. if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)(@[LCR])?$#i', $alignment, $matches)) {
  945. $originOffsetX = intval($matches[1]);
  946. $originOffsetY = intval($matches[2]);
  947. $alignment = (@$matches[4] ? $matches[4] : 'L');
  948. $margin = 0;
  949. } else {
  950. $originOffsetX = 0;
  951. $originOffsetY = 0;
  952. }
  953. $metaTextArray = array(
  954. '^Fb' => $this->phpThumbObject->getimagesizeinfo['filesize'],
  955. '^Fk' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024),
  956. '^Fm' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576),
  957. '^X' => $this->phpThumbObject->getimagesizeinfo[0],
  958. '^Y' => $this->phpThumbObject->getimagesizeinfo[1],
  959. '^x' => ImageSX($gdimg),
  960. '^y' => ImageSY($gdimg),
  961. '^^' => '^',
  962. );
  963. $text = strtr($text, $metaTextArray);
  964. $text = str_replace("\r\n", "\n", $text);
  965. $text = str_replace("\r", "\n", $text);
  966. $textlines = explode("\n", $text);
  967. $this->DebugMessage('Processing '.count($textlines).' lines of text', __FILE__, __LINE__);
  968. if (@is_readable($ttffont) && is_file($ttffont)) {
  969. $opacity = 100 - intval(max(min($opacity, 100), 0));
  970. $letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
  971. $this->DebugMessage('Using TTF font "'.$ttffont.'"', __FILE__, __LINE__);
  972. $TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
  973. $min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
  974. $max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
  975. //$text_width = round($max_x - $min_x + ($size * 0.5));
  976. $text_width = round($max_x - $min_x);
  977. $min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
  978. $max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
  979. //$text_height = round($max_y - $min_y + ($size * 0.5));
  980. $text_height = round($max_y - $min_y);
  981. $TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'jH');
  982. $char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
  983. $char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
  984. $char_height = round($char_max_y - $char_min_y);
  985. if ($alignment == '*') {
  986. $text_origin_y = $char_height + $margin;
  987. while (($text_origin_y - $text_height) < ImageSY($gdimg)) {
  988. $text_origin_x = $margin;
  989. while ($text_origin_x < ImageSX($gdimg)) {
  990. ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
  991. $text_origin_x += ($text_width + $margin);
  992. }
  993. $text_origin_y += ($text_height + $margin);
  994. }
  995. } else {
  996. // this block for background color only
  997. switch ($alignment) {
  998. case '*':
  999. // handled separately
  1000. break;
  1001. case 'T':
  1002. $text_origin_x = ($originOffsetX ? $originOffsetX - round($text_width / 2) : round((ImageSX($gdimg) - $text_width) / 2));
  1003. $text_origin_y = $char_height + $margin + $originOffsetY;
  1004. break;
  1005. case 'B':
  1006. $text_origin_x = ($originOffsetX ? $originOffsetX - round($text_width / 2) : round((ImageSX($gdimg) - $text_width) / 2));
  1007. $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin + $originOffsetY;
  1008. break;
  1009. case 'L':
  1010. $text_origin_x = $margin + $originOffsetX;
  1011. $text_origin_y = ($originOffsetY ? $originOffsetY : round((ImageSY($gdimg) - $text_height) / 2) + $char_height);
  1012. break;
  1013. case 'R':
  1014. $text_origin_x = ($originOffsetX ? $originOffsetX - $text_width : ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin);
  1015. $text_origin_y = ($originOffsetY ? $originOffsetY : round((ImageSY($gdimg) - $text_height) / 2) + $char_height);
  1016. break;
  1017. case 'C':
  1018. $text_origin_x = ($originOffsetX ? $originOffsetX - round($text_width / 2) : round((ImageSX($gdimg) - $text_width) / 2));
  1019. $text_origin_y = ($originOffsetY ? $originOffsetY : round((ImageSY($gdimg) - $text_height) / 2) + $char_height);
  1020. break;
  1021. case 'TL':
  1022. $text_origin_x = $margin + $originOffsetX;
  1023. $text_origin_y = $char_height + $margin + $originOffsetY;
  1024. break;
  1025. case 'TR':
  1026. $text_origin_x = ($originOffsetX ? $originOffsetX - $text_width : ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin);
  1027. $text_origin_y = $char_height + $margin + $originOffsetY;
  1028. break;
  1029. case 'BL':
  1030. $text_origin_x = $margin + $originOffsetX;
  1031. $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin + $originOffsetY;
  1032. break;
  1033. case 'BR':
  1034. default:
  1035. $text_origin_x = ($originOffsetX ? $originOffsetX - $text_width : ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin);
  1036. $text_origin_y = ImageSY($gdimg) + $TTFbox[1] - $margin + $originOffsetY;
  1037. break;
  1038. }
  1039. //ImageRectangle($gdimg, $text_origin_x + $min_x, $text_origin_y + $TTFbox[1], $text_origin_x + $min_x + $text_width, $text_origin_y + $TTFbox[1] - $text_height, $letter_color_text);
  1040. if (phpthumb_functions::IsHexColor($bg_color)) {
  1041. $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
  1042. $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, $bg_color, false, $text_background_alpha);
  1043. } else {
  1044. $text_color_background = phpthumb_functions::ImageHexColorAllocate($gdimg, 'FFFFFF', false, 127);
  1045. }
  1046. $x1 = $text_origin_x + $min_x;
  1047. $y1 = $text_origin_y + $TTFbox[1];
  1048. $x2 = $text_origin_x + $min_x + $text_width;
  1049. $y2 = $text_origin_y + $TTFbox[1] - $text_height;
  1050. $x_TL = preg_match('#x#i', $fillextend) ? 0 : min($x1, $x2);
  1051. $y_TL = preg_match('#y#i', $fillextend) ? 0 : min($y1, $y2);
  1052. $x_BR = preg_match('#x#i', $fillextend) ? ImageSX($gdimg) : max($x1, $x2);
  1053. $y_BR = preg_match('#y#i', $fillextend) ? ImageSY($gdimg) : max($y1, $y2);
  1054. //while ($y_BR > ImageSY($gdimg)) {
  1055. // $y_TL--;
  1056. // $y_BR--;
  1057. // $text_origin_y--;
  1058. //}
  1059. $this->DebugMessage('WatermarkText() calling ImageFilledRectangle($gdimg, '.$x_TL.', '.$y_TL.', '.$x_BR.', '.$y_BR.', $text_color_background)', __FILE__, __LINE__);
  1060. ImageFilledRectangle($gdimg, $x_TL, $y_TL, $x_BR, $y_BR, $text_color_background);
  1061. // end block for background color only
  1062. $y_offset = 0;
  1063. foreach ($textlines as $dummy => $line) {
  1064. $TTFboxLine = ImageTTFbBox($size, $angle, $ttffont, $line);
  1065. $min_x_line = min($TTFboxLine[0], $TTFboxLine[2], $TTFboxLine[4], $TTFboxLine[6]);
  1066. $max_x_line = max($TTFboxLine[0], $TTFboxLine[2], $TTFboxLine[4], $TTFboxLine[6]);
  1067. //$text_width = round($max_x - $min_x + ($size * 0.5));
  1068. $text_width_line = round($max_x_line - $min_x_line);
  1069. $min_y_line = min($TTFboxLine[1], $TTFboxLine[3], $TTFboxLine[5], $TTFboxLine[7]);
  1070. $max_y_line = max($TTFboxLine[1], $TTFboxLine[3], $TTFboxLine[5], $TTFboxLine[7]);
  1071. //$text_height = round($max_y - $min_y + ($size * 0.5));
  1072. $text_height_line = round($max_y_line - $min_y_line);
  1073. switch ($alignment) {
  1074. // $text_origin_y set above, just re-set $text_origin_x here as needed
  1075. case 'L':
  1076. case 'TL':
  1077. case 'BL':
  1078. // no change neccesary
  1079. break;
  1080. case 'C':
  1081. case 'T':
  1082. case 'B':
  1083. $text_origin_x = ($originOffsetX ? $originOffsetX - round($text_width_line / 2) : round((ImageSX($gdimg) - $text_width_line) / 2));
  1084. break;
  1085. case 'R':
  1086. case 'TR':
  1087. case 'BR':
  1088. $text_origin_x = ($originOffsetX ? $originOffsetX - $text_width_line : ImageSX($gdimg) - $text_width_line + $TTFbox[0] - $min_x + round($size * 0.25) - $margin);
  1089. break;
  1090. }
  1091. //ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
  1092. $this->DebugMessage('WatermarkText() calling ImageTTFtext($gdimg, '.$size.', '.$angle.', '.$text_origin_x.', '.($text_origin_y + $y_offset).', $letter_color_text, '.$ttffont.', '.$line.')', __FILE__, __LINE__);
  1093. ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y + $y_offset, $letter_color_text, $ttffont, $line);
  1094. $y_offset += $char_height;
  1095. }
  1096. }
  1097. return true;
  1098. } else {
  1099. $size = min(5, max(1, $size));
  1100. $this->DebugMessage('Using built-in font (size='.$size.') for text watermark'.($ttffont ? ' because $ttffont !is_readable('.$ttffont.')' : ''), __FILE__, __LINE__);
  1101. $text_width = 0;
  1102. $text_height = 0;
  1103. foreach ($textlines as $dummy => $line) {
  1104. $text_width = max($text_width, ImageFontWidth($size) * strlen($line));
  1105. $text_height += ImageFontHeight($size);
  1106. }
  1107. if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
  1108. ImageAlphaBlending($img_watermark, false);
  1109. if (phpthumb_functions::IsHexColor($bg_color)) {
  1110. $text_background_alpha = round(127 * ((100 - min(max(0, $bg_opacity), 100)) / 100));
  1111. $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, $bg_color, false, $text_background_alpha);
  1112. } else {
  1113. $text_color_background = phpthumb_functions::ImageHexColorAllocate($img_watermark, 'FFFFFF', false, 127);
  1114. }
  1115. $this->DebugMessage('WatermarkText() calling ImageFilledRectangle($img_watermark, 0, 0, '.ImageSX($img_watermark).', '.ImageSY($img_watermark).', $text_color_background)', __FILE__, __LINE__);
  1116. ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
  1117. if ($angle && function_exists('ImageRotate')) {
  1118. // using $img_watermark_mask is pointless if ImageRotate function isn't available
  1119. if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
  1120. $mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
  1121. ImageAlphaBlending($img_watermark_mask, false);
  1122. ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
  1123. $mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
  1124. }
  1125. }
  1126. $text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
  1127. foreach ($textlines as $key => $line) {
  1128. switch ($alignment) {
  1129. case 'C':
  1130. $x_offset = round(($text_width - (ImageFontWidth($size) * strlen($line))) / 2);
  1131. $originOffsetX = (ImageSX($gdimg) - ImageSX($img_watermark)) / 2;
  1132. $originOffsetY = (ImageSY($gdimg) - ImageSY($img_watermark)) / 2;
  1133. break;
  1134. case 'T':
  1135. $x_offset = round(($text_width - (ImageFontWidth($size) * strlen($line))) / 2);
  1136. $originOffsetX = (ImageSX($gdimg) - ImageSX($img_watermark)) / 2;
  1137. $originOffsetY = $margin;
  1138. break;
  1139. case 'B':
  1140. $x_offset = round(($text_width - (ImageFontWidth($size) * strlen($line))) / 2);
  1141. $originOffsetX = (ImageSX($gdimg) - ImageSX($img_watermark)) / 2;
  1142. $originOffsetY = ImageSY($gdimg) - ImageSY($img_watermark) - $margin;
  1143. break;
  1144. case 'L':
  1145. $x_offset = 0;
  1146. $originOffsetX = $margin;
  1147. $originOffsetY = (ImageSY($gdimg) - ImageSY($img_watermark)) / 2;
  1148. break;
  1149. case 'TL':
  1150. $x_offset = 0;
  1151. $originOffsetX = $margin;
  1152. $originOffsetY = $margin;
  1153. break;
  1154. case 'BL':
  1155. $x_offset = 0;
  1156. $originOffsetX = $margin;
  1157. $originOffsetY = ImageSY($gdimg) - ImageSY($img_watermark) - $margin;
  1158. break;
  1159. case 'R':
  1160. $x_offset = $text_width - (ImageFontWidth($size) * strlen($line));
  1161. $originOffsetX = ImageSX($gdimg) - ImageSX($img_watermark) - $margin;
  1162. $originOffsetY = (ImageSY($gdimg) - ImageSY($img_watermark)) / 2;
  1163. break;
  1164. case 'TR':
  1165. $x_offset = $text_width - (ImageFontWidth($size) * strlen($line));
  1166. $originOffsetX = ImageSX($gdimg) - ImageSX($img_watermark) - $margin;
  1167. $originOffsetY = $margin;
  1168. break;
  1169. case 'BR':
  1170. default:
  1171. $x_offset = $text_width - (ImageFontWidth($size) * strlen($line));
  1172. $originOffsetX = ImageSX($gdimg) - ImageSX($img_watermark) - $margin;
  1173. $originOffsetY = ImageSY($gdimg) - ImageSY($img_watermark) - $margin;
  1174. break;
  1175. }
  1176. $this->DebugMessage('WatermarkText() calling ImageString($img_watermark, '.$size.', '.$x_offset.', '.($key * ImageFontHeight($size)).', '.$line.', $text_color_watermark)', __FILE__, __LINE__);
  1177. ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
  1178. if ($angle && $img_watermark_mask) {
  1179. $this->DebugMessage('WatermarkText() calling ImageString($img_watermark_mask, '.$size.', '.$x_offset.', '.($key * ImageFontHeight($size)).', '.$text.', $mask_color_watermark)', __FILE__, __LINE__);
  1180. ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
  1181. }
  1182. }
  1183. if ($angle && $img_watermark_mask) {
  1184. $img_watermark = ImageRotate($img_watermark, $angle, $text_color_background);
  1185. $img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
  1186. phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
  1187. }
  1188. //phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
  1189. $this->DebugMessage('WatermarkText() calling phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, '.($originOffsetX.'x'.$originOffsetY).', '.$opacity.', 0)', __FILE__, __LINE__);
  1190. phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $originOffsetX.'x'.$originOffsetY, $opacity, 0);
  1191. ImageDestroy($img_watermark);
  1192. return true;
  1193. }
  1194. }
  1195. return false;
  1196. }
  1197. function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment='*', $opacity=50, $margin_x=5, $margin_y=null) {
  1198. if (is_resource($gdimg_dest) && is_resource($img_watermark)) {
  1199. $watermark_source_x = 0;
  1200. $watermark_source_y = 0;
  1201. $img_source_width = ImageSX($gdimg_dest);
  1202. $img_source_height = ImageSY($gdimg_dest);
  1203. $watermark_source_width = ImageSX($img_watermark);
  1204. $watermark_source_height = ImageSY($img_watermark);
  1205. $watermark_opacity_percent = max(0, min(100, $opacity));
  1206. $margin_y = (is_null($margin_y) ? $margin_x : $margin_y);
  1207. $watermark_margin_x = ((($margin_x > 0) && ($margin_x < 1)) ? round((1 - $margin_x) * $img_source_width) : $margin_x);
  1208. $watermark_margin_y = ((($margin_y > 0) && ($margin_y < 1)) ? round((1 - $margin_y) * $img_source_height) : $margin_y);
  1209. if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
  1210. $watermark_destination_x = intval($matches[1]);
  1211. $watermark_destination_y = intval($matches[2]);
  1212. } else {
  1213. switch ($alignment) {
  1214. case '*':
  1215. if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) {
  1216. ImageAlphaBlending($gdimg_tiledwatermark, false);
  1217. ImageSaveAlpha($gdimg_tiledwatermark, true);
  1218. $text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127);
  1219. ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent);
  1220. // set the tiled image transparent color to whatever the untiled image transparency index is
  1221. // ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark));
  1222. // a "cleaner" way of doing it, but can't handle the margin feature :(
  1223. // ImageSetTile($gdimg_tiledwatermark, $img_watermark);
  1224. // ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED);
  1225. // break;
  1226. // ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark));
  1227. // tile the image as many times as can fit
  1228. for ($x = $watermark_margin_x; $x < ($img_source_width + $watermark_source_width); $x += ($watermark_source_width + $watermark_margin_x)) {
  1229. for ($y = $watermark_margin_y; $y < ($img_source_height + $watermark_source_height); $y += ($watermark_source_height + $watermark_margin_y)) {
  1230. ImageCopy(
  1231. $gdimg_tiledwatermark,
  1232. $img_watermark,
  1233. $x,
  1234. $y,
  1235. 0,
  1236. 0,
  1237. min($watermark_source_width, $img_source_width - $x - $watermark_margin_x),
  1238. min($watermark_source_height, $img_source_height - $y - $watermark_margin_y)
  1239. );
  1240. }
  1241. }
  1242. $watermark_source_width = ImageSX($gdimg_tiledwatermark);
  1243. $watermark_source_height = ImageSY($gdimg_tiledwatermark);
  1244. $watermark_destination_x = 0;
  1245. $watermark_destination_y = 0;
  1246. ImageDestroy($img_watermark);
  1247. $img_watermark = $gdimg_tiledwatermark;
  1248. }
  1249. break;
  1250. case 'T':
  1251. $watermark_destination_x = round((($img_source_width / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
  1252. $watermark_destination_y = $watermark_margin_y;
  1253. break;
  1254. case 'B':
  1255. $watermark_destination_x = round((($img_source_width / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
  1256. $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y;
  1257. break;
  1258. case 'L':
  1259. $watermark_destination_x = $watermark_margin_x;
  1260. $watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
  1261. break;
  1262. case 'R':
  1263. $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x;
  1264. $watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
  1265. break;
  1266. case 'C':
  1267. $watermark_destination_x = round(($img_source_width / 2) - ($watermark_source_width / 2));
  1268. $watermark_destination_y = round(($img_source_height / 2) - ($watermark_source_height / 2));
  1269. break;
  1270. case 'TL':
  1271. $watermark_destination_x = $watermark_margin_x;
  1272. $watermark_destination_y = $watermark_margin_y;
  1273. break;
  1274. case 'TR':
  1275. $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x;
  1276. $watermark_destination_y = $watermark_margin_y;
  1277. break;
  1278. case 'BL':
  1279. //echo '<pre>';
  1280. ////var_dump($watermark_destination_x);
  1281. ////var_dump($watermark_destination_y);
  1282. //var_dump($watermark_margin_x);
  1283. //var_dump($img_source_height);
  1284. //var_dump($watermark_source_height);
  1285. //var_dump($watermark_margin_y);
  1286. $watermark_destination_x = $watermark_margin_x;
  1287. $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y;
  1288. break;
  1289. case 'BR':
  1290. default:
  1291. $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x;
  1292. $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y;
  1293. break;
  1294. }
  1295. }
  1296. ImageAlphaBlending($gdimg_dest, false);
  1297. ImageSaveAlpha($gdimg_dest, true);
  1298. ImageSaveAlpha($img_watermark, true);
  1299. phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent);
  1300. return true;
  1301. }
  1302. return false;
  1303. }
  1304. function DebugMessage($message, $file='', $line='') {
  1305. if (is_object($this->phpThumbObject)) {
  1306. return $this->phpThumbObject->DebugMessage($message, $file, $line);
  1307. }
  1308. return false;
  1309. }
  1310. }
  1311. ?>