计数器
php具有极其强大的图像处理能力,用它可以很轻易的动态生成web图像。一下是一个使用php做成的一个简单计数器。
1. 总体思路:
把以往的访问人数记录在一个文本文件中,当网页被访问的时候,从打开该文件
并从中读出以往的访问人数,加 1,得到最新的访问人数,并把该数目格式化成
标准的格式,再调用图像处理函数,把该数字输出成图片,再把新的访问数字回
写到纪录访问人数的文件中。
2. 程序所用到的函数说明:
A. 相关的文件操作:
a. 打开文件:
函数的原型:int fopen(string filename, string mode);
返回结果:如果打开文件成功,函数返回文件流指针,否则返回 FALSE(0)。
参数说明:
string filename -- 要打开的文件名,必须为字符串形式。
例如, "zzm.txt"、"..\zzm.txt"等。
string mode -- 打开文件的方式,必须为字符形式。
'r',只读形式,文件指针指向文件的开头
'r ',可读可写,文件指针指向文件的开头
'w',只写形式,文件指针指向文件的开头,把文件长度截成0,
如果文件不存在,将尝试建立文件。
'w ',可读可写,文件指针指向文件的开头,把文件长度截成0,
如果文件不存在,将尝试建立文件。
'a',追加形式(只可写入),文件指针指向文件的最后,如果文
件不存在,将尝试建立文件。
'a ',可读可写,文件指针指向文件的最后,如果文件不存在,
将尝试建立文件。
例子:用只读形式打开当前目录下面的"zzm.txt"
$fp = fopen("zzm.txt", "r");
b. 关闭文件:
函数原型:int fclose(int fp);
返回结果:成功返回1,失败返回0
参数说明:int fp是 fopen函数返回的文件流指针。
例子:关闭干刚才用fopen打开的zzm.txt文件
fclose($fp);
c. 读文件:
函数原型:string fgets(int fp, int length);
返回结果:返回 length -1 长度的字符串,如果到文件结尾,返回 EOF(End Of File)
参数说明:
int fp -- 要读入数据的文件流指针,由fopen函数返回的数值
int length -- 读入的字符个数,实际读入字符个数为 length -1 个
例子:从 $fp 中读取9个字符
$str1 = fgets($fp,10);
d. 写文件:
函数原型:int fputs(int fp, string str, int [length]);
返回结果:和fclose同
参数说明:
int fp -- 要写入信息的文件流指针,由fopen函数返回的数值
string str -- 要写入文件的字符串。
int length -- 写入的长度,可选的,如果不提供length,则整个串将被写入,
否则,写入length长度个字符。
例子:向 $fp 写入 "0000000001"
fput($fp, "0000000001");
B. 相关的字符串函数:
a. 计算字符串长度:
函数原型:int strlen(string str);
返回结果:返回字符串的长度
参数说明:
string str -- 要计算长度的字符串
例子:计算 "000000000" 的字符串长度
$str2 = "000000000";
$len2 = strlen($str);
b. 字符串相加:最简单不过了,用一个 . 把两个字符串连接起来。
例子:把 $str1和$str2相加
$str = $str1.$str2
C. 相关的图形函数:
a. 新建图像:
函数原型:int imagecreate(int x_size, int y_size);
返回结果:返回一个 X*Y 像素大小的空图像识别号(ImageID)
参数说明:x_size,y_size分别是新建图像的宽度和高度(以像素为单位)
例子:新建一个 88*31 像素大小的空图片
$ImageID = imagecreate(88, 31);
b. 给图像分配一种颜色:
函数原型:int imagecolorallocate(int im, int red, int green, int blue);
返回结果:给图像($im)返回一个RGB颜色识别号
参数说明:int im 图像识别号
int red、green、blue分别是红绿蓝三种颜色的分量,取值范围 0 - 255
例子:给图像$im 分配一个识别号为$white白色颜色,白色的RGB为(255,255,255)
$white = imagecolorallocate($im, 255, 255, 255);
c. 给图像填充颜色:
函数原型:int imagefill(int im, int x, int y, int col);
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
int x, int y,从图像的(x,y)坐标开始填充颜色
(0,0)表示图像的左上角
int col,颜色的识别号
例子:从图像的左上角开始(即整个图片)填入黑色(已经用imagecolorallocate函数
定义了黑色的颜色识别号为$black了)。
imagefill($im, 0, 0, $black);
d. 计算图像的宽度:
函数原型:int imagesx(int im);
返回结果:返回图像的宽度(单位为像素)
参数说明:int im,图像的识别号。
例子:计算图像$im的宽度
$px = imagesx($im);
e. 在图像中写入水平文字:
函数原型:int imagestring(int im, int font, int x, int y, string s, int col)
;
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
int font,字体识别号,内建字体1到5,用户可以用imageloadfont()自己?
?
载字体.
int x,int y,开始写入字体的坐标,(0,0)为图片的左上角。
string s,要写入的字串
int col,字体的颜色识别号
例子:在图像(3,3)位置写入字号为3,颜色为白(已经用imagecolorallocate()函数
定义了黑色的颜色识别号为$white)的字串"E&J Counter"
ImageString($im, 3, 3, 3, "E&J Counter", $white);
f. 在图像中划直线:
函数原型:int imageline(int im, int x1, int y1, int x2, int y2, int col);
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
int x1,int y1,划线的起坐标
int x2,int y2,划线的止坐标
int col,线的颜色识别号
例子:在图像$im中划一条从(1,14)到(85,14)颜色为$white的直线
imageline($im, 1, 14, 85, 14, $white);
g. 把图像输出成GIF格式:
函数原型:int imagegif(int im, string filename);
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
string filename,生成图片的名字,可选的,如果filename为空,则直接?
氖涑?
图片,你需要用Header("Content-type: image/gif")预先定义php输出的内
容为图片
例子:把图像$im输出成文件名为"image1.gif"的图片
imagegif($im, "image1.gif");
h. 释放图像:
函数原型:int imagedestroy(int im);
返回结果:成功返回1,否则返回0
参数说明:int im,要释放的图像识别号。该函数会释放识别号im的图像及图像所占
用的系统资源。
例子:释放图像$im
imagedestroy($im);
3. 如何安装这个计数器:
A. 系统必须安装了PHP解释器。PHP可以在http://www.php.net/上下载,在该站点上还有很
详细
的技术资料可以浏览或者下载阅读。如何安装PHP请参照它自己的说明。
B. 把下面的程序清单拷贝到一个文件中,并取扩展名为php,放入能够运行php脚本的目录?
旅妫?
并在目录下面建立一个纯文本文件,名字为zzm.txt。这个文件的作用是用来记录以往的
访问人
数用的。你可以预先设置计数器的初始值,例如5000。
C. 在网页如何调用这个计数器?你可以通过以下方式来调用:

附:完整的程序清单
Header("Content-type: image/gif");
//定义输出为图像类型
$fp = fopen("zzm.txt", "r");
//以读形式打开记录以往访问人数的文件zzm.txt
$str1 = fgets($fp,10);
//从文件中读入9个字符,本计数器最大能记录的访问人数为999999999
$str1 ;
//计数器加入
fclose($fp);
//关闭文件
$fp = fopen("zzm.txt", "w");
//一写的方式打开记录访问人数的文件zzm.txt
fputs($fp, $str1);
//把最新的访问人数写入文件
fclose($fp);
//关闭文件
/*
以下是把访问人数格式化输出,如果访问人数位数不够9位,例如时5000(4位),
则把访问人数变换成000005000的形式输出。方法是计算访问人数的位数,并且
把它和000000000的位数(9位)比较,得到相差的位数,然后在数字前面不上相
应个0。例如5000,和000000000两者的长度相差5,因此要在5000前面补5个0。
*/
$len1 = strlen($str1);
//计算访问人数的位数
$str2 = "000000000";
$len2 = strlen($str);
//定义计数器最大的计数位数
$dif = $len2 - $len1;
//计算两者的位数之差,即前面要补的0的个数
$rest = substr($str2, 0, $dif);
//截取要补的0
$string = $rest.$str1;
//前面补0
$font = 4;
//定义字号
$im = imagecreate(88,31);
//新建图象
$black = ImageColorAllocate($im, 0,0,0);
//定义黑色
$white = ImageColorAllocate($im, 255,255,255);
//定义白色
imagefill($im, 0,0,$black);
//把计数器的底色设置成黑色
$px = (imagesx($im)-8.3*strlen($string))/2;
//根据字串的长度,计算字串开始写入的水平坐标,目的是尽量让字串水平对中
ImageString($im,3,$px,2,"E&J Counter",$white);
//向图象写入"E&J Counter"
imageline($im, 1, 14, 85, 14, $white);
//划一根水平线
ImageString($im,$font,$px,15.5,$string,$white);
//写访问人数
ImageGif($im);
//把图象输出成GIF格式
ImageDestroy($im);
//释放图象
?>

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)