GD库图像处理
1. PHP中GD库的使用
1.1 GD简介
PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。目前GD2库支持GIF、JPEG、PNG和WBMP等格式。此外还支持一些FreeType、Type1等字体库。
JPEG 是一种压缩标准的名字,通常是用来存储照片或者存储具有丰富色彩和色彩层次的图像。这种格式使用了有损压缩。
PNG 是可移植的网络图像,对图像采用了无损压缩标准。
GIF 原义是“图像互换格式”,是一种基于LZW算法的连续色调的无损压缩格式
1.2 画布管理
在PHP中创建一个图像应该完成如下所示的4个步骤:
1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。
2.在背景上绘制图像轮廓或输入文本。
3.输出最终图形
4.释放资源
1.3 设置颜色
imagecolorallocate -- 为一幅图像分配颜色
语法:int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
比如:$im = imagecreatetruecolor(100, 100); //创建画布的大小为100x100
$red = imagecolorallocate($im,255,0,0); //由十进制整数设置一个颜色
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);// 十六进制方式
1.4 生成图像
imagegif -- 以 GIF 格式将图像输出到浏览器或文件
语法:bool imagegif (resource image [,string filename] )
imagejpeg -- 以 JPEG 格式将图像输出到浏览器或文件
语法:bool imagejpeg (resource image [,string filename [, int quality]] )
imagepng -- 以 PNG 格式将图像输出到浏览器或文件
语法:bool imagepng (resource image [,string filename] )
imagewbmp -- 以 WBMP 格式将图像输出到浏览器或文件
语法:bool imagewbmp (resource image [, string filename [, int foreground]] )
1.5 绘制图像
imagefill -- 区域填充
语法:bool imagefill(resource image,int x,int y, int color)
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagesetpixel -- 画一个单一像素
语法:bool imagesetpixel ( resource image, int x, int y, int color )
imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
imageline -- 画一条线段
语法:bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
imagerectangle -- 画一个矩形
语法:bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int col )
imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。
imagefilledrectangle -- 画一矩形并填充
语法:bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )
imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。
imagepolygon -- 画一个多边形
语法:bool imagepolygon ( resource image, array points, int num_points, int color )
imagepolygon() 在图像中创建一个多边形。points 是一个 PHP 数组,包含了多边形的各个顶点坐标,即 points[0] = x0,points[1] = y0,points[2] = x1,points[3] = y1,以此类推。num_points 是顶点的总数。
imagefilledpolygon -- 画一多边形并填充
语法:bool imagefilledpolygon ( resource image, array points, int num_points, int color )
imagefilledpolygon() 在 image 图像中画一个填充了的多边形。points 参数是一个按顺序包含有多边形各顶点的 x 和 y 坐标的数组。 num_points 参数是顶点的总数,必须大于 3。
imageellipse -- 画一个椭圆
语法:bool imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imageellipse() 在 image 所代表的图像中画一个中心为 cx,cy(图像左上角为 0, 0)的椭圆。w 和 h 分别指定了椭圆的宽度和高度,椭圆的颜色由 color 指定。
imagefilledellipse -- 画一椭圆并填充
语法:bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)为中心画一个椭圆。w 和 h 分别指定了椭圆的宽和高。椭圆用 color 颜色填充。如果成功则返回 TRUE,失败则返回 FALSE。
imagearc -- 画椭圆弧
bool imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color )
imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。
imagefilledarc -- 画一椭圆弧且填充
bool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
imagefilledarc() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。如果成功则返回 TRUE,失败则返回 FALSE。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style 可以是下列值按位或(OR)后的值:
IMG_ARC_PIE IMG_ARC_CHORD
IMG_ARC_NOFILL IMG_ARC_EDGED
1.6 在图像中绘制文字
imagestring -- 水平地画一行字符串
语法:bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagestringup -- 垂直地画一行字符串
语法:bool imagestringup ( resource image, int font, int x, int y, string s, int col )
imagestring()用 col 颜色将字符串 s 垂直地画到 image 所代表的图像的 x, y 座标处(图像的左上角为 0, 0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagechar -- 水平地画一个字符
语法:bool imagechar ( resource image, int font, int x, int y, string c, int color )
imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 是 1,2,3,4 或 5,则使用内置的字体(更大的数字对应于更大的字体)。
imagecharup -- 垂直地画一个字符
语法:bool imagecharup ( resource image, int font, int x, int y, string c, int color )
imagecharup() 将字符 c 垂直地画在 image 指定的图像上,位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 为 1,2,3,4 或 5,则使用内置的字体。
imagettftext -- 用 TrueType 字体向图像写入文本
语法 :array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
1.7 使用GD库做一个简单的项目
<?php
header("content-type:text/html;charset=utf-8");
/**
* Created by PhpStorm.
* User: lanouhn
* Date: 2017/9/7
* Time: 9:49
*/
//1,创建画布 背景
$width = 500;
$height = 500;
//创建真彩色画布背景
$img = imagecreatetruecolor($width,$height);
//2,配色 调色
$white = imagecolorallocate($img,255,255,255);
//3,画画
imagefill($img,0,0,$white);
//写入字符串
$black = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
$code = mt_rand(1000,9999);
imagestring($img,5,250,250,"$code",$black);
//画点
for($i=0;$i<5000;$i++){
$x = mt_rand(0,500);
$y = mt_rand(0,500);
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($img,$x,$y,$color);
}
//划线
for($j=0;$j<100;$j++){
$x1 = mt_rand(0,500);
$y1 = mt_rand(0,500);
$x2 = mt_rand(0,500);
$y2 = mt_rand(0,500);
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($img,$x1,$y1,$x2,$y2,$color);
}
//4,输出图像
header("content-type:image/png"); //图像格式
//imagepng($img) 参数1 是资源 参数2 是保存的路径 没有参数2是直接页面输出
//imagepng($img,"./1.png");
imagepng($img);
//5,释放资源
imagedestroy($img);
结果显示:
2. 验证码的绘制和使用
使用GD库创建一个登录界面的验证码
登录界面代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="../jquery/jquery.js"></script>
<title>登录</title>
<style>
div,div>img{
width: 80px;
height: 35px;
}
input{
width: 200px;
height: 30px;
}
div{
margin-top: -35px;
margin-left: 290px;
}
</style>
</head>
<body>
<form action="login_action.php" method="post">
<span>用户名:</span>
<input type="text" name="name"><br>
<span>密码: </span>
<input type="password" name="pass"><br>
<span>验证码:</span>
<input type="text" name="verify">
<div><img src="01-verify.php" alt="" id="pic"></div><br>
<input type="submit" value="登录">
</form>
</body>
</html>
<script>
var pic = document.getElementById("pic");
pic.onclick = function fun() {
this.src = this.src+"?rand=" +Math.random();
}
</script>
登录界面的后台处理代码:
<?php
session_start();
header("content-type:text/html;charset=utf-8");
/**
* Created by PhpStorm.
* User: lanouhn
* Date: 2017/9/7
* Time: 10:00
*/
$name = $_POST["name"];
$pass = $_POST["pass"];
$verify = strtolower($_POST["verify"]);
$code = $_SESSION["code"];
if($verify == $code){
echo "验证码正确";
}else{
echo "验证码不正确";
}
获取验证码并处理验证码:
<?php
session_start();
header("content-type:text/html;charset=utf-8");
/**
* Created by PhpStorm.
* User: lanouhn
* Date: 2017/9/7
* Time: 16:21
*/
function get_verify($key=3,$l= 4){
$width = 150;
$height = 45;
$img = imagecreatetruecolor($width,$height);
$color6 = imagecolorallocate($img, 220, 220, 220);
imagefill($img,0,0,$color6);
switch ($key){
case 1:
//纯数字
$text = "0123456789";
break;
case 2:
//纯字母
$text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;
case 3:
//字母加数字
$text = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;
}
$color = imagecolorallocate($img, 0, 0, 0);
$code = substr(str_shuffle($text),0,$l);
imagettftext($img,20,0,40,35,$color,"msyhbd.ttc",$code);
$_SESSION["code"] = strtolower($code);
//画点
for($i=0;$i<100;$i++){
$x = mt_rand(0,150);
$y = mt_rand(0,50);
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($img,$x,$y,$color);
}
//划线
for($j=0;$j<15;$j++){
$x1 = mt_rand(0,150);
$y1 = mt_rand(0,50);
$x2 = mt_rand(0,150);
$y2 = mt_rand(0,50);
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($img,$x1,$y1,$x2,$y2,$color);
}
header("content-type:image/png");
imagepng($img);
imagedestroy($img);
}
get_verify();
结果显示: