加载中...

ThinkPHP 整合PhpQrcode生成二维码


先安照路径放好如图。
5858e28b01f1b.png

简单使用无logo:

  1. public function qrcode(){
  2. Vendor('phpqrcode.phpqrcode');
  3. //生成二维码图片
  4. $object = new \QRcode();
  5. $url='http://www.shouce.ren/';//网址或者是文本内容
  6. $level=3;
  7. $size=4;
  8. $errorCorrectionLevel =intval($level) ;//容错级别
  9. $matrixPointSize = intval($size);//生成图片大小
  10. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  11. }

高级使用带logo:

  1. public function qrcode(){
  2. Vendor('phpqrcode.phpqrcode');
  3. //生成二维码图片
  4. $object = new \QRcode();
  5. $qrcode_path='';
  6. $file_tmp_name='';
  7. $errors=array();
  8. if(!empty($_POST)){
  9. $content = trim($_POST['content']); //二维码内容
  10. $contentSize=$this->getStringLength($content);
  11. if($contentSize>150){
  12. $errors[]='字数过长,不能多于150个字符!';
  13. }
  14. if(isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])){
  15. if($_FILES['upimage']['size']>512000){
  16. $errors[]="你上传的文件过大,最大不能超过500K。";
  17. }
  18. $file_tmp_name=$_FILES['upimage']['tmp_name'];
  19. $fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png");
  20. if(!in_array($_FILES['upimage']['type'],$fileext)){
  21. $errors[]="你上传的文件格式不正确,仅支持 png, jpg, gif格式。";
  22. }
  23. }
  24. $tpgs=$_POST['tpgs'];//图片格式
  25. $qrcode_bas_path='upload/qrcode/';
  26. if(!is_dir($qrcode_bas_path)){
  27. mkdir($qrcode_bas_path, 0777, true);
  28. }
  29. $uniqid_rand=date("Ymdhis").uniqid(). rand(1,1000);
  30. $qrcode_path=$qrcode_bas_path.$uniqid_rand. "_1.".$tpgs;//原始图片路径
  31. $qrcode_path_new=$qrcode_bas_path.$uniqid_rand."_2.".$tpgs;//二维码图片路径
  32. if(Helper::getOS()=='Linux'){
  33. $mv = move_uploaded_file($file_tmp_name, $qrcode_path);
  34. }else{
  35. //解决windows下中文文件名乱码的问题
  36. $save_path = Helper::safeEncoding($qrcode_path,'GB2312');
  37. if(!$save_path){
  38. $errors[]='上传失败,请重试!';
  39. }
  40. $mv = move_uploaded_file($file_tmp_name, $qrcode_path);
  41. }
  42. if(empty($errors)){
  43. $errorCorrectionLevel = $_POST['errorCorrectionLevel'];//容错级别
  44. $matrixPointSize = $_POST['matrixPointSize'];//生成图片大小
  45. $matrixMarginSize = $_POST['matrixMarginSize'];//边距大小
  46. //生成二维码图片
  47. $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
  48. $QR = $qrcode_path_new;//已经生成的原始二维码图
  49. $logo = $qrcode_path;//准备好的logo图片
  50. if (file_exists($logo)) {
  51. $QR = imagecreatefromstring(file_get_contents($QR));
  52. $logo = imagecreatefromstring(file_get_contents($logo));
  53. $QR_width = imagesx($QR);//二维码图片宽度
  54. $QR_height = imagesy($QR);//二维码图片高度
  55. $logo_width = imagesx($logo);//logo图片宽度
  56. $logo_height = imagesy($logo);//logo图片高度
  57. $logo_qr_width = $QR_width / 5;
  58. $scale = $logo_width/$logo_qr_width;
  59. $logo_qr_height = $logo_height/$scale;
  60. $from_width = ($QR_width - $logo_qr_width) / 2;
  61. //重新组合图片并调整大小
  62. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
  63. $logo_qr_height, $logo_width, $logo_height);
  64. //输出图片
  65. //header("Content-type: image/png");
  66. imagepng($QR,$qrcode_path);
  67. imagedestroy($QR);
  68. }else{
  69. $qrcode_path=$qrcode_path_new;
  70. }
  71. }else{
  72. $qrcode_path='';
  73. }
  74. }
  75. $data=array('data'=>array('errors'=>$errors,'qrcode_path'=>$qrcode_path));
  76. $this->assign('data',$data);
  77. $this->display();

演示地址:http://www.shouce.ren/tool/qrcode

下载地址


还没有评论.