首页 > 编程语言 >PHP构造验证码

PHP构造验证码

时间:2022-11-11 15:34:29浏览次数:47  
标签:rand img 200 验证码 构造 imagecolorallocate 100 PHP 255


代码如下:

<?php
header('Content-type:image/jpeg');
$width=120;
$height=40;
$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string='';
for ($i=0;$i<5;$i++){
<span style="white-space:pre"> </span>$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width, $height);//设置图片大小
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//随机产生背景色
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//随机产生背景色
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBg);//设置图片背景色
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);//构建矩形边框
for($i=0;$i<100;$i++){//画一百个小像素
<span style="white-space:pre"> </span>imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));//画一个单一的像素
}
for($i=0;$i<3;$i++){//画三条随机线
<span style="white-space:pre"> </span>imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
<span style="white-space:pre"> </span>//前两个为起点坐标,后两个为终点坐标,起点控制在左半边,终点控制在右半边
}
//imagestring($img,5,0,0,'abcd',$colorString);
imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);//绘制文字,可以选择丰富的字体的方法
//说明:array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
imagejpeg($img);
imagedestroy($img);


重点介绍

imagettftext()方法:



array imagettftext ( resource$image , float$size , float$angle , int$x , int$y , int$color , string$fontfile , string$text



image

图像资源。见 ​​imagecreatetruecolor()​​。

size

字体大小。根据 GD 版本不同,应该以像素大小指定(GD1)或点大小(GD2)。 angle

角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。 x

xy 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和​​imagestring()​

y

Y 坐标。它设定了字体基线的位置,不是字符的最底端。 color

颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见 ​​imagecolorallocate()​​。

fontfile

是想要使用的 TrueType 字体的路径。 根据 PHP 所使用的 GD 库的不同,当fontfile 没有以/ 开头时则 .ttf 将被加到文件名之后并且会在库定义字体路径中尝试搜索该文件名。

当使用的 GD 库版本低于 2.0.18 时,一个空格字符 而不是分号将被用来作为不同字体文件的“路径分隔符”。不小心使用了此特性将会导致一条警告信息:Warning: Could not find/open font。对受影响的版本来说唯一解决方案就是将字体移动到不包含空格的路径中去。


很多情况下字体都放在脚本的同一个目录下。下面的小技巧可以减轻包含的问题。



​<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>​



text

文本字符串。 可以包含十进制数字化字符表示(形式为:&#8364;)来访问字体中超过位置 127 的字符。UTF-8 编码的字符串可以直接传递。 如果字符串中使用的某个字符不被字体支持,一个空心矩形将替换该字符。

imagettftext()


Example #1 imagettftext()



本例中的脚本将生成一个白色的 400x30 像素 PNG 图像,其中有黑色(带灰色阴影)Arial 字体写的“Testing...”。

​​<?php
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>​​

标签:rand,img,200,验证码,构造,imagecolorallocate,100,PHP,255
From: https://blog.51cto.com/u_15866446/5845037

相关文章

  • thinkPHP查询数据库常用函数
      1.find()  查询一条数据2.field()  查询的字段如field('id,name,age')3.select()  查询多条数据4.setField()  修改一个字段或多个字段值  如se......
  • PHP代码
    //要访问的目标页面$targetUrl="http://ip.hahado.cn/ip";//$targetUrl="http://ip.hahado.cn/switch-ip";//$targetUrl="http://ip.hahado.cn/curr......
  • PHP判断内网/外网IP
        工作中用到PHP来判断内外网IP,查找资料偶然发现已有现成的实现函数,cool!filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_R......
  • php 获取文件mime类型的方法
     php获取文件mime类型的方法 1.使用mime_content_type方法stringmime_content_type(string$filename)ReturnstheMIMEcontenttypeforafileasdeterminedb......
  • centos7安装php8
    原文: http://www.manongjc.com/detail/25-qpyxndyogppmfdf.html前言centos7默认源的php版本只有5.4,版本太老,而mediawiki需要的php版本为7.4以上,所以本文直接升级到php8......
  • java 实现 PHP password_hash() password_verify() 单向验证
    近期一个php转java项目中遇到。写出来分享一下:javaBCrypt类库。​​https://github.com/patrickfav/bcrypt​​@TestpublicvoidtestBCrypt(){Stringp......
  • 构造函数
    构造函数构造函数在创建实例后,是无法在进行添加属性的,所以每个构造函数都有一个prototype属性,这个属性指定一个原型对象,这个原型对象上的所有属性和方法都可以被构造函数......
  • 学习笔记-Frida构造数组,对象,Map和类参数
    Frida构造数组,对象,Map和类参数数组/(字符串)对象数组/gson/Java.array对象/多态,强转Java.cast接口interface,Java.register枚举,泛型,List,Map,Set,迭代打印重要思路:开......
  • 浅谈PHP设计模式的模板方法模式
    简介:模板方法模式,是行为型的设计模式。定义一个操作中的算法的骨架,而将一些步骤延迟到子类当中,使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。通......
  • 浅谈PHP设计模式的原型模式
    简介:原型模式,属于创建型模式的一种。主要针对对象进行克隆,把被克隆的对象称之为原型,原型模式称之为克隆模式也许更为贴切。用原型实例指定创建对象的种类,并且通过拷贝这......