首頁  >  文章  >  後端開發  >  PHP中仿製 ecshop驗證碼實例

PHP中仿製 ecshop驗證碼實例

墨辰丷
墨辰丷原創
2018-05-26 16:38:281365瀏覽

這篇文章主要介紹了PHP中仿製ecshop驗證碼實例,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

仿製ecshop驗證碼的程式碼如下所示:

<?php
//仿制ecshop验证码(四位大写字母和数字、背景)
//处理码值(四位大写字母和数字组成)
//所有的可能的字符集合
$chars = &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
$chars_len = strlen($chars); //集合长度
//随机选取
$code_len = 4;//验证码长度
$code=&#39;&#39;; //验证码值初始化
for($i=0;$i<$code_len;++$i){
  //随机取得一个字符下标
  $rand_index = mt_rand(0,$chars_len-1);
  //利用字符串的下标操做,获得选择的字符
  $code .= $chars[$rand_index];
}
//echo $code;
//存储于session中(用于校验)
session_start();
$_SESSION[&#39;code&#39;] = $code;

//验证码图像(已知的背景图片)
//处理背景
$bg_file= &#39;./captcha/captcha_bg&#39; . mt_rand(1,5). &#39;.jpg&#39;;
//依据该图片,创建画布
$image = imagecreatefromjpeg($bg_file);
//简单的将字符串写在画布上的函数(imageString();)
//imageString(画布,字体,位置X, 位置y,字符串内容,颜色); 
//字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();

//分配字体颜色(随机分配黑色或者白色)
if(mt_rand(0,1)==1){
    $str_color = imagecolorallocate($image,0,0,0); //黑色
  }else{
    $str_color = imagecolorallocate($image,255,0xff,255);//白色
}
//内置5号字体
$font = 5;
//位置
//画布大小
$image_w = imagesx($image);
$image_h = imagesy($image);
//获得字体的宽和高
$font_w = imagefontwidth($font);
$font_h = imagefontheight($font);
//获得字符串的宽高
$str_w = $font_w * $code_len;
$str_h = $font_h;
//计算位置
$str_x = ($image_w-$str_w) / 2;
$str_y = ($image_h-$str_h) / 2;
//字符串
imagestring($image,$font,$str_x,$str_y,$code,$str_color);
//输出和销毁画布
header("content-type:image/jpeg");
imagejpeg($image);
imagedestroy($image);

封裝驗證碼工具類別:

#
//验证码工具类(将所有和验证码操作相关的全部封装到该类中)
    class Captcha{
      /*生成验证码*/
    public function makeImage($code_len=4){
    //仿制ecshop验证码(四位大写字母和数字、背景)
    //处理码值(四位大写字母和数字组成)
    //所有的可能的字符集合
    $chars = &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
    $chars_len = strlen($chars); //集合长度
    //随机选取
    $code=&#39;&#39;; //验证码值初始化
    for($i=0;$i<$code_len;++$i){
      //随机取得一个字符下标
      $rand_index = mt_rand(0,$chars_len-1);
      //利用字符串的下标操做,获得选择的字符
      $code .= $chars[$rand_index];
    }
    //echo $code;
    //存储于session中(用于校验)
    @session_start();
    $_SESSION[&#39;code&#39;] = $code;
    //验证码图像(已知的背景图片)
    //处理背景
    $bg_file= TOOL . &#39;./captcha/captcha_bg&#39; . mt_rand(1,5). &#39;.jpg&#39;;
    //依据该图片,创建画布
    $image = imagecreatefromjpeg($bg_file);
    //简单的将字符串写在画布上的函数(imageString();)
    //imageString(画布,字体,位置X, 位置y,字符串内容,颜色); 
    //字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();
    //分配字体颜色(随机分配黑色或者白色)
    if(mt_rand(0,1)==1){
        $str_color = imagecolorallocate($image,0,0,0); //黑色
      }else{
        $str_color = imagecolorallocate($image,255,0xff,255);//白色
    }
    //内置5号字体
    $font = 5;
    //位置
    //画布大小
    $image_w = imagesx($image);
    $image_h = imagesy($image);
    //获得字体的宽和高
    $font_w = imagefontwidth($font);
    $font_h = imagefontheight($font);
    //获得字符串的宽高
    $str_w = $font_w * $code_len;
    $str_h = $font_h;
    //计算位置
    $str_x = ($image_w-$str_w) / 2;
    $str_y = ($image_h-$str_h) / 2;
    //字符串
    imagestring($image,$font,$str_x,$str_y,$code,$str_color);
    //输出和销毁画布
    header("content-type:image/jpeg");
    imagejpeg($image);
    imagedestroy($image);
  }
}

以上就是本文的全部內容,希望對大家的學習有幫助。


相關推薦:

ECSHOP中實作ajax彈窗登入功能

##詳解Ecshop後台如何新增功能及權限設定

ECSHOP中Deprecated: preg_replace()報錯的解決方案

#

以上是PHP中仿製 ecshop驗證碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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