search
HomePHP FrameworkThinkPHPThinkPhp5.1 + jSignature implements online signature function

Online signatures are used in many places. The editor recently took on a project, which involves the approval signature function. The customer required that the real-time signature function on the mobile phone must be implemented. After research, the jSignature library was used to complete this Function, share it for everyone’s reference.

Since the jSignature library is used to create signatures, of course you need to download the jSignature library. The editor provides the download address as follows. Friends can go to download:

https://www .phpclasses.org/browse/file/42277.html

After getting the jSignature library, how to use it? Many friends may be stuck in this area. Similarly, the editor is Please provide documentation, you can check it out:

jsignature Chinese Development Manual

In addition, jsignature needs to be used with the jQuery library, otherwise some functions cannot be used Display, the download address is also provided for everyone to download:

linkDownloading jQuery

Of course, there are many versions of jQuery, and the editor uses jquery-3.2. 1.js

The preliminary preparation function is ready, and the production method is provided below.

Front-end HTML

<style>
    .main_sign{
        padding: 10px 10px;
        color:black;
        background-color:darkgrey;
    }
    .main_sign .sign_btn{
        padding: 5px 10px;
    }
    #signature {
        border: 2px dotted black;

    }
</style>
<div class="main_sign" id="writers">
    <div id=&#39;signature&#39; style=&#39;background-color: #d2d2e8;&#39;></div>
    <button type="button" class="sign_btn" id="reset" style="margin: 10px 5px;">重写</button>
    <button type="button" class="sign_btn" id="yes" style="margin: 10px 5px;">确认</button>
    <div id="show_img" style="display: none;"><img  src="/static/imghwm/default1.png"  data-src="" id="  class="lazy"   id="images" alt="ThinkPhp5.1 + jSignature implements online signature function" ></div>
</div>

1 Instantiate jsignature

$(document).ready(function(){
    var arguments = {
        width: &#39;100%&#39;,
        height: &#39;200px&#39;,
    };
    $("#signature").jSignature(arguments);
});

2 Reset signature

$("#reset").click(function(){
    $("#signature").jSignature("reset"); //重置画布,可以进行重新作画
    $("#images").attr(&#39;src&#39;,&#39;&#39;);
});

3 Submit signature

The editor uses TP5.1 ajax submission [Related recommendations: thinkphp video tutorial]

//点击确定按钮,把签名的转成图片,然后把数据放进图片中,最后把图片中的数据传到后台
$("#yes").click(function(){
    //将画布内容转换为图片
    var $signature = $("#signature");
    var datapair = $signature.jSignature("getData", "image");
    $("#images").attr(&#39;src&#39;,&#39;data:&#39; + datapair[0] + "," + datapair[1]);
    var src_data = $("#images").attr(&#39;src&#39;);//拿到图片中的src,这就是我们需要的base64
    //console.info(src_data);//显示生成的笔迹图片
    //在这里就写我们的后台操作
    $.ajax({
        url:"{:url(&#39;getSignInfo&#39;)}",
        data:{src_data:src_data},
        type:"post",
        dataType:"json",
        success:function(data){
            window.location.href = data.dump_url;
        },
        error:function(){
            console.log("错误");
        }
    });
});

4 Background data reception (getSignInfo.php)

$data = Request::param();
$src = $this->base64ContentToImage($data[&#39;src_data&#39;],$path);

$src is the saving address of the signature image we need, $path is the saving path of the signature image

5 Convert the image base64 code into a standard image (base64ContentToImage method)

public function base64ContentToImage($base64_image_content,$path){
    $dir = "./".$path;
    if(!file_exists($dir)){
        mkdir(iconv("GBK", "UTF-8", $dir),0777,true);
    }
    //匹配出图片的格式
    if (preg_match(&#39;/^(data:\s*image\/(\w+);base64,)/&#39;, $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path."/".date(&#39;Ymd&#39;,time())."/";
        if(!file_exists($new_file)){
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($new_file, 0700);
        }
        $new_file = $new_file.time().".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], &#39;&#39;, $base64_image_content)))){
            return &#39;/&#39;.$new_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

The above is the complete summary of the editor using the TP5.1 jSignature library to generate a signature Production method, I hope it will be helpful to everyone, thank you!

The above is the detailed content of ThinkPhp5.1 + jSignature implements online signature function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor