设为首页 加入收藏

TOP

PHP全栈学习笔记7(一)
2019-08-15 23:32:55 】 浏览:221
Tags:PHP 全栈 学习 笔记

PHP全栈学习笔记7

图形图像处理技术,gd库的强大支持,PHP的图像可以是PHP的强项,PHP图形化类库,jpgraph是一款非常好用的强大的图形处理工具。

在PHP中加载GD库

gd官方网址下载:

http://www.boutell.com/gd

激活gd库,修改php.in文件

将该文件中的“;extension=php_gd2.dll”选项前的分号“;”删除

验证GD库是否安装成功
输入“127.0.0.1/phpinfo.php”并按Enter键,检索到的安装信息,即说明GD库安装成功。

Jpgraph的安装与配置

官方网站http://www.aditus.nu/jpgraph/下载

解压到文件夹,编辑php.ini文件,修改include_path参数,如include_path = ".;F:\AppServ\www\jpgraph",重新启动Apache。

配置Jpgraph类库的文件jpg-config.inc.php,
支持中文的配置

DEFINE('CHINESE_TTF_FONT','bkai00mp.ttf');

默认图片格式的配置

DEFINE("DEFAULT_GFORMAT","auto");

创建画布,可以通过imagecreate()函数实现

<?php
$im = imagecreate(200,100);
$white = imagecolorallocate($im, 255,65,150);
imagegif($im);
?>

gd库支持中文,但只能是utf-8,使用imageString()会显示乱码,只能接收utf-8编码格式,默认使用英文字体。

header()函数定义输出图像类型
imagecreatefromjpeg()函数载入图片
imagecolorallocate()函数设置输出字体颜色
iconv()函数对输出的中文字符串的编码格式进行转换
imageTTFText()函数向照片中添加文字

<?php
header("content-type:image/jpeg");
$im = imagecreateformjpeg("images/photo.jpg");
$textcolor = imagecolorallocate($im, 35,35,23);

//定义字体
$fnt="c:/windows/fonts/simhei.ttf";
//定义输出字体串
$motto = iconv("gb2312","utf-8","长白山");
// 写文字
imageTTFText($im, 220,0,200,233, $textcolor, $fnt, $motto); // 写TTF文字到图中
// 简历里jpeg图形
imageipeg($im);
imagedestory($im);
?>

使用图像处理技术生成的验证码

<?php
session_start();
header("content-type:image/png");     //设置创建图像的格式
$image_width=70;                      //设置图像宽度
$image_height=18;                     //设置图像高度
srand(microtime()*100000);            //设置随机数的种子
for($i=0;$i<4;$i++){                  //循环输出一个4位的随机数
   $new_number.=dechex(rand(0,15));
}
$_SESSION[check_checks]=$new_number;    //将获取的随机数验证码写入到SESSION变量中     

$num_image=imagecreate($image_width,$image_height);  //创建一个画布
imagecolorallocate($num_image,255,255,255);          //设置画布的颜色
for($i=0;$i<strlen($_SESSION[check_checks]);$i++){  //循环读取SESSION变量中的验证码
   $font=mt_rand(3,5);                              //设置随机的字体
   $x=mt_rand(1,8)+$image_width*$i/4;               //设置随机字符所在位置的X坐标
   $y=mt_rand(1,$image_height/4);                   //设置随机字符所在位置的Y坐标
   $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));       //设置字符的颜色
   imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color);                   //水平输出字符
}
imagepng($num_image);               //生成PNG格式的图像
imagedestroy($num_image);           //释放图像资源
?>
<?php
session_start();
if($_POST["Submit"]!=""){
$checks=$_POST["checks"];
if($checks==""){
echo "<script> alert('验证码不能为空');window.location.href='index.php';</script>";
}
if($checks==$_SESSION[check_checks]){
    echo "<script> alert('用户登录成功!');window.location.href='index.php';</script>";
}else{
    echo "<script> alert('您输入的验证码不正确!');window.location.href='index.php';</script>";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>验证码应用</title>
<style type="text/css">
<!--
.STYLE1 {
    font-size: 12px;
    color: #FFFFFF;
    font-weight: bold;
}
.style2 {font-weight: bold; font-size: 12px;}
-->
</style>
</head>
<body>
<form name=
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/12/12
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇腾讯云服务器搭建Apache/PHP/MySQ.. 下一篇PHP全栈学习笔记8

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目