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

Language PHP Lines 282
MD5 Hash 8049255841744c5f3a281e803ef9910a
Repository https://github.com/cuijinquan/nextwind.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
Wind::import('LIB:utility.verifycode.PwBaseCode');
 /**
  * GD库验证码
  * 
  * the last known user to change this file in the repository  <$LastChangedBy: gao.wanggao $>
  * @author $Author: gao.wanggao $ foxsee@aliyun.com
  * @version $Id: PwGDCode.php 23362 2013-01-09 04:19:41Z gao.wanggao $ 
  * @package 
  */
class PwGDCode extends PwBaseCode {
	
	private static $_background;
	
	private static $_graph;
	
	private static $_font;
	
	private static $_size;
	
	private static $_angle;
	
	private static $_color;
	
	private static $_codeLen = 0;
	
	private static $_codeX = 0;
	
	private static $_codeY = 0;
	
	private static $_image;

	public static function init() {
		if(!function_exists("imagecreatetruecolor") && !function_exists("imagecolorallocate") && !function_exists("imagestring") && !function_exists("imagepng") && !function_exists("imagesetpixel") && !function_exists("imagefilledrectangle") && !function_exists("imagerectangle")) {
			return false;
		}
		self::setRandCode();
		return true;
	}
	
	public static function outputImage() {
		 return self::$isRandGif ? (self::_outGif()) : (self::_outPng());
	}
	
	public static function outputFlash() {
		 return self::_outFlash();
	}

	private static function _setRandX() {
		self::$_codeX = floor((self::$verifyWidth -20) / self::$_codeLen);
		
	}
	
	private static function _setRandY() {
		$_y = self::$verifyHeight/2;
		self::$_codeY = mt_rand($_y+5,$_y+10);
	}
	

	private static function _setRandBackground() {
		$red 	= self::$isRandBackground ? mt_rand(100, 200) : 255;
		$green  = self::$isRandBackground ? mt_rand(100, 200) : 255;
		$blue   = self::$isRandBackground ? mt_rand(100, 200) : 255;
		$alpha  = 0;//self::$isRandBackground ? mt_rand(0, 127) : 0;
		self::$_background = array('red'=>$red,'green'=>$green,'blue'=>$blue,'alpha'=>$alpha);
	}
	
	private static function _setPicBackground() {
		$bgs=self::getVerifyBackground();
		$rand = array_rand($bgs);
		$imbg = imagecreatefromjpeg($bgs[$rand]);
		if (!$imbg) return false;
		imagecopymerge(self::$_image, $imbg, 0, 0, mt_rand(0,450-self::$verifyWidth), mt_rand(0,150-self::$verifyHeight), self::$verifyWidth, self::$verifyHeight, 100);
		imagedestroy($imbg);
	}
	
	private static function _setRandFont() {
		$_path=Wind::getRealDir(self::$path.'.font');
		if (self::$verifyType < 5) {
			$_fontList= self::getEnFontList();
		} else {
			$_fontList = self::getCnFontList();
		}
		$key = self::$isRandFont ? array_rand($_fontList,1) : 0;
		self::$_font =  $_path.'/'.$_fontList[$key];
		return;
	}
	
	private static function _setRandSize() {
		self::$_size = self::$isRandSize ? mt_rand(14,20) : 18;
	}
	
	private static function _setRandAngle() {
		self::$_angle = self::$isRandAngle ? mt_rand(-20, 10) : 0;
	}
	
	private static function _setRandColor() {
		if (!self::$isRandColor) {
			self::$_color = imagecolorallocate(self::$_image, 0, 0, 0);
		} else {
			self::$_color =  imagecolorallocate(self::$_image, mt_rand(0, 255), mt_rand(0, 120), mt_rand(0, 255));
		}
	}
	
	private static function _setRandDistortion() {
		if (!self::$isRandDistortion) return true;
		$_tmp = self::$_image;
		self::$verifyWidth = self::$verifyWidth;
		self::_creatImage();
		self::_creatBackground();
        for ( $i=0; $i<self::$verifyWidth; $i++) {
            for ( $j=0; $j<self::$verifyHeight; $j++) {
                $_color = imagecolorat($_tmp, $i , $j);
                if( intval($i+sin($j/self::$verifyHeight*2*M_PI)*10) <= self::$verifyWidth && intval(($i+sin($j/self::$verifyHeight*2*M_PI)*10)) >=0 ) {
               		 imagesetpixel(self::$_image, intval($i+sin($j/self::$verifyHeight*2*M_PI-0.6)*5), $j,$_color);
                }
                
            }
       }
	}
	
	
	private static function _setRandGraph() {
		if (!self::$isRandGraph) return true;
		$_tmp = mt_rand(1,3);
		switch ($_tmp) {
			case '1':
		   		self::_setImageLine();
		    	break;
		    case '2':
		   		self::_setImagePix();
		    	break;
		    case '3':
		   		self::_setImageEarc();
		    	break;
		   /* case '4':
		    	self::_setImageLine();
		   		self::_setImageEarc();
		    	break;
		    case '5':
		   		self::_setImageLine();
		   		//self::_setImagePix();
		    	break;
		    case '6':
		   		//self::_setImagePix();
		   		self::_setImageEarc();
		    	break;*/
		}
	}
	
	private static function _setImageLine() {
		$_tmp = mt_rand(30,40);
		for ($i = 0; $i < $_tmp; $i++) {
			$_x = mt_rand(0, self::$verifyWidth);
			$_y = mt_rand(0, self::$verifyHeight);
			$_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
			imageline ( self::$_image, $_x, $_y, mt_rand(($_x-10), ($_x+5)), mt_rand(($_y+5), ($_y+20)), $_color);
		}
	}
	
	private static function _setImagePix() {
		$_tmp = mt_rand(600,800);
		for ($i = 0; $i < $_tmp; $i++) {
			$_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
			imagesetpixel(self::$_image, mt_rand(0, self::$verifyWidth), mt_rand(0, self::$verifyHeight), $_color);	
		}
	}
	private static function _setImageEarc() {
		$_tmp = mt_rand(5,10);
		for ($i = 0; $i < $_tmp; $i++) {
			$_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
			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);
		}
	}
	
	private static function _getCodeLenth() {
		//self::$_codeLen = Pw::strlen(self::$verifyCode);
		self::$_codeLen = WindString::strlen(self::$verifyCode, 'utf-8');
	}
	
	private static function _outFlash() {
		if (!class_exists('SWFBitmap')) return false;
		self::_getCodeLenth();
		self::_creatImage();
		self::_setRandBackground();
		self::_creatBackground();
		//self::_setPicBackground();
		self::_setRandFont();
		self::_setRandGraph();	
		self::_writeImage();
		self::_setRandDistortion();	
		$_tmpPath = Wind::getRealDir('DATA:tmp.');
		$_tmp = $_tmpPath.WindUtility::generateRandStr(8).'.png';
		imagepng(self::$_image, $_tmp);
		if (!WindFile::isFile($_tmp)) return false;
		imagedestroy(self::$_image);
		$bit= new SWFBitmap($_tmp);
		$shape= new SWFShape(); 
		$shape->setRightFill($shape->addFill($bit)); 
		$shape->drawLine($bit->getWidth(),0); 
		$shape->drawLine(0,$bit->getHeight()); 
		$shape->drawLine(-$bit->getWidth(),0); 
		$shape->drawLine(0, -$bit->getHeight()); 
		$movie= new SWFMovie(); 
		$movie->setDimension($bit->getWidth(),$bit->getHeight()); 
		$flash=$movie->add($shape);
		header("Pragma:no-cache");
		header("Cache-control:no-cache");
		header('Content-type: application/x-shockwave-flash'); 
		$movie->output();
		WindFolder::clear($_tmpPath);
	}
	
	private static function _outPng() {
		header("Pragma:no-cache");
		header("Cache-control:no-cache");
		header("Content-type: image/png");
		self::_getCodeLenth();
		self::_creatImage();
		if (self::$isRandBackground) {
			self::_setPicBackground();
		} else {
			self::_setRandBackground();
			self::_creatBackground();
		}
		self::_setRandFont();
		self::_setRandGraph();	
		self::_writeImage();
		self::_setRandDistortion();	
		imagepng(self::$_image);
		imagedestroy(self::$_image);
	}
	
	private static function _outGif() {
		header("Pragma:no-cache");
		header("Cache-control:no-cache");
		header("Content-type: image/gif");
		Wind::import('LIB:utility.verifycode.GifMerge');
		self::_getCodeLenth();
		self::_setRandBackground();
		self::_setRandFont();
		for ($i=0; $i<3; $i++) {
			self::_creatImage();
			self::_creatBackground();
			self::_setRandGraph();
			self::_writeImage();
			ob_start();
			imageGif(self::$_image);
			$frame[] = ob_get_contents();
			$delay[] = 100;
			imagedestroy(self::$_image);
			ob_end_clean();
		}
		$gif = new GifMerge($frame, 0, 0, 0, 0, $delay, 0, 0, 'C_MEMORY');
		echo $gif->getAnimation();
		
	}
	private static function _writeImage() {
		for ($i = 0; $i < self::$_codeLen; $i++) { 
			//$_text = Pw::substrs(self::$verifyCode, 1, $i, false);
			$_text = WindString::substr(self::$verifyCode, $i, 1, 'utf-8', false);
			self::_setRandSize();
			self::_setRandAngle();
			self::_setRandX();
			self::_setRandY();
			self::_setRandColor();
			ImageTTFText(self::$_image, self::$_size, self::$_angle, (self::$_codeX * $i +10) , self::$_codeY, self::$_color, self::$_font,$_text);
		}
	}
	
	private static function _creatImage() {
		self::$_image = imagecreatetruecolor(self::$verifyWidth, self::$verifyHeight);	
		imagesavealpha(self::$_image, true);
	}
	
	private static function _creatBackground() {
		$_tmp = imagecolorallocatealpha(self::$_image, self::$_background['red'], self::$_background['green'], self::$_background['blue'], self::$_background['alpha']); 
		imagefill(self::$_image, 20, 0, $_tmp); 
	}

}
?>
Back to Top