PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/template_engines_bench/libs/quicky/plugins/CAPTCHA/imagedraw.class.php

https://github.com/limb-php-framework/limb-tools
PHP | 425 lines | 402 code | 9 blank | 14 comment | 69 complexity | a37eb2d6a9f6f3e045799212c5d8c90f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**************************************************************************/
  3. /* (c)oded 2006 by white phoenix
  4. /* http://whitephoenix.ru
  5. /*
  6. /* imagedraw.class.php: Image Draw
  7. /**************************************************************************/
  8. if (!function_exists('win2uni'))
  9. {
  10. function win2uni($s)
  11. {
  12. $s = convert_cyr_string($s,'w','i');
  13. for ($result='',$i=0; $i<strlen($s); $i++)
  14. {
  15. $charcode = ord($s[$i]);
  16. $result .= ($charcode>175)?'&#'.(1040+($charcode-176)).';':$s[$i];
  17. }
  18. return $result;
  19. }
  20. }
  21. define('MIRROR_HORIZONTAL',1);
  22. define('MIRROR_VERTICAL',2);
  23. define('MIRROR_BOTH',3);
  24. define('VERTICAL',1);
  25. define('HORIZONTAL',2);
  26. class imagedraw
  27. {
  28. var $res;
  29. var $W;
  30. var $H;
  31. var $scale = 1;
  32. var $cX;
  33. var $cY;
  34. var $colors = array();
  35. var $type = 'png';
  36. var $quality = 100;
  37. var $outfile = '';
  38. var $offsetX = 0;
  39. var $offsetY = 0;
  40. var $colortransparent;
  41. function imagedraw() {$this->init();}
  42. function init()
  43. {
  44. $this->sW = $this->W*$this->scale;
  45. $this->sH = $this->H*$this->scale;
  46. $this->cX = $this->W/2;
  47. $this->cY = $this->H/2;
  48. $this->set_offset($this->offsetX,$this->offsetY);
  49. }
  50. function sY() {return imagesy($this->res);}
  51. function sX() {return imagesx($this->res);}
  52. function set_offset($offsetX,$offsetY)
  53. {
  54. $this->offsetX = $offsetX*$this->scale;
  55. $this->offsetY = $offsetY*$this->scale;
  56. }
  57. function hex2color($color,$d=false)
  58. {
  59. if (is_resource($color)) {return $color;}
  60. if (is_string($color)) {$color = hexdec($color);}
  61. if (is_int($color)) {$color = sprintf('%06x',$color);}
  62. if (isset($this->colors[$color])) {return $this->colors[$color];}
  63. return $this->colors[$color] = imagecolorallocate($this->res,hexdec(substr($color,0,2)),hexdec(substr($color,2,2)),hexdec(substr($color,4,2)));
  64. }
  65. function createfromstring($string)
  66. {
  67. $this->res = imagecreatefromstring($string);
  68. }
  69. function createfromjpeg($file)
  70. {
  71. $this->res = imagecreatefromjpeg($file);
  72. }
  73. function createtruecolor($w=NULL,$h=NULL)
  74. {
  75. if (is_null($w)) {return $this->res = imagecreatetruecolor($this->sW+$this->offsetX,$this->sH+$this->offsetY);}
  76. else {return $this->res = imagecreatetruecolor($w,$h);}
  77. }
  78. function colorat($x,$y) {return imagecolorat($this->res,$x+$this->offsetX,$y+$this->offsetY);}
  79. function create($w=NULL,$h=NULL)
  80. {
  81. if (is_null($w)) {return $this->res = imagecreate($this->sW+$this->offsetX,$this->sH+$this->offsetY);}
  82. else {return $this->res = imagecreate($w,$h);}
  83. }
  84. function out()
  85. {
  86. if ($this->type == 'png') {return $this->outfile != ''?imagepng($this->res,strval($this->outfile)):imagepng($this->res);}
  87. elseif ($this->type == 'jpeg') {return $this->outfile != ''?imagejpeg($this->res,strval($this->outfile),$this->quality):imagejpeg($this->res,NULL,$this->quality);}
  88. elseif ($this->type == 'gif') {return $this->outfile != ''?imagegif($this->res,strval($this->outfile)):imagegif($this->res);}
  89. }
  90. function setbgcolor($color = 0xFFFFFF) {return imagefill($this->res,0,0,$this->hex2color($color));}
  91. function border($color = 0x000000)
  92. {
  93. return imageRectangle($this->res,
  94. $this->offsetX, $this->offsetY,
  95. $this->sW+$this->offsetX-1, $this->sH+$this->offsetY-1,
  96. $this->hex2color($color));
  97. }
  98. function rscale($a) {return $a/$this->scale;}
  99. function tscale($a) {return $a*$this->scale;}
  100. function setScale($s) {return $this->scale = floatval($s);}
  101. function setpixel($x,$y,$color = 0x000000) {return imagesetpixel($this->res,$this->offsetX+$x,$this->offsetY+$y,$this->hex2color($color));}
  102. function line($x1,$y1,$x2,$y2,$color = 0x000000,$thick = 1)
  103. {
  104. $x1 = $x1*$this->scale+$this->offsetX;
  105. $x2 = $x2*$this->scale+$this->offsetX;
  106. $y1 = $y1*$this->scale+$this->offsetY;
  107. $y2 = $y2*$this->scale+$this->offsetY;
  108. $color = $this->hex2color($color);
  109. if ($thick == 1) {return imageline($this->res,$x1,$y1,$x2,$y2,$color);}
  110. $t = $thick/2-0.5;
  111. if ($x1 == $x2 || $y1 == $y2) {return imagefilledrectangle($this->res,round(min($x1,$x2)-$t),round(min($y1,$y2)-$t),round(max($x1,$x2)+$t),round(max($y1,$y2)+$t),$color);}
  112. $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
  113. $a = $t / sqrt(1 + pow($k,2));
  114. $points = array(
  115. round($x1-(1+$k)*$a),round($y1+(1-$k)*$a),
  116. round($x1-(1-$k)*$a),round($y1-(1+$k)*$a),
  117. round($x2+(1+$k)*$a),round($y2-(1-$k)*$a),
  118. round($x2+(1-$k)*$a),round($y2+(1+$k)*$a),
  119. );
  120. imagefilledpolygon($this->res,$points,4,$color);
  121. imagepolygon($this->res,$points,4,$color);
  122. return;
  123. }
  124. function line1($x1,$y1,$x2,$y2,$color = 0x000000,$thickness=5)
  125. {
  126. imagesetthickness($this->res,$thickness);
  127. imageline($this->res,
  128. $this->offsetX+$x1*$this->scale, $this->offsetY-1+$y1*$this->scale,
  129. $this->offsetX+$x2*$this->scale, $this->offsetY-1+$y2*$this->scale,
  130. $this->hex2color($color));
  131. imagesetthickness($this->res,1);
  132. }
  133. function ttftext($text,$color,$font,$size,$x,$y,$angle=0)
  134. {
  135. if (!is_file($font) or !is_readable($font)) {user_error('Can\'t open font '.$font,E_USER_WARNING); return;}
  136. return imagettftext($this->res,$size*$this->scale,$angle,
  137. $this->offsetX+$x*$this->scale,
  138. $this->offsetY+$y*$this->scale,
  139. $this->hex2color($color),$font,win2uni($text));
  140. }
  141. function arc($cx,$cy,$w,$h,$s,$e,$color = 0x000000) {return imagearc($this->res,$cx+$this->offsetX,$cy+$this->offsetY,$w,$h,$s,$e,$this->hex2color($color));}
  142. function filledarc($cx,$cy,$w,$h,$s,$e,$color = 0x000000) {return imagefilledarc($this->res,$cx+$this->offsetX,$cy+$this->offsetY,$w,$h,$s,$e,$this->hex2color($color));}
  143. function ellipse($cx,$cy,$w,$h,$color = 0x000000) {return imageellipse($this->res,$cx+$this->offsetX,$cy+$this->offsetY,$w,$h,$s,$e,$this->hex2color($color));}
  144. function filledellipse($cx,$cy,$w,$h,$color = 0x000000) {return imagefilledellipse($this->res,$cx+$this->offsetX,$cy+$this->offsetY,$w,$h,$this->hex2color($color));}
  145. function pointmark($x,$y,$name=NULL,$color = 0x000000)
  146. {
  147. $this->filledellipse($x,$y,5,5,$color);
  148. if (is_null($name)) {$name = '('.round($x,3).','.round($y,3).')';}
  149. $this->ttftext($name,$color,CORE_PATH.'fonts/ARIAL.TTF',9,$x+5,$y-5);
  150. }
  151. function filledpolygon($points,$color = 0x000000)
  152. {
  153. if (!is_array($points)) {return FALSE;}
  154. else {$points = array_values($points); $points = array_map('intval',$points);}
  155. foreach ($points as $i=>$v) {$points[$i] = $v*$this->scale+(($i%2 == 0)?$this->offsetX:$this->offsetY);}
  156. return imagefilledpolygon($this->res,$points,sizeof($points)/2,$this->hex2color($color));
  157. }
  158. function polygon($points,$color = 0x000000)
  159. {
  160. if (!is_array($points)) {return FALSE;}
  161. else {$points = array_values($points); $points = array_map('intval',$points);}
  162. foreach ($points as $i=>$v) {$points[$i] = $v*$this->scale+(($i%2 == 0)?$this->offsetX:$this->offsetY);}
  163. return imagefilledpolygon($this->res,$points,sizeof($points)/2,$this->hex2color($color));
  164. }
  165. function antialias($bool) {imageantialias($this->res,!!$bool);}
  166. function colortransparent($color = 0xFFFFFF) {return imagecolortransparent($this->res,$this->colortransparent = $this->hex2color($color));}
  167. function filter_twirl($dimx=NULL,$dimy=NULL)
  168. {
  169. if (is_null($dimx)) {$dimx = $this->W-1;}
  170. if (is_null($dimy)) {$dimy = $this->H-1;}
  171. $wp = $dimx/2;
  172. $hp = $dimy/2;
  173. $im_source = imagecreatetruecolor($dimx,$dimy);
  174. imagecopy($im_source,$this->res,
  175. 0,0,
  176. 0,0,
  177. $dimx,$dimy
  178. );
  179. $im_filter = imagecreatetruecolor($dimx+100,$dimy+100);
  180. imagealphablending($im_filter,FALSE);
  181. imagesavealpha($im_filter,TRUE);
  182. $color_filter = imagecolorallocatealpha($im_filter,255,255,255,127);
  183. imagefill($im_filter,0,0,$color_filter);
  184. $a = atan2(-1.0,$wp-1.0);
  185. if ($a < 0.0) {$a += 2.0*M_PI;}
  186. $dx = $dimx / $a;
  187. $d = sqrt($hp*$hp+$wp*$wp);
  188. $dy = $dimy / $d;
  189. for ($h = 0; $h < $dimy + 1; $h++)
  190. {
  191. for ($w = 0; $w < $dimx + 1; $w++)
  192. {
  193. $x = ($w-$wp);
  194. $y = ($h-$hp);
  195. $dist = sqrt($x*$x+$y*$y);
  196. $angle = atan2($y,$x);
  197. if ($angle < 0) {$angle += 2.0 * M_PI;}
  198. $rgb = imagecolorat($this->res,(int)($dimx - $dx*$angle),(int)($dy*$dist));
  199. $a = ($rgb >> 24) & 0xFF;
  200. $r = ($rgb >> 16) & 0xFF;
  201. $g = ($rgb >> 8) & 0xFF;
  202. $b = $rgb & 0xFF;
  203. $color_filter = imagecolorallocatealpha($im_filter,$r,$g,$b,$a);
  204. imagesetpixel($im_filter,$w,$h,$color_filter);
  205. }
  206. }
  207. imagecopy($this->res,$im_filter,
  208. 0,0,
  209. 0,0,
  210. $dimx,$dimy
  211. );
  212. imagedestroy($im_filter);
  213. }
  214. function filter_swirl($dimx=NULL,$dimy=NULL)
  215. {
  216. if (is_null($dimx)) {$dimx = $this->W;}
  217. if (is_null($dimy)) {$dimy = $this->H;}
  218. $wp = $dimx/2;
  219. $hp = $dimy/2;
  220. $im_source = imagecreatetruecolor($dimx,$dimy);
  221. imagecopy($im_source,$this->res,
  222. 0,0,
  223. 0,0,
  224. $dimx,$dimy
  225. );
  226. $im_filter = imagecreatetruecolor($dimx,$dimy);
  227. imagealphablending($im_filter,FALSE);
  228. imagesavealpha($im_filter,TRUE);
  229. $color_filter = imagecolorallocatealpha($im_filter,255,255,255,127);
  230. imagefill($im_filter,0,0,$color_filter);
  231. $dz = -0.01;
  232. for ($h = 0; $h < $dimy + 1; $h++)
  233. {
  234. for ($w = 0; $w < $dimx + 1; $w++)
  235. {
  236. $x = ($w - $wp);
  237. $y = ($h - $hp);
  238. $dist = sqrt($x*$x + $y*$y);
  239. $angle = atan2($y,$x);
  240. if ($angle < 0) {$angle += 2.0 * M_PI;}
  241. @$rgb = imagecolorat($im_source,(int) ($wp + $dist * cos($angle + $dist * $dz)),(int)($hp + $dist * sin($angle + $dist * $dz)));
  242. $a = ($rgb >> 24) & 0xFF;
  243. $r = ($rgb >> 16) & 0xFF;
  244. $g = ($rgb >> 8) & 0xFF;
  245. $b = $rgb & 0xFF;
  246. $color_filter = imagecolorallocatealpha($im_filter,$r,$g,$b,$a);
  247. imagesetpixel($im_filter,$w,$h,$color_filter);
  248. }
  249. }
  250. imagecopy($this->res,$im_filter,
  251. 0,0,
  252. 0,0,
  253. $dimx,$dimy
  254. );
  255. //imagedestroy($im_source);
  256. //imagedestroy($im_filter);
  257. }
  258. function wave_region($x,$y,$width,$height,$grade=10)
  259. {
  260. for ($i = 0; $i < $width; $i += 2)
  261. {
  262. imagecopy($this->res,$this->res,
  263. $x+$i-2,$y+sin($i/10)*$grade, //dest
  264. $x+$i,$y, //src
  265. 2,$height);
  266. }
  267. }
  268. function resize_file($src,$dest,$width,$height,$rgb = 0xFFFFFF,$quality = 100)
  269. {
  270. if (!file_exists($src)) {return FALSE;}
  271. $size = getimagesize($src);
  272. if (!$size) return false;
  273. if (($size[0] <= $width) && ($size[1] <= $height)) {return copy($src,$dest);}
  274. $format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
  275. $icfunc = 'imagecreatefrom'.$format;
  276. if (!function_exists($icfunc)) {return FALSE;}
  277. $x_ratio = $width / $size[0];
  278. $y_ratio = $height / $size[1];
  279. $ratio = min($x_ratio, $y_ratio);
  280. $use_x_ratio = ($x_ratio == $ratio);
  281. $new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
  282. $new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
  283. $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
  284. $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
  285. $isrc = $icfunc($src);
  286. $idest = imagecreatetruecolor($width, $height);
  287. imagefill($idest, 0, 0, $rgb);
  288. imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0,
  289. $new_width, $new_height, $size[0], $size[1]);
  290. imagejpeg($idest,$dest,$quality);
  291. imagedestroy($isrc);
  292. imagedestroy($idest);
  293. return TRUE;
  294. }
  295. function skew($skew_val)
  296. {
  297. $width = $this->sX();
  298. $height = $this->sY();
  299. $imgdest = imagecreatetruecolor($width,$height+($height*$skew_val));
  300. $trans = imagecolorallocate($imgdest,0,0,0);
  301. $temp = 0;
  302. for($x = 0; $x < $width; $x++)
  303. {
  304. for($y = 0; $y < $height; $y++)
  305. {
  306. imagecopy($imgdest,$this->res,$x,$y+$temp,$x,$y,1,1);
  307. imagecolortransparent($imgdest,$trans);
  308. }
  309. $temp += $skew_val;
  310. }
  311. $this->destroy();
  312. $this->res = $imgdest;
  313. }
  314. function flip($mode)
  315. {
  316. $w = $this->sX();
  317. $h = $this->sY();
  318. $flipped = imagecreatetruecolor($w,$h);
  319. if ($mode & VERTICAL)
  320. {
  321. for ($y = 0; $y < $h; $y++) {imagecopy($flipped,$this->res,0,$y,0,$h - $y - 1,$w,1);}
  322. }
  323. if ($mode & HORIZONTAL)
  324. {
  325. for ($x = 0; $x < $w; $x++) {imagecopy($flipped,$this->res,$x,0,$w - $x - 1,0,1,$h);}
  326. }
  327. $this->res = $flipped;
  328. }
  329. function mirror($type=1)
  330. {
  331. $imgsrc = $this->res;
  332. $width = $this->sX();
  333. $height = $this->sY();
  334. $imgdest = imagecreatetruecolor($width,$height);
  335. for ($x=0 ; $x<$width ; $x++)
  336. {
  337. for ($y=0 ; $y<$height ; $y++)
  338. {
  339. if ($type == MIRROR_HORIZONTAL) imagecopy($imgdest,$imgsrc,$width-$x-1,$y,$x,$y,1,1);
  340. if ($type == MIRROR_VERTICAL) imagecopy($imgdest,$imgsrc,$x,$height-$y-1,$x,$y,1,1);
  341. if ($type == MIRROR_BOTH) imagecopy($imgdest,$imgsrc,$width-$x-1,$height-$y-1,$x,$y,1,1);
  342. }
  343. }
  344. $this->destroy();
  345. $this->res = $imgdest;
  346. }
  347. function watermark($img)
  348. {
  349. $bwidth = $this->sX();
  350. $bheight = $this->xY();
  351. $lwidth = imagesx($img);
  352. $lheight = imagesy($img);
  353. $src_x = $bwidth - ($lwidth + 5);
  354. $src_y = $bheight - ($lheight + 5);
  355. imageAlphaBlending($this->res,TRUE);
  356. imagecopy($this->res,$img,$src_x,$src_y,0,0,$lwidth,$lheight);
  357. }
  358. function skew_waves()
  359. {
  360. $width = $this->sx();
  361. $height = $this->sY();
  362. $img = $this->res;
  363. $img2 = imagecreatetruecolor($width,$height);
  364. // частоты
  365. $rand1 = mt_rand(700000,1000000) / 15000000;
  366. $rand2 = mt_rand(700000,1000000) / 15000000;
  367. $rand3 = mt_rand(700000,1000000) / 15000000;
  368. $rand4 = mt_rand(700000,1000000) / 15000000;
  369. // фазы
  370. $rand5 = mt_rand(0,3141592) / 1000000;
  371. $rand6 = mt_rand(0,3141592) / 1000000;
  372. $rand7 = mt_rand(0,3141592) / 1000000;
  373. $rand8 = mt_rand(0,3141592) / 1000000;
  374. // амплитуды
  375. $rand9 = mt_rand(400,600) / 100;
  376. $rand10 = mt_rand(400,600) / 100;
  377. for ($x = 0; $x < $width; $x++)
  378. {
  379. for ($y = 0; $y < $height; $y++)
  380. {
  381. // координаты пикселя-первообраза.
  382. $sx = $x + (sin($x * $rand1 + $rand5) + sin($y * $rand3 + $rand6)) * $rand9;
  383. $sy = $y + (sin($x * $rand2 + $rand7) + sin($y * $rand4 + $rand8)) * $rand10;
  384. // первообраз за пределами изображения
  385. if ($sx < 0 || $sy < 0 || $sx >= $width - 1 || $sy >= $height - 1)
  386. {
  387. $color = 0xFF;
  388. $color_x = 0xFF;
  389. $color_y = 0xFF;
  390. $color_xy = 0xFF;
  391. }
  392. else
  393. { // цвета основного пикселя и его 4-х соседей для лучшего антиалиасинга
  394. $color = (imagecolorat($img,$sx,$sy) >> 16) & 0xFF;
  395. $color_x = (imagecolorat($img,$sx + 1,$sy) >> 16) & 0xFF;
  396. $color_y = (imagecolorat($img,$sx,$sy + 1) >> 16) & 0xFF;
  397. $color_xy = (imagecolorat($img,$sx + 1,$sy + 1) >> 16) & 0xFF;
  398. }
  399. $frsx = $sx - floor($sx); //дробная часть отклонения координат первообраза от целого
  400. $frsy = $sy - floor($sy);
  401. $frsx1 = 1 - $frsx;
  402. $frsy1 = 1 - $frsy;
  403. // вычисление цвета нового пикселя как пропорции от цвета основного пикселя и его соседей
  404. $newcolor = floor( $color * $frsx1 * $frsy1 +
  405. $color_x * $frsx * $frsy1 +
  406. $color_y * $frsx1 * $frsy +
  407. $color_xy * $frsx * $frsy );
  408. $s = dechex($newcolor);
  409. imagesetpixel($img2,$x,$y,hexdec($s.$s.$s));
  410. }
  411. }
  412. $this->destroy();
  413. $this->res = $img2;
  414. }
  415. function destroy() {return imagedestroy($this->res);}
  416. }