时间:2021-07-01 10:21:17 帮助过:15人阅读

在介绍php画图功能之前,我们首先需要了解一下php中的绘图坐标系:
php坐标系中,坐标原点位于左上角,以像素为单位。(推荐学习:PHP编程从入门到精通)
坐标(x,y)——第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置为垂直方向,距离坐标点y个像素。

特别说明:像素不是一个长度单位,而是一个密度单位。理论上,分辨率越小,一个像素所占的长度会大大一些。
php画图的基本原理和步骤
(1)创建画布
(2)绘制需要的各种图形(圆,直线,矩形,弧线,扇形...)
(3)输出图像到网页,也可以另存(可以另存为网站开发常见的几种图片格式,基于存储格式问题)
(4)销毁该图片(释放内存)
下面是画验证码的代码:
<?php
//echo rand(2,9);
//echo "<br/>".dechex(rand(1,15))."<br/>";
session_start();
$checkCode="";
for($i=0;$i<4;$i++) {
$checkCode.=dechex(rand(1,15));
}
//讲随机验证码保存到session中
$_SESSION['myCheckCode']=$checkCode;
//创建图片,并把随机数画上去
$img=imagecreatetruecolor(110, 30);
//背景默认就是黑色
//你可以指定背景颜色
$bgcolor=imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
//创建新的颜色
$white=imagecolorallocate($img, 255, 255, 255);
$blue=imagecolorallocate($img, 0, 0, 255);
$red=imagecolorallocate($img, 255, 0, 0);
$green=imagecolorallocate($img, 0, 255, 0);
//画出干扰线段
for($i=0;$i<20;$i++) {
//更好的方法是颜色随机
imageline($img, rand(0,110), rand(0,30), rand(0,110), rand(0,30),
imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)));
}
//画出噪点,自己画
//for($i=0;$i<10;$i++)
//把四个随机值画上去
imagestring($img, rand(1,5), rand(2,80), rand(2,10), $checkCode, $white);
//如果要使用中文
//array imagefttext(string $font_file, string $text [,array $extrainfo)
//imagettftext($img,15,10,20,25,$white,"STXINWET.TTF","北京你好");
//输出
header("content-type: image/png");
imagepng($img);
?>用于登录界面(这里只提供部分代码,但是不影响理解,可删除无关内容,直接使用)
<form action="loginProcess.php" method="post">
<table>
<tr><td>用户id</td><td><input type="text" name="id" value="<?php echo getCookieVal("id"); ?>"/></td></tr>
<tr><td>密 码</td><td><input type="password" name="password"/></td></tr>
<tr><td>验证码</td><td><input type="text" name="checkCode"/>
<img src="checkCode.php" onClick="this.src='checkCode.php?aa='+Math.random()"/></td></tr>
<tr><td colspan="2">是否保存用户id<input type="checkbox" value="yes" name="keep"></td></tr>
<tr><td><input type="submit" value="用户登录"/></td>
<td><input type="reset" value="重新填写"/></td></tr>
</table>
</form>以上就是php画图功能有什么用途的详细内容,更多请关注Gxl网其它相关文章!