图像处理及验证码制作

php中通过GD库(扩展)处理图像,开启GD库扩展

图像处理流程:

创建图像

  • 所有的绘图设计都基于一个图像完成
1
imagecreatetruecolor(x,y)

#函数 x,y分别为长和高,单位是像素

绘制图像

  • 填充点线数字文字等

#分配一个颜色

1
$color1=imagecolorallocate(resource,red,green,blue);

#填充图像 imagefill($img,x,y,$color1); #x,y是坐标

输出图像

  • 将图像以某种格式保存到服务器中或者直接输出到浏览器显示给用户

用header()告诉浏览器,当前返回内容为图片,不用text方式解析!!

默认下为header(‘Content-type:text/html;charset=utf-8’)

现在修改为header('Content-type:image/jepg')

在header函数前不能输出任何内容 要将header()写在第一行

#转成jpeg格式,调用php将返回图片

1
imagejepg(resource[,string filename[,int quality]])

#保存到文件,调用php将不返回图片

1
imagejepg($resource,'name.jepg')

释放资源

imagedestory($source);

用php写完后返回的资源是个图,则可以在html中调用这个php文件,显示图片

生成验证码php代码

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
<?php

//必须至于顶部,多服务器端记录验证码信息,便于用户输入后做校验
function vocode($width=120,$height=40,$fontsize=30){
session_start();
//默认返回的是黑色的照片
$image = imagecreatetruecolor($width, $height);
//设置各种颜色
$bgcolor = imagecolorallocate($image, 255, 255, 255);
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$stringcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
//将白色铺满地图
imagefill($image, 0, 0, $bgcolor);
//空字符串,每循环一次,追加到字符串后面
$captch_code='';
$data=str_split("abcdefghijkmnpqrstuvwxyz3456789QWERTYUIOPASDFGHJKLZXCVNBM");
$len_data=count($data);
//验证码为随机四个数字
for ($i=0; $i < 4; $i++) {
// 产生随机数字0-9
$fontcontent = $data[rand(0,$len_data-1)];
$captch_code.= $fontcontent;
}
$the_font="./front_style/ManyGifts.ttf";
imagettftext($image,$fontsize,rand(-5,5),rand(5,15),rand(30,35),$fontcolor,$the_font,$captch_code);

$_SESSION['authcode'] = $captch_code;
#画点
for ($i = 0; $i < 50; $i++) {
imagesetpixel($image,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}
// 画线
for ($i = 0; $i < rand(2,3); $i++) {
imageline($image,rand(0,$width/2-1),rand(0,$height-1),rand($width/2+1,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}
header('content-type:image/png');
imagepng($image);
//销毁
imagedestroy($image);
}
?><?php
// 返回图片
header('Content-type:image/png');
// 设置宽度 高度
$width=200;
$height=50;
// 产生一个图像
$image=imagecreatetruecolor($width,$height);
// 设置不同的颜色
$Bgcolor=imagecolorallocate($image,255,255,255);
$fontcolor=imagecolorallocate($image,0,0,0);
// 填充图像
imagefill($image,0,0,$Bgcolor);
#画点
for ($i = 0; $i < 50; $i++) {
imagesetpixel($image,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}

// 画线
for ($i = 0; $i < rand(2,3); $i++) {
imageline($image,rand(0,$width/2-1),rand(0,$height-1),rand($width/2+1,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}
// 设置字
$the_font='arial.ttf';
// 设置字的 大小 角度 起始位置 颜色 字体 内容
imagettftext($image,20,20,0,0,$fontcolor,$the_font,'AAA');
#生成图像
imagepng($image);
// 销毁
imagedestroy($image);