ThinkPHP利用phpqrcode生成二维码

亲测 简单的生成方法

  //生成带logo二维码
    public function qrcode($text='',$size='4',$level='L',$padding=2,$logo=true,$raoma){

        Vendor('phpqrcode.phpqrcode');
        $value = $text;//二维码数据
        $errorCorrectionLevel = 'L';//纠错级别:L、M、Q、H
        $matrixPointSize = 4;//二维码点的大小:1到10,手机端一般是4
        $QR_name = 'D:/WWW/qiye_tui/Public/success222.png';//最后生成的图片地址及定义的名字
        \QRcode::png ( $value, $QR_name, $errorCorrectionLevel, $matrixPointSize, 1 );
        //以上是不带Logo二维码的文件名,下面是为生成的二维码添加logo
        $logo = 'D:/WWW/qiye_tui/Public/logo.png';//需要显示在二维码中的Logo图像
        $logo = false; //false就是不带logo,带的话写地址
        if ($logo !== FALSE) {
            // 从字符串中的图像流新建一图像
            $QR = imagecreatefromstring(file_get_contents($QR_name));
            $logo = imagecreatefromstring(file_get_contents($logo));
            // 获取图像的宽度和高度
            $QR_width = imagesx($QR);
            $QR_height = imagesy($QR);
            $logo_width = imagesx($logo);
            $logo_height = imagesy($logo);
            $logo_qr_width = $QR_width / 5;
            // 输出合适宽高的图片
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        }
        imagepng ( $QR, $QR_name );//带Logo二维码的文件名
}

 

 

http://www.thinkphp.cn/extend/713.html

downurl:  http://www.thinkphp.cn/download/713.html

 

先安照路径放好如图。

简单使用无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

用到助手类Helper地址:http://www.thinkphp.cn/topic/34875.html

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值