PageRenderTime 65ms CodeModel.GetById 35ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/startZine/src/main/core/class/verify.class.php

http://ownerpress.googlecode.com/
PHP | 408 lines | 368 code | 27 blank | 13 comment | 77 complexity | a6ddd04a2a29cd9c757336d842901594 MD5 | raw file
  1<?php
  2// +----------------------------------------------------------------------
  3// | ?????????? (Build on ThinkPHP)
  4// +----------------------------------------------------------------------
  5// | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6// +----------------------------------------------------------------------
  7
  8/**  
  9 * verify.class.php
 10 *
 11 * ??????
 12 *
 13 * @package class
 14 * @author awfigq <awfigq@qq.com>
 15 */
 16class Verify
 17{
 18	var $code;
 19	var $type 	= 0;
 20	var $width 	= 90;
 21	var $height 	= 36;
 22	var $background	= 1;
 23	var $adulterate	= 1;
 24	var $ttf 	= 0;
 25	var $angle 	= 0;
 26	var $warping 	= 0;
 27	var $scatter	= 0;
 28	var $color 	= 1;
 29	var $size 	= 0;
 30	var $shadow 	= 1;
 31
 32	var $fontcolor;
 33	var $im;
 34	
 35	function display()
 36	{
 37		$this->width = $this->width >= 80 && $this->width <= 200 ? $this->width : 90;
 38		$this->height = $this->height >= 30 && $this->height <= 80 ? $this->height : 36;
 39		if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
 40			function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
 41			function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
 42			$this->image();
 43		} else {
 44			$this->bitmap();
 45		}
 46	}
 47
 48	function image()
 49	{
 50		$bgcontent = $this->background();
 51		$this->im = imagecreatefromstring($bgcontent);
 52		$this->adulterate && $this->adulterate();
 53		$this->ttf && function_exists('imagettftext') ? $this->ttffont() : $this->giffont();
 54		$this->scatter && $this->scatter($this->im);
 55
 56		if(function_exists('imagepng'))
 57		{
 58			header('Content-type: image/png');
 59			imagepng($this->im);
 60		}
 61		else
 62		{
 63			header('Content-type: image/jpeg');
 64			imagejpeg($this->im, '', 100);
 65		}
 66		imagedestroy($this->im);
 67	}
 68
 69	function background()
 70	{
 71		$this->im = imagecreatetruecolor($this->width, $this->height);
 72		$backgrounds = $c = array();
 73		if($this->background && function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') && function_exists('imagecopymerge') &&
 74			function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY'))
 75		{
 76			if($handle = @opendir(PUBLIC_ROOT.'./verify/background/'))
 77			{
 78				while($bgfile = @readdir($handle))
 79				{
 80					if(preg_match('/\.jpg$/i', $bgfile))
 81					{
 82						$backgrounds[] = PUBLIC_ROOT.'./verify/background/'.$bgfile;
 83					}
 84				}
 85				@closedir($handle);
 86			}
 87			
 88			if($backgrounds)
 89			{
 90				$imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]);
 91				$colorindex = imagecolorat($imwm, 0, 0);
 92				$c = imagecolorsforindex($imwm, $colorindex);
 93				$colorindex = imagecolorat($imwm, 1, 0);
 94				imagesetpixel($imwm, 0, 0, $colorindex);
 95				$c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue'];
 96				imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100);
 97				imagedestroy($imwm);
 98			}
 99		}
100		
101		if(!$this->background || !$backgrounds)
102		{
103			for($i = 0;$i < 3;$i++)
104			{
105				$start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];
106			}
107			
108			for($i = 0;$i < $this->width;$i++)
109			{
110				$color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);
111				imageline($this->im, $i, 0, $i, $this->height, $color);
112				$c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
113			}
114			
115			$c[0] -= 20;$c[1] -= 20;$c[2] -= 20;
116		}
117		ob_start();
118		if(function_exists('imagepng'))
119		{
120			imagepng($this->im);
121		}
122		else
123		{
124			imagejpeg($this->im, '', 100);
125		}
126		imagedestroy($this->im);
127		$bgcontent = ob_get_contents();
128		ob_end_clean();
129		$this->fontcolor = $c;
130		return $bgcontent;
131	}
132
133	function adulterate()
134	{
135		$linenums = $this->height / 10;
136		for($i = 0; $i <= $linenums;$i++) {
137			$color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
138			$x = mt_rand(0, $this->width);
139			$y = mt_rand(0, $this->height);
140			if(mt_rand(0, 1)) {
141				$w = mt_rand(0, $this->width);
142				$h = mt_rand(0, $this->height);
143				$s = mt_rand(0, 360);
144				$e = mt_rand(0, 360);
145				for($j = 0;$j < 3;$j++) {
146					imagearc($this->im, $x + $j, $y, $w, $h, $s, $e, $color);
147				}
148			} else {
149				$xe = mt_rand(0, $this->width);
150				$ye = mt_rand(0, $this->height);
151				imageline($this->im, $x, $y, $xe, $ye, $color);
152				for($j = 0;$j < 3;$j++) {
153					imageline($this->im, $x + $j, $y, $xe, $ye, $color);
154				}
155			}
156		}
157	}
158
159	function adulteratefont()
160	{
161		$seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
162		$x = $this->width / 4;
163		$y = $this->height / 10;
164		$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
165		for($i = 0; $i <= 3; $i++) {
166			$adulteratecode = $seccodeunits[mt_rand(0, 23)];
167			imagechar($this->im, 5, $x * $i + mt_rand(0, $x - 10), mt_rand($y, $this->height - 10 - $y), $adulteratecode, $text_color);
168		}
169	}
170
171	function ttffont()
172	{
173		$seccode = $this->code;
174		$seccoderoot = PUBLIC_ROOT.'./verify/font/';
175		$dirs = opendir($seccoderoot);
176		$seccodettf = array();
177		while($entry = readdir($dirs))
178		{
179			if($entry != '.' && $entry != '..' && in_array(strtolower(fileext($entry)), array('ttf', 'ttc')))
180			{
181				$seccodettf[] = $entry;
182			}
183		}
184		
185		if(empty($seccodettf))
186		{
187			$this->giffont();
188			return;
189		}
190		
191		$seccodelength = 4;
192		
193		$widthtotal = 0;
194		for($i = 0; $i < $seccodelength; $i++)
195		{
196			$font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];
197			$font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;
198			$font[$i]['size'] = $this->type ? $this->width / 7 : $this->width / 6;
199			$this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size'] + $this->width / 20);
200			$box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
201			$font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);
202			$box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);
203			$font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
204			$font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];
205			$font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->width / 8);
206			$font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i]['width'];
207			$widthtotal += $font[$i]['width'];
208		}
209		$x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);
210		!$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
211		for($i = 0; $i < $seccodelength; $i++)
212		{
213			if($this->color)
214			{
215				$this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
216				$this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
217				$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
218			}
219			elseif($this->shadow)
220			{
221				$text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
222			}
223			$y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']);
224			$this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor, $font[$i]['font'], $seccode[$i]);
225			imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font'], $seccode[$i]);
226			$x += $font[$i]['width'];
227		}
228		$this->warping && $this->warping($this->im);
229	}
230
231	function warping(&$obj)
232	{
233		$rgb = array();
234		$direct = rand(0, 1);
235		$width = imagesx($obj);
236		$height = imagesy($obj);
237		$level = $width / 20;
238		for($j = 0;$j < $height;$j++)
239		{
240			for($i = 0;$i < $width;$i++)
241			{
242				$rgb[$i] = imagecolorat($obj, $i , $j);
243			}
244			for($i = 0;$i < $width;$i++)
245			{
246				$r = sin($j / $height * 2 * M_PI - M_PI * 0.5) * ($direct ? $level : -$level);
247				imagesetpixel($obj, $i + $r , $j , $rgb[$i]);
248			}
249		}
250	}
251
252	function scatter(&$obj, $level = 0)
253	{
254		$rgb = array();
255		$this->scatter = $leval ? $level : $this->scatter;
256		$width = imagesx($obj);
257		$height = imagesy($obj);
258		for($j = 0;$j < $height;$j++)
259		{
260			for($i = 0;$i < $width;$i++)
261			{
262				$rgb[$i] = imagecolorat($obj, $i , $j);
263			}
264			
265			for($i = 0;$i < $width;$i++)
266			{
267				$r = rand(-$this->scatter, $this->scatter);
268				imagesetpixel($obj, $i + $r , $j , $rgb[$i]);
269			}
270		}
271	}
272
273	function giffont()
274	{
275		$seccode = $this->code;
276		$seccodedir = array();
277		if(function_exists('imagecreatefromgif'))
278		{
279			$seccoderoot = PUBLIC_ROOT.'./verify/gif/';
280			$dirs = opendir($seccoderoot);
281			while($dir = readdir($dirs))
282			{
283				if($dir != '.' && $dir != '..' && file_exists($seccoderoot.$dir.'/9.gif'))
284				{
285					$seccodedir[] = $dir;
286				}
287			}
288		}
289		
290		$widthtotal = 0;
291		for($i = 0; $i <= 3; $i++)
292		{
293			$this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
294			if(!empty($this->imcodefile) && file_exists($this->imcodefile))
295			{
296				$font[$i]['file'] = $this->imcodefile;
297				$font[$i]['data'] = getimagesize($this->imcodefile);
298				$font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
299				$font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
300				$font[$i]['width'] += mt_rand(0, $this->width / 5 - $font[$i]['width']);
301				$widthtotal += $font[$i]['width'];
302			}
303			else
304			{
305				$font[$i]['file'] = '';
306				$font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);
307				$widthtotal += $font[$i]['width'];
308			}
309		}
310		$x = mt_rand(1, $this->width - $widthtotal);
311		for($i = 0; $i <= 3; $i++)
312		{
313			$this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
314			if($font[$i]['file'])
315			{
316				$this->imcode = imagecreatefromgif($font[$i]['file']);
317				if($this->size)
318				{
319					$font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this->width / 20);
320					$font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this->width / 20);
321				}
322				$y = mt_rand(0, $this->height - $font[$i]['height']);
323				if($this->shadow)
324				{
325					$this->imcodeshadow = $this->imcode;
326					imagecolorset($this->imcodeshadow, 0, 0, 0, 0);
327					imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
328				}
329				imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
330				imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
331			}
332			else
333			{
334				$y = mt_rand(0, $this->height - 20);
335				if($this->shadow)
336				{
337					$text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
338					imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
339				}
340				$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
341				imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);
342			}
343			$x += $font[$i]['width'];
344		}
345	}
346
347	function bitmap()
348	{
349		$numbers = array
350			(
351			'B' => array('00','fc','66','66','66','7c','66','66','fc','00'),
352			'C' => array('00','38','64','c0','c0','c0','c4','64','3c','00'),
353			'E' => array('00','fe','62','62','68','78','6a','62','fe','00'),
354			'F' => array('00','f8','60','60','68','78','6a','62','fe','00'),
355			'G' => array('00','78','cc','cc','de','c0','c4','c4','7c','00'),
356			'H' => array('00','e7','66','66','66','7e','66','66','e7','00'),
357			'J' => array('00','f8','cc','cc','cc','0c','0c','0c','7f','00'),
358			'K' => array('00','f3','66','66','7c','78','6c','66','f7','00'),
359			'M' => array('00','f7','63','6b','6b','77','77','77','e3','00'),
360			'P' => array('00','f8','60','60','7c','66','66','66','fc','00'),
361			'Q' => array('00','78','cc','cc','cc','cc','cc','cc','78','00'),
362			'R' => array('00','f3','66','6c','7c','66','66','66','fc','00'),
363			'T' => array('00','78','30','30','30','30','b4','b4','fc','00'),
364			'V' => array('00','1c','1c','36','36','36','63','63','f7','00'),
365			'W' => array('00','36','36','36','77','7f','6b','63','f7','00'),
366			'X' => array('00','f7','66','3c','18','18','3c','66','ef','00'),
367			'Y' => array('00','7e','18','18','18','3c','24','66','ef','00'),
368			'2' => array('fc','c0','60','30','18','0c','cc','cc','78','00'),
369			'3' => array('78','8c','0c','0c','38','0c','0c','8c','78','00'),
370			'4' => array('00','3e','0c','fe','4c','6c','2c','3c','1c','1c'),
371			'6' => array('78','cc','cc','cc','ec','d8','c0','60','3c','00'),
372			'7' => array('30','30','38','18','18','18','1c','8c','fc','00'),
373			'8' => array('78','cc','cc','cc','78','cc','cc','cc','78','00'),
374			'9' => array('f0','18','0c','6c','dc','cc','cc','cc','78','00')
375			);
376
377		foreach($numbers as $i => $number) {
378			for($j = 0; $j < 6; $j++) {
379				$a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
380				$a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
381				mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
382				mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
383			}
384		}
385
386		$bitmap = array();
387		for($i = 0; $i < 20; $i++) {
388			for($j = 0; $j <= 3; $j++) {
389				$bytes = $numbers[$this->code[$j]][$i];
390				$a = mt_rand(0, 14);
391				array_push($bitmap, $bytes);
392			}
393		}
394
395		for($i = 0; $i < 8; $i++) {
396			$a = substr('012345', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
397			array_unshift($bitmap, $a);
398			array_push($bitmap, $a);
399		}
400
401		$image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
402				'0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));
403
404		header('Content-Type: image/bmp');
405		echo $image;
406	}
407}
408?>