PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/core/model/phpthumb/phpthumb.filters.php

http://github.com/modxcms/revolution
PHP | 1543 lines | 1195 code | 237 blank | 111 comment | 244 complexity | 678b3bf0c7c2ae25ff82463bed2fdc1c MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1

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

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