首先,有一个在php.ini看看gd库是不是开启的,下一步,我们会考虑每一个细节的图像生成步骤.
php生成图片验证码实例代码如下:
- <?php
-
- create_image();
- exit();
-
- function create_image()
- {
-
- $md5 = md5(rand(0,999));
-
- $pass = substr($md5, 10, 5);
-
-
- $width = 100;
- $height = 20;
-
-
- $image = ImageCreate($width, $height);
-
-
- $white = ImageColorAllocate($image, 255, 255, 255);
- $black = ImageColorAllocate($image, 0, 0, 0);
- $grey = ImageColorAllocate($image, 204, 204, 204);
-
-
- ImageFill($image, 0, 0, $black);
-
-
- ImageString($image, 3, 30, 3, $pass, $white);
-
-
-
- break
- ImageRectangle($image,0,0,$width-1,$height-1,$grey);
- imageline($image, 0, $height/2, $width, $height/2, $grey);
- imageline($image, $width/2, 0, $width/2, $height, $grey);
-
-
- header("Content-Type: image/jpeg");
-
-
- ImageJpeg($image);
-
-
- ImageDestroy($image);
- }
- ?>
验证码调用方法,代码如下:
<img height="120" alt="Dynamically generated image" src="generate_image.php" width="200"> |