PageRenderTime 36ms CodeModel.GetById 19ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/src/library/utility/verifycode/PwGDCode.php

https://github.com/cuijinquan/nextwind
PHP | 282 lines | 222 code | 37 blank | 23 comment | 28 complexity | 8049255841744c5f3a281e803ef9910a MD5 | raw file
  1<?php
  2Wind::import('LIB:utility.verifycode.PwBaseCode');
  3 /**
  4  * GD库验证码
  5  * 
  6  * the last known user to change this file in the repository  <$LastChangedBy: gao.wanggao $>
  7  * @author $Author: gao.wanggao $ foxsee@aliyun.com
  8  * @version $Id: PwGDCode.php 23362 2013-01-09 04:19:41Z gao.wanggao $ 
  9  * @package 
 10  */
 11class PwGDCode extends PwBaseCode {
 12	
 13	private static $_background;
 14	
 15	private static $_graph;
 16	
 17	private static $_font;
 18	
 19	private static $_size;
 20	
 21	private static $_angle;
 22	
 23	private static $_color;
 24	
 25	private static $_codeLen = 0;
 26	
 27	private static $_codeX = 0;
 28	
 29	private static $_codeY = 0;
 30	
 31	private static $_image;
 32
 33	public static function init() {
 34		if(!function_exists("imagecreatetruecolor") && !function_exists("imagecolorallocate") && !function_exists("imagestring") && !function_exists("imagepng") && !function_exists("imagesetpixel") && !function_exists("imagefilledrectangle") && !function_exists("imagerectangle")) {
 35			return false;
 36		}
 37		self::setRandCode();
 38		return true;
 39	}
 40	
 41	public static function outputImage() {
 42		 return self::$isRandGif ? (self::_outGif()) : (self::_outPng());
 43	}
 44	
 45	public static function outputFlash() {
 46		 return self::_outFlash();
 47	}
 48
 49	private static function _setRandX() {
 50		self::$_codeX = floor((self::$verifyWidth -20) / self::$_codeLen);
 51		
 52	}
 53	
 54	private static function _setRandY() {
 55		$_y = self::$verifyHeight/2;
 56		self::$_codeY = mt_rand($_y+5,$_y+10);
 57	}
 58	
 59
 60	private static function _setRandBackground() {
 61		$red 	= self::$isRandBackground ? mt_rand(100, 200) : 255;
 62		$green  = self::$isRandBackground ? mt_rand(100, 200) : 255;
 63		$blue   = self::$isRandBackground ? mt_rand(100, 200) : 255;
 64		$alpha  = 0;//self::$isRandBackground ? mt_rand(0, 127) : 0;
 65		self::$_background = array('red'=>$red,'green'=>$green,'blue'=>$blue,'alpha'=>$alpha);
 66	}
 67	
 68	private static function _setPicBackground() {
 69		$bgs=self::getVerifyBackground();
 70		$rand = array_rand($bgs);
 71		$imbg = imagecreatefromjpeg($bgs[$rand]);
 72		if (!$imbg) return false;
 73		imagecopymerge(self::$_image, $imbg, 0, 0, mt_rand(0,450-self::$verifyWidth), mt_rand(0,150-self::$verifyHeight), self::$verifyWidth, self::$verifyHeight, 100);
 74		imagedestroy($imbg);
 75	}
 76	
 77	private static function _setRandFont() {
 78		$_path=Wind::getRealDir(self::$path.'.font');
 79		if (self::$verifyType < 5) {
 80			$_fontList= self::getEnFontList();
 81		} else {
 82			$_fontList = self::getCnFontList();
 83		}
 84		$key = self::$isRandFont ? array_rand($_fontList,1) : 0;
 85		self::$_font =  $_path.'/'.$_fontList[$key];
 86		return;
 87	}
 88	
 89	private static function _setRandSize() {
 90		self::$_size = self::$isRandSize ? mt_rand(14,20) : 18;
 91	}
 92	
 93	private static function _setRandAngle() {
 94		self::$_angle = self::$isRandAngle ? mt_rand(-20, 10) : 0;
 95	}
 96	
 97	private static function _setRandColor() {
 98		if (!self::$isRandColor) {
 99			self::$_color = imagecolorallocate(self::$_image, 0, 0, 0);
100		} else {
101			self::$_color =  imagecolorallocate(self::$_image, mt_rand(0, 255), mt_rand(0, 120), mt_rand(0, 255));
102		}
103	}
104	
105	private static function _setRandDistortion() {
106		if (!self::$isRandDistortion) return true;
107		$_tmp = self::$_image;
108		self::$verifyWidth = self::$verifyWidth;
109		self::_creatImage();
110		self::_creatBackground();
111        for ( $i=0; $i<self::$verifyWidth; $i++) {
112            for ( $j=0; $j<self::$verifyHeight; $j++) {
113                $_color = imagecolorat($_tmp, $i , $j);
114                if( intval($i+sin($j/self::$verifyHeight*2*M_PI)*10) <= self::$verifyWidth && intval(($i+sin($j/self::$verifyHeight*2*M_PI)*10)) >=0 ) {
115               		 imagesetpixel(self::$_image, intval($i+sin($j/self::$verifyHeight*2*M_PI-0.6)*5), $j,$_color);
116                }
117                
118            }
119       }
120	}
121	
122	
123	private static function _setRandGraph() {
124		if (!self::$isRandGraph) return true;
125		$_tmp = mt_rand(1,3);
126		switch ($_tmp) {
127			case '1':
128		   		self::_setImageLine();
129		    	break;
130		    case '2':
131		   		self::_setImagePix();
132		    	break;
133		    case '3':
134		   		self::_setImageEarc();
135		    	break;
136		   /* case '4':
137		    	self::_setImageLine();
138		   		self::_setImageEarc();
139		    	break;
140		    case '5':
141		   		self::_setImageLine();
142		   		//self::_setImagePix();
143		    	break;
144		    case '6':
145		   		//self::_setImagePix();
146		   		self::_setImageEarc();
147		    	break;*/
148		}
149	}
150	
151	private static function _setImageLine() {
152		$_tmp = mt_rand(30,40);
153		for ($i = 0; $i < $_tmp; $i++) {
154			$_x = mt_rand(0, self::$verifyWidth);
155			$_y = mt_rand(0, self::$verifyHeight);
156			$_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
157			imageline ( self::$_image, $_x, $_y, mt_rand(($_x-10), ($_x+5)), mt_rand(($_y+5), ($_y+20)), $_color);
158		}
159	}
160	
161	private static function _setImagePix() {
162		$_tmp = mt_rand(600,800);
163		for ($i = 0; $i < $_tmp; $i++) {
164			$_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
165			imagesetpixel(self::$_image, mt_rand(0, self::$verifyWidth), mt_rand(0, self::$verifyHeight), $_color);	
166		}
167	}
168	private static function _setImageEarc() {
169		$_tmp = mt_rand(5,10);
170		for ($i = 0; $i < $_tmp; $i++) {
171			$_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
172			imagearc(self::$_image, mt_rand(0, self::$verifyWidth), mt_rand(10,self::$verifyHeight), mt_rand(10, self::$verifyWidth), mt_rand(self::$verifyHeight, self::$verifyHeight*2), mt_rand(0, 90), mt_rand(0,90), $_color);
173		}
174	}
175	
176	private static function _getCodeLenth() {
177		//self::$_codeLen = Pw::strlen(self::$verifyCode);
178		self::$_codeLen = WindString::strlen(self::$verifyCode, 'utf-8');
179	}
180	
181	private static function _outFlash() {
182		if (!class_exists('SWFBitmap')) return false;
183		self::_getCodeLenth();
184		self::_creatImage();
185		self::_setRandBackground();
186		self::_creatBackground();
187		//self::_setPicBackground();
188		self::_setRandFont();
189		self::_setRandGraph();	
190		self::_writeImage();
191		self::_setRandDistortion();	
192		$_tmpPath = Wind::getRealDir('DATA:tmp.');
193		$_tmp = $_tmpPath.WindUtility::generateRandStr(8).'.png';
194		imagepng(self::$_image, $_tmp);
195		if (!WindFile::isFile($_tmp)) return false;
196		imagedestroy(self::$_image);
197		$bit= new SWFBitmap($_tmp);
198		$shape= new SWFShape(); 
199		$shape->setRightFill($shape->addFill($bit)); 
200		$shape->drawLine($bit->getWidth(),0); 
201		$shape->drawLine(0,$bit->getHeight()); 
202		$shape->drawLine(-$bit->getWidth(),0); 
203		$shape->drawLine(0, -$bit->getHeight()); 
204		$movie= new SWFMovie(); 
205		$movie->setDimension($bit->getWidth(),$bit->getHeight()); 
206		$flash=$movie->add($shape);
207		header("Pragma:no-cache");
208		header("Cache-control:no-cache");
209		header('Content-type: application/x-shockwave-flash'); 
210		$movie->output();
211		WindFolder::clear($_tmpPath);
212	}
213	
214	private static function _outPng() {
215		header("Pragma:no-cache");
216		header("Cache-control:no-cache");
217		header("Content-type: image/png");
218		self::_getCodeLenth();
219		self::_creatImage();
220		if (self::$isRandBackground) {
221			self::_setPicBackground();
222		} else {
223			self::_setRandBackground();
224			self::_creatBackground();
225		}
226		self::_setRandFont();
227		self::_setRandGraph();	
228		self::_writeImage();
229		self::_setRandDistortion();	
230		imagepng(self::$_image);
231		imagedestroy(self::$_image);
232	}
233	
234	private static function _outGif() {
235		header("Pragma:no-cache");
236		header("Cache-control:no-cache");
237		header("Content-type: image/gif");
238		Wind::import('LIB:utility.verifycode.GifMerge');
239		self::_getCodeLenth();
240		self::_setRandBackground();
241		self::_setRandFont();
242		for ($i=0; $i<3; $i++) {
243			self::_creatImage();
244			self::_creatBackground();
245			self::_setRandGraph();
246			self::_writeImage();
247			ob_start();
248			imageGif(self::$_image);
249			$frame[] = ob_get_contents();
250			$delay[] = 100;
251			imagedestroy(self::$_image);
252			ob_end_clean();
253		}
254		$gif = new GifMerge($frame, 0, 0, 0, 0, $delay, 0, 0, 'C_MEMORY');
255		echo $gif->getAnimation();
256		
257	}
258	private static function _writeImage() {
259		for ($i = 0; $i < self::$_codeLen; $i++) { 
260			//$_text = Pw::substrs(self::$verifyCode, 1, $i, false);
261			$_text = WindString::substr(self::$verifyCode, $i, 1, 'utf-8', false);
262			self::_setRandSize();
263			self::_setRandAngle();
264			self::_setRandX();
265			self::_setRandY();
266			self::_setRandColor();
267			ImageTTFText(self::$_image, self::$_size, self::$_angle, (self::$_codeX * $i +10) , self::$_codeY, self::$_color, self::$_font,$_text);
268		}
269	}
270	
271	private static function _creatImage() {
272		self::$_image = imagecreatetruecolor(self::$verifyWidth, self::$verifyHeight);	
273		imagesavealpha(self::$_image, true);
274	}
275	
276	private static function _creatBackground() {
277		$_tmp = imagecolorallocatealpha(self::$_image, self::$_background['red'], self::$_background['green'], self::$_background['blue'], self::$_background['alpha']); 
278		imagefill(self::$_image, 20, 0, $_tmp); 
279	}
280
281}
282?>