首頁 php教程 PHP源码 PHP高自定义性安全验证码

PHP高自定义性安全验证码

May 25, 2016 pm 05:12 PM

php代码

<?php
public function captcha(){
    $font_dir   = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf";	// 字体库
    $img_w      = 58;   // 设置图片宽
    $img_h      = 20;   // 设置图片高
    $font_size  = 11;   // 字体大小
    $angle_l    = -10;  // 左偏角
    $angle_r    = 10;   // 右偏角
    $code_str   = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
    $word_len   = 4;    // 验证码位数
    $padding    = 5;    // 每两个文字之间间隔
    $margin     = 2;    // 左侧边距
    $base_line  = 15;   // 文字基线位置
    $base_line_offset = 2;  // 基准线偏移量
    $pixel_num  = 3;    // 杂点数目基数
    $pixel_color= 8;    // 杂点只有 $pixel_color 种颜色  总的杂点数为$pixel_num*$pixel_color
    $noise_font_size = 1; // 杂点字体大小
	$session_key= "my_captcha";	//自定义session键名

    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Pragma: no-cache");
    header("Cache-control: private");
    header(&#39;Content-Type: image/png&#39;);

    session_start();
    $word = "";
    $code_str_len = strlen($code_str) - 1;
    for ($i = 0; $i < $word_len; $i++) {
        $word.= $code_str[rand(0, $code_str_len)];
    }
    $_SESSION [$session_key] = strtolower($word);
    $image = imagecreatetruecolor($img_w, $img_h);
    imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255)));
  
    //绘制杂点
    for($i = 0; $i < $pixel_color; $i++){ 
        $noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) );
        for($j = 0; $j < $pixel_num; $j++) {
            imagestring( $image,  $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color );
        }
    }
    
    //绘制文字
    for ($i = 0; $i < $word_len; ++$i) {           
        $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
        imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font_dir, mb_substr($word, $i, 1, &#39;utf-8&#39;));
        $margin += (imagefontwidth($font_size) + $padding);
    }

    imagepng($image);
    imagedestroy($image);
    exit;
}
登入後複製

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1317
25
PHP教程
1268
29
C# 教程
1246
24