php拼图验证码,旋转拼图验证码

本文目录一览:

php的图片验证码代码

这个是phpcms的验证码,经过十几万个网站经验的,非常好用

?php

session_start();

$enablegd = 1;

//判断图像处理函数是否存在

$funcs = array(‘imagecreatetruecolor’,’imagecolorallocate’,’imagefill’,’imagestring’,’imageline’,’imagerotate’,’imagedestroy’,’imagecolorallocatealpha’,’imageellipse’,’imagepng’);

foreach($funcs as $func)

{

if(!function_exists($func))

{

$enablegd = 0;

break;

}

}

ob_clean(); //清理缓冲

if($enablegd)

{

//create captcha

$consts = ‘cdfgkmnpqrstwxyz23456’;

$vowels = ‘aek23456789’;

for ($x = 0; $x 6; $x++)

{

$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数

$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数

}

$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];

$_SESSION[‘checkcode’] = $string = substr($radomstring,0,4); //显示4个字符

$imageX = strlen($radomstring)*8; //图像的宽

$imageY = 20; //图像的高

$im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像

//creates two variables to store color

$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色

$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),

imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))

);

$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度

$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景

$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2

//与左上角的颜色相同的都会被填充

imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));

//往图像上写入文字

imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ALGER.TTF’, $string[0]);

imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ARIALNI.TTF’, $string[1]);

imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ALGER.TTF’, $string[2]);

imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/arial.ttf’, $string[3]);

//画边框

$border = imagecolorallocate($im, 133, 153, 193);

imagerectangle($im, 0, 0, $imageX – 1, $imageY – 1, $border);

//画一些随机出现的点

$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

for ($i=0;$i80;$i++)

{

imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);

}

//画随机出现的线

for ($x=0; $x9;$x++)

{

if(mt_rand(0,$x)%2==0)

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆

}

else

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);

}

}

//output to browser

header(“content-type:image/png\r\n”);

imagepng($im);

imagedestroy($im);

}

else

{

$files = glob(XINCHENG_ROOT.’images/checkcode/*.jpg’);

if(!is_array($files)) die(‘请检查文件目录完整性:/images/checkcode/’);

$checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件

$_SESSION[‘checkcode’] = substr(basename($checkcodefile), 0, 4); //获得文件名

header(“content-type:image/jpeg\r\n”);

include $checkcodefile;

}

?

PHP滑动拼图验证码的图片是怎样生成的

1 如果放在项目中用,验证码图片希望可以是接口返回。ImageView以及其子类支持花式加载图片。

2 继承自ImageView,绘制图片本身不用我们干预,也不用我们操心scaleType,节省很多工作。

* 在onSizeChanged()方法中生成 和 控件宽高相关的属性值:

1 初始化时随机生成验证码区域起点

2 生成验证码区域Path

3 生成滑块Bitmap

* onDraw()时,依次绘制:

1 验证码阴影

2 滑块

php图片验证码实现

可以用php的GD库做

//随机生成验证码

class randomString

{

function createRandomStr($strLen)

{

list($usec, $sec) = explode(‘ ‘, microtime());

(float) $sec + ((float) $usec * 100000);

$number = ”;

$number_len = $strLen;

$stuff = ‘1234567890abcdefghijklmnopqrstuvwxyz’;//附加码显示范围ABCDEFGHIJKLMNOPQRSTUVWXYZ

$stuff_len = strlen($stuff) – 1;

for ($i = 0; $i $number_len; $i++) {

$number .= substr($stuff, mt_rand(0, $stuff_len), 1);

}

return $number;

}

}

通过ZD库将验证码变成图片

$number = $createStr-createRandomStr(‘4’);//验证码的位数

$number_len = strlen($number);

$_SESSION[“VERIFY_CODE”] = $number;

// 生成验证码图片

$img_width = 60;

$img_height = 20;

$img = imageCreate($img_width, $img_height);

ImageColorAllocate($img, 0x6C, 0x74, 0x70);

$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);

$ix = 6;

$iy = 2;

for ($i = 0; $i $number_len; $i++) {

imageString($img, 5, $ix, $iy, $number[$i], $white);

$ix += 14;

}

for($i=0;$i200;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);

}

// 输出图片

header(“Content-type: ” . image_type_to_mime_type(IMAGETYPE_PNG));

imagepng($img);

imagedestroy($img);

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/297867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-28 12:17
下一篇 2024-12-28 12:17

相关推荐

  • PHP和Python哪个好找工作?

    PHP和Python都是非常流行的编程语言,它们被广泛应用于不同领域的开发中。但是,在考虑择业方向的时候,很多人都会有一个问题:PHP和Python哪个好找工作?这篇文章将从多个方…

    编程 2025-04-29
  • 心形照片拼图模板

    如何使用心形照片拼图模板 一、模板介绍 心形照片拼图模板是一种让用户可以将自己的照片拼接成一个心形的巧妙设计,每个照片都是一个拼图块,当所有的照片配合完成时,呈现出一个完整的心形。…

    编程 2025-04-29
  • PHP怎么接币

    想要在自己的网站或应用中接受比特币等加密货币的支付,就需要对该加密货币拥有一定的了解,并使用对应的API进行开发。本文将从多个方面详细阐述如何使用PHP接受加密货币的支付。 一、环…

    编程 2025-04-29
  • 使用PHP foreach遍历有相同属性的值

    本篇文章将介绍如何使用PHP foreach遍历具有相同属性的值,并给出相应的代码示例。 一、基础概念 在讲解如何使用PHP foreach遍历有相同属性的值之前,我们需要先了解几…

    编程 2025-04-28
  • PHP获取301跳转后的地址

    本文将为大家介绍如何使用PHP获取301跳转后的地址。301重定向是什么呢?当我们访问一个网页A,但是它已经被迁移到了另一个地址B,此时若服务器端做了301重定向,那么你的浏览器在…

    编程 2025-04-27
  • PHP登录页面代码实现

    本文将从多个方面详细阐述如何使用PHP编写一个简单的登录页面。 1. PHP登录页面基本架构 在PHP登录页面中,需要包含HTML表单,用户在表单中输入账号密码等信息,提交表单后服…

    编程 2025-04-27
  • PHP与Python的比较

    本文将会对PHP与Python进行比较和对比分析,包括语法特性、优缺点等方面。帮助读者更好地理解和使用这两种语言。 一、语法特性 PHP语法特性: <?php // 简单的P…

    编程 2025-04-27
  • PHP版本管理工具phpenv详解

    在PHP项目开发过程中,我们可能需要用到不同版本的PHP环境来试验不同的功能或避免不同版本的兼容性问题。或者我们需要在同一台服务器上同时运行多个不同版本的PHP语言。但是每次手动安…

    编程 2025-04-24
  • PHP数组去重详解

    一、array_unique函数 array_unique是php中常用的数组去重函数,它基于值来判断元素是否重复,具体使用方法如下: $array = array(‘a’, ‘b…

    编程 2025-04-24
  • PHP导出Excel文件

    一、PHP导出Excel文件列宽调整 当我们使用PHP导出Excel文件时,有时需要调整单元格的列宽。可以使用PHPExcel类库中的setWidth方法来设置单元格的列宽。下面是…

    编程 2025-04-24

发表回复

登录后才能评论