博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP全栈学习笔记7
阅读量:4318 次
发布时间:2019-06-06

本文共 12026 字,大约阅读时间需要 40 分钟。

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()函数实现

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

header()函数定义输出图像类型

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

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

alert('验证码不能为空');window.location.href='index.php';";}if($checks==$_SESSION[check_checks]){ echo "";}else{ echo "";}}?>
验证码应用
     
 
     
  用户名  
  密码  
  验证码     
        
 
     

使用柱形图

SetScale("textlin");$graph->yaxis->scale->SetGrace(20);//创建画布阴影$graph->SetShadow();//设置显示区左、右、上、下距边线的距离,单位为像素$graph->img->SetMargin(40,30,30,40);//创建一个矩形的对象$bplot = new BarPlot($datay);//设置柱形图的颜色$bplot->SetFillColor('orange'); //设置显示数字 $bplot->value->Show();//在柱形图中显示格式化的图书销量$bplot->value->SetFormat('%d');//将柱形图添加到图像中$graph->Add($bplot);//设置画布背景色为淡蓝色$graph->SetMarginColor("lightblue");//创建标题$graph->title->Set("《PHP》");//设置X坐标轴文字$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");$graph->xaxis->SetTickLabels($a); //设置字体$graph->title->SetFont(FF_SIMSUN);$graph->xaxis->SetFont(FF_SIMSUN); //输出矩形图表$graph->Stroke();?>

使用折线图统计

img->SetMargin(50,40,30,40); //设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素 $graph->img->SetAntiAliasing(); //设置折线的平滑状态 $graph->SetScale("textlin"); //设置刻度样式 $graph->SetShadow(); //创建画布阴影 $graph->title->Set("2000年PHP图书月销售额折线图"); //设置标题 $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体 $graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体 $graph->xaxis->SetPos("min"); $graph->yaxis->HideZeroLabel(); $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5'); $a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"); //X轴 $graph->xaxis->SetTickLabels($a); //设置X轴 $graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体 $graph->yscale->SetGrace(20); $p1 = new LinePlot($datay); //创建折线图对象 $p1->mark->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记 $p1->mark->SetFillColor("red"); //设置填充的颜色 $p1->mark->SetWidth(4); //设置圆形标记的直径为4像素 $p1->SetColor("blue"); //设置折形颜色为蓝色 $p1->SetCenter(); //在X轴的各坐标点中心位置绘制折线 $graph->Add($p1); //在统计图上绘制折线 $graph->Stroke(); //输出图像?>

应用3D饼形图

SetShadow(); //设置画布阴影$graph->title->Set("应用3D饼形图统计2000年商品的年销售额比率"); //创建标题$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //设置图例字体$p1 = new PiePlot3D($data); //创建3D饼形图对象$p1->SetLegends(array("IT数码","家电通讯","家居日用","服装鞋帽","健康美容","食品烟酒"));$targ=array("pie3d_csimex1.php?v=1","pie3d_csimex1.php?v=2","pie3d_csimex1.php?v=3", "pie3d_csimex1.php?v=4","pie3d_csimex1.php?v=5","pie3d_csimex1.php?v=6");$alts=array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d");$p1->SetCSIMTargets($targ,$alts);$p1->SetCenter(0.4,0.5); //设置饼形图所在画布的位置$graph->Add($p1); //将3D饼图形添加到图像中$graph->StrokeCSIM(); //输出图像到浏览器?>

应用柱形图依次统计2000年月销量

SetScale("textlin");$graph->SetShadow();$graph->SetMarginColor("yellow"); //设置画布背景色为淡蓝色$graph->img->SetMargin(40,30,40,40);$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());$graph->xaxis->title->Set('');$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);$graph->title->Set('应用柱形图依次统计2000年月销量');$graph->title->SetFont(FF_SIMSUN,FS_BOLD);$bplot1 = new BarPlot($datay1);$bplot2 = new BarPlot($datay2);$bplot1->SetFillColor("orange");$bplot2->SetFillColor("lightblue");$bplot1->SetShadow();$bplot2->SetShadow();$bplot1->SetShadow();$bplot2->SetShadow();$gbarplot = new GroupBarPlot(array($bplot1,$bplot2));$gbarplot->SetWidth(0.6);$graph->Add($gbarplot);$graph->Stroke();?>

轿车的月销量统计

SetMarginColor('red');$graph->SetScale("textlin");$graph->SetFrame(false);$graph->SetMargin(30,5,25,20);//创建标签标题、颜色、文字大小等属性$graph->tabtitle->Set(' 2000年轿车的月销量统计 ' );$graph->tabtitle->SetFont(FF_SIMSUN,FS_NORMAL,10);$graph->tabtitle->SetColor('darkred','yellow');// 设置X轴网格$graph->xgrid->Show();//应用月份做为X轴的坐标$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());//创建折线对象$p1 = new LinePlot($datay1);$p1->SetColor("navy");$p1->mark->SetType(MARK_IMG,'car.gif',0.8); //载入汽车模型标记,并限制其输出大小//输出汽车模型标记的位置$p1->value->SetFormat('%d');$p1->value->Show();$p1->value->SetColor('darkred');$p1->value->SetFont(FF_ARIAL,FS_BOLD,10);$p1->value->SetMargin(14); //设置汽车销量距汽车模型标记的距离$p1->SetCenter(); //设置汽车销量及模型在X轴各坐标点居中显示$graph->Add($p1); //添加折线图到图像中$graph->Stroke(); //输出图像到浏览器?>

统计2006年、2007年、2008年、2009年农产品的产量比率

SetShadow();//设置标题名称$graph->title->Set("统计2006年、2007年、2008年、2009年农产品的产量比率");$graph->title->SetFont(FF_SIMSUN,FS_BOLD);$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL);//创建饼形图对象$size=0.13;$p1 = new PiePlot($data1);$p1->SetLegends(array("大豆","玉米","水稻","小麦","高梁"));$p1->SetSize($size);$p1->SetCenter(0.25,0.32);$p1->value->SetFont(FF_FONT0);$p1->title->Set("2006年");$p1->title->SetFont(FF_SIMSUN,FS_BOLD);$p2 = new PiePlot($data2);$p2->SetSize($size);$p2->SetCenter(0.65,0.32);$p2->value->SetFont(FF_FONT0);$p2->title->Set("2007年");$p2->title->SetFont(FF_SIMSUN,FS_BOLD);$p3 = new PiePlot($data3);$p3->SetSize($size);$p3->SetCenter(0.25,0.75);$p3->value->SetFont(FF_FONT0);$p3->title->Set("2008年");$p3->title->SetFont(FF_SIMSUN,FS_BOLD);$p4 = new PiePlot($data4);$p4->SetSize($size);$p4->SetCenter(0.65,0.75);$p4->value->SetFont(FF_FONT0);$p4->title->Set("2009年");$p4->title->SetFont(FF_SIMSUN,FS_BOLD);$graph->Add($p1);$graph->Add($p2);$graph->Add($p3);$graph->Add($p4);$graph->Stroke();?>

文件系统

文件是用来存储数据的方式之一。

打开文件,关闭文件,读写文件,操作文件。

打开文件

resource fopen ( string filename, string mode [, bool use_include_path]);

关闭文件

bool fclose ( resource handle ) ;

读取数据

可以读取一个字符,一行字串,整个文件
读取文件:readfile()、file()和file_get_contents()

readfile()函数

读入一个文件并将其写入到输出缓冲

int readfile(string filename)

file()函数读取整个文件的内容

file()函数将文件内容按行存放到数组中

array file(string filename)

file_get_contents()函数

将文件内容(filename)读入一个字符串

string file_get_contents(string filename[,int offset[,int maxlen]])

案例:

读取整个文件
使用readfile()函数读取文件内容:
使用file()函数读取文件内容:
"; } ?>
使用file_get_contents()函数读取文件内容:

读取一行数据:fgets()和fgetss()

fgets()函数用于一次读取一行数据

string fgets( int handle [, int length] )

fgetss()函数是fgets()函数的变体,用于读取一行数据

string fgetss ( resource handle [, int length [, string allowable_tags]] )

fgets和fgetss的区别

fgets和fgetss的区别
使用fgets函数:
使用fgetss函数:

读取一个字符:fgetc() 读取任意长度的字串:fread()

string fgetc ( resource handle )
使用fgetc函数读取字符
string fread ( int handle, int length )
使用fread函数读取文件
"; echo fread($fp,filesize($filename));?>

写入文件

使用fwrite()和file_put_contents()函数向文件中写入数据

fwrite()函数

int fwrite ( resource handle, string string [, int length] )

file_put_contents()函数

int file_put_contents ( string filename, string data [, int flags])

使用fwrite和file_put_contents函数写入数据

使用fwrite和file_put_contents函数写入数据
"; echo "用fwrite写入文件:"; $fopen = fopen($filepath,'wb') or die('文件不存在'); fwrite($fopen,$str); fclose($fopen); readfile($filepath); echo "

用file_put_contents写入文件:"; file_put_contents($filepath,$str); readfile($filepath);?>

操作文件

bool copy();bool rename();bool unlink();int fileatime();int filemtime();int filesize();array pathinfo();string realpath();

目 录 处 理

打开/关闭目录

浏览目录
操作目录

opendir()函数

resource opendir ( string path)

closedir()函数

void closedir ( resource handle )

案例:

浏览目录,scandir()函数

array scandir ( string directory [, int sorting_order ])
浏览目录
"; } }else{ echo "目录路径错误!"; }?>

操作目录

bool mkdirbool rmdirstring getcwdbool chdirfloat disk_free_spacestring readdirvoid rewinddir

高级应用

远程文件访问
文件指针
锁定文件

php.ini中配置,找到allow_url_fopen,设为ON

fopen('http://127.0.0.1/index.php','rb');

文件指针

1.rewind()函数

bool rewind ( resource handle )

2.fseek()函数

int fseek ( resource handle, int offset [, int whence] )

handle参数为要打开的文件

offset为指针位置或相对whence参数的偏移量,可以是负值
whence的值包括3种:
SEEK_SET,位置等于offset字节
SEEK_CUR,位置等于当前位置加上offset字节
SEEK_END,位置等于文件尾加上offset字节
如果忽略whence参数,系统默认为SEEK_SET

3.feof()函数

bool feof ( resource handle )

4.ftell()函数

int ftell ( resource handle )
文件指针函数
"; $fopen = fopen($filename,'rb'); echo "初始指针位置是:".ftell($fopen)."
"; fseek($fopen,33); echo "使用fseek()函数后指针位置:".ftell($fopen)."
"; echo "输出当前指针后面的内容:".fgets($fopen)."
"; if(feof($fopen)) echo "当前指针指向文件末尾:".ftell($fopen)."
"; rewind($fopen); echo "使用rewind()函数后指针的位置:".ftell($fopen)."
"; echo "输出前33字节的内容:".fgets($fopen,33); fclose($fopen);}else{ echo "文件不存在";}?>

锁定文件

bool flock ( int handle, int operation)LOCK_SH取得共享锁定(读取程序)LOCK_EX取得独占锁定(写入程序)LOCK_UN释放锁定LOCK_NB防止flock()在锁定时堵塞operation的参数
使用flock函数

文件上传

文件上传是要通过http协议来实现的,要在php.ini文件中进行对上传文件的设置,要了解$_FILES变量和函数move_uploaded_file()函数实现上传。

要配置php.ini文件,在文件中找到file_uploads,如果值为on,说明服务器支持文件上传,如果为off,则表示不支持,upload_tmp_dir为上传文件临时位置,系统默认,也可以自己定。

upload_max_filesize为服务器允许上传的文件的最大值,默认2mb。max_execution_timePHP中一个指令所能执行的最大时间,memory_limitPHP中一个指令所分配的内存空间。

$_FILES[filename][name]上传文件的文件名$_FILES[filename][size]文件大小$_FILES[filename][tmp_name]为临时文件名$_FILES[filename][type]文件的类型$_FILES[filename][error]上传文件的结果。如果返回0,说明文件上传成功
$_FIELS变量
请选择上传文件:
$value) echo $name.' = '.$value.'
'; }?>

文件上传函数

move_uploaded_file()函数上传文件

bool move_uploaded_file ( string filename, string destination )

将上传文件存储到指定的位置

单文件上传

单文件上传
0){ move_uploaded_file($fileinfo['tmp_name'],$fileinfo['name']); echo '上传成功'; }else{ echo '文件太大或未知'; } }?>
     
   
      
     

多文件上传

多文件上传请选择要上传的文件
上传文件
上传文件
上传文件
上传文件
'; } }}?>

访问量:

fputs($fp,$counter); //将新的统计数据增加1 fclose($fp); } //关闭文 $_SESSION[temp]=1; //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1}?>
通过文本文件统计页面访问量

限制大小的文件上传

限制大小的文件上传
     
   
      
     
1000000){ echo "对不起,您上传的文件超过规定的大小!!"; echo "
将在3秒钟后返回前页..."; }else{ $path = './upfiles/'. $_FILES['files']['name']; if (move_uploaded_file($_FILES['files']['tmp_name'],$path)) { echo "上传成功!!"; echo "
"; }else{ echo "文件上传失败!!"; echo "
"; } } }?>

结言

好了,欢迎在留言区留言,与大家分享你的经验和心得。

感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。

感谢!承蒙关照!您真诚的赞赏是我前进的最大动力!

image

image

转载于:https://www.cnblogs.com/dashucoding/p/11140281.html

你可能感兴趣的文章
Linux驱动学习 —— 在/sys下面创建目录示例
查看>>
Linux下安装Android的adb驱动-解决不能识别的问题
查看>>
Why is the size of an empty class not zero in C++?
查看>>
海亮SC
查看>>
[Hibernate] - Generic Dao
查看>>
【Linux】一步一步学Linux——Linux系统常用快捷键(12) 待更新...
查看>>
Vue中computed和watch使用场景和方法
查看>>
laravel路由与控制器(资源路由restful)
查看>>
Html5移动端页面自适应布局详解(阿里rem布局)
查看>>
memoize-one在React中的应用
查看>>
SpringBoot整合JDBC数据库操作第二弹-配置基本数据库连接源
查看>>
nginx日志切割脚本
查看>>
ipvsadm添加虚拟服务器报错问题
查看>>
LVS-DR集群搭建脚本
查看>>
Docker拉取的镜像源更改为国内的镜像源
查看>>
LVS健康检查脚本
查看>>
PowerCLI 对vm批量关机
查看>>
拿来即用学PYTHON:序
查看>>
github+jenkins+maven+docker自动化构建部署
查看>>
前端禁止鼠标右键、禁止全选、复制、粘贴
查看>>