目录

开箱可用, 此项目的作者在代码中做了详细的注释。

https://github.com/941477276/Tablet/tree/master

将它集成到你的项目中需要四个文件。

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" id="viewport" name="viewport">
  6. <meta name="format-detection" content="telephone=no, email=no" />
  7. <meta name="wap-font-scale" content="no">
  8. <meta name="apple-touch-fullscreen" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="HandheldFriendly" content="true">
  11. <meta name="MobileOptimized" content="320">
  12. <meta name="screen-orientation" content="portrait">
  13. <meta name="x5-orientation" content="portrait">
  14. <meta name="x5-page-mode" content="app">
  15. <meta name="msapplication-tap-highlight" content="no">
  16. <meta name="apple-mobile-web-app-capable" content="yes">
  17. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  18. <title>用户签名</title>
  19. <link rel="stylesheet" href="your-path/colpick.css" />
  20. <style>
  21. body,canvas{
  22. padding: 0;
  23. margin: 0;
  24. background-color: #f0f0f0;
  25. }
  26. *{
  27. box-sizing: border-box;
  28. padding: 0;
  29. margin: 0;
  30. }
  31. .container{
  32. height: 100%;
  33. }
  34. .container .-tablet,
  35. .container .-tablet-container,
  36. .container .-canvas-wrapper{
  37. height: 100%;
  38. }
  39. /* 签字板横屏处理*/
  40. @media all and (orientation : portrait) {
  41. .layui-m-layermain {
  42. transform: rotate(90deg) !important;
  43. }
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="container" id="container"></div>
  49. <script type="text/html" id="temp">
  50. <span class="save-canvas-to-img">
  51. 确认签名
  52. </span>
  53. </script>
  54. <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script>
  55. <script src="your-path/layer.js"></script>
  56. <script src="your-path/Tablet-1.0.js"></script>
  57. <script>
  58. let tablet;
  59. $(function (){
  60. tablet = new Tablet("#container",{
  61. defaultColor: "#000", //笔的默认颜色
  62. otherHtml: $("#temp").html(), //外部功能小部件
  63. response: true, //开启响应式
  64. onInit: function (){
  65. let that = this,
  66. container = this.container;
  67. container.find(".save-canvas-to-img").on("click", function (){
  68. /* 直接获取 base64 编码的图片*/
  69. let signImg = that.getBase64()
  70. console.log(signImg)
  71. layer.open({
  72. content: '确定提交自己的个人签名么?'
  73. ,btn: ['确定', '取消']
  74. ,yes: function(index){
  75. layer.close(index)
  76. $.ajax({
  77. url: "your-url",
  78. method: 'post',
  79. data: {
  80. signImg: signImg
  81. },
  82. success: function(data) {
  83. /* 这里返回数据根据你的实际情况处理*/
  84. let status = data.result.status
  85. let result = data.result.result
  86. if (status === 200) {
  87. layer.open({
  88. time: 1,
  89. title: [
  90. '提示信息',
  91. 'background-color: green; color:#fff;'
  92. ]
  93. ,content: result.toString()
  94. });
  95. } else {
  96. layer.open({
  97. time: 1,
  98. title: [
  99. '提示信息',
  100. 'background-color: #FF4351; color:#fff;'
  101. ]
  102. ,content: result.toString()
  103. });
  104. }
  105. },
  106. error: function (error) {}
  107. })
  108. that.clear()
  109. },no: function (index) {
  110. layer.close(index)
  111. that.clear()
  112. }
  113. })
  114. })
  115. }
  116. });
  117. /* 横屏处理,这里简单粗暴,如果屏幕转动,直接重载页面。*/
  118. var evt = "onorientationchange" in window ? "orientationchange" : "resize";
  119. window.addEventListener(evt,resize,false);
  120. window.orientation = 90;
  121. var oldOrientation = window.orientation;
  122. function resize(fals) {
  123. if(window.orientation !== oldOrientation) {
  124. window.document.location.reload()
  125. oldOrientation = window.orientation
  126. }
  127. if (window.orientation === 0 || window.orientation === 180) {
  128. tablet.rotate(90);
  129. }
  130. }
  131. resize(true);
  132. });
  133. </script>
  134. </body>
  135. </html>
  1. /* 处理 ajax 传来的 base64编码*/
  2. function userSignData($signImg)
  3. {
  4. $dest = 'your-path/signImg'.uniqid().'.png';
  5. $base64 = explode(',', $signImg);
  6. /* 这里默认当成 png 处理了,处女座请自己完善*/
  7. $pngCode = base64_decode(end($base64));
  8. file_put_contents($dest, $pngCode);
  9. $res = compressImg($dest, $dest, 0.5);
  10. if ($res) {
  11. /* 这里就是压缩后的图片编码*/
  12. $base64NewImg = base64_encode(file_get_contents($dest));
  13. /* 删除保存的图片,当然你可以不删除,注释以下即可*/
  14. unlink($dest);
  15. return ['status' => 200, 'result' => '已提交签名'];
  16. } else {
  17. return ['status' => 500, 'result' => '巴拉巴拉巴拉'];
  18. }
  19. }
  20. /* 压缩图片*/
  21. function compressImg($source, $dest, $quality = 0.7)
  22. {
  23. // 判断原图是否存在
  24. if(!file_exists($source)){
  25. return false;
  26. }
  27. // 获取原图信息
  28. list($owidth, $oheight, $otype) = getimagesize($source);
  29. $newWidth = $owidth * $quality;
  30. $newHeight = $oheight * $quality;
  31. /* 由于签名是透明背景的 png,这里创建一个透明背景的新图*/
  32. $newImg = imagecreatetruecolor($newWidth, $newHeight);
  33. $color=imagecolorallocate($newImg,255,255,255);
  34. imagecolortransparent($newImg,$color);
  35. imagefill($newImg,0,0,$color);
  36. switch($otype){
  37. case 1: $source_img = imagecreatefromgif($source); break;
  38. case 2: $source_img = imagecreatefromjpeg($source); break;
  39. case 3: $source_img = imagecreatefrompng($source); break;
  40. default:
  41. return false;
  42. }
  43. imagecopyresampled($newImg, $source_img, 0, 0, 0, 0, $newWidth, $newHeight, $owidth, $oheight);
  44. // 生成图片
  45. switch($otype){
  46. case 1: imagegif($newImg, $dest); break;
  47. case 2: imagejpeg($newImg, $dest); break;
  48. case 3: imagepng($newImg, $dest); break;
  49. }
  50. imagedestroy($source_img);
  51. imagedestroy($newImg);
  52. return is_file($dest)? true : false;
  53. }

版权声明:本文为my3306原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/my3306/p/9895920.html