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