Rumah > rangka kerja php > INI > teks badan

利用yii2生成二维码的具体步骤

王林
Lepaskan: 2021-03-15 10:22:29
ke hadapan
2936 人浏览过

利用yii2生成二维码的具体步骤

生成二维码的具体步骤如下所示:

1、在官网下载类库

在官网下载类库后,确保当前PHP环境支持GD2,然后我们只需要使用phpqrcode.php就可以生成二维码了。

phpqrcode.php提供了一个关键的png()方法,其中

  • 参数$text表示生成二位的的信息文本;

  • 参数$outfile表示是否输出二维码图片 文件,默认否;

  • 参数$level表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%); 

  • 参数$size表示生成图片大小,默认是3;参数$margin表示二维码周围边框空白区域间距值;

  • 参数$saveandprint表示是否保存二维码并显示。

2、下载后把解压后的phpqrcode文件夹放到extensions文件夹下,如下图:

7c612339715fce0d769defa9a040283.png

3、引入类

Yii::$enableIncludePath = false;
Yii::import ('application.extensions.phpqrcode.phpqrcode', 1 );
Salin selepas log masuk

4、具体代码

public function actionQrcode(){
        $this->breadcrumbs=array_merge($this->breadcrumbs,array(
                '生成二维码'
        ));
        $qrcode_path='';
        $file_tmp_name='';
        $errors=array();
        if(!empty($_POST)){
            $content = trim($_POST['content']); //二维码内容
            $contentSize=$this->getStringLength($content);
            if($contentSize>290){
                $errors[]='字数过长,不能多于150个字符!';
            }
            Yii::$enableIncludePath = false;
            Yii::import ('application.extensions.phpqrcode.phpqrcode', 1 );
            if(isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])){
                if($_FILES['upimage']['size']>512000){
                    $errors[]="你上传的文件过大,最大不能超过500K。";
                }
                $file_tmp_name=$_FILES['upimage']['tmp_name'];
                $fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png");
                if(!in_array($_FILES['upimage']['type'],$fileext)){
                    $errors[]="你上传的文件格式不正确,仅支持 png, jpg, gif格式。";
                }
            }
            $tpgs=$_POST['tpgs'];//图片格式
            $bas_path=dirname ( Yii::app ()->BasePath );
            $qrcode_bas_path=$bas_path.'/upload/qrcode/';
            if(!is_dir($qrcode_bas_path)){
                mkdir($qrcode_bas_path, 0777, true);
            }
            $uniqid_rand=date("Ymdhis").uniqid(). rand(1,1000);
            $qrcode_path=$qrcode_bas_path.$uniqid_rand. "_1.".$tpgs;
            $qrcode_path_new=$qrcode_bas_path.$uniqid_rand."_2.".$tpgs;
            if(Helper::getOS()=='Linux'){

                $mv = move_uploaded_file($file_tmp_name, $qrcode_path);
            }else{
                //解决windows下中文文件名乱码的问题
                $save_path = Helper::safeEncoding($qrcode_path,'GB2312');
                if(!$save_path){
                    $errors[]='上传失败,请重试!';
                }
                $mv = move_uploaded_file($file_tmp_name, $qrcode_path);
            }
            if(empty($errors)){
                $errorCorrectionLevel = $_POST['errorCorrectionLevel'];//容错级别
                $matrixPointSize = $_POST['matrixPointSize'];//生成图片大小
                $matrixMarginSize = $_POST['matrixMarginSize'];//边距大小
                //生成二维码图片
                QRcode::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
                $QR = $qrcode_path_new;//已经生成的原始二维码图
                $logo = $qrcode_path;//准备好的logo图片
                if (file_exists($logo)) {
                    $QR = imagecreatefromstring(file_get_contents($QR));
                    $logo = imagecreatefromstring(file_get_contents($logo));
                    $QR_width = imagesx($QR);//二维码图片宽度
                    $QR_height = imagesy($QR);//二维码图片高度
                    $logo_width = imagesx($logo);//logo图片宽度
                    $logo_height = imagesy($logo);//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);
                    //输出图片
//                     header("Content-type: image/png");
                    imagepng($QR,$qrcode_path);
                    imagedestroy($QR);
                }else{
                    $qrcode_path=$qrcode_path_new;
                }
                $qrcode_path=str_replace($bas_path,'', $qrcode_path);
            }else{
                $qrcode_path='';
            }
        }
        $data=array('data'=>array('errors'=>$errors,'qrcode_path'=>$qrcode_path));
        $this->render ( 'qrcode',$data);
    }
Salin selepas log masuk

免费视频教程分享:php视频教程

4、前台的上传界面

request->hostInfo;
$matrixPointSize=6;
$matrixMarginSize=2;
$errorCorrectionLevel='M';
$tpgs='gif';
if(!empty($_POST)){
    $content=$_POST['content'];
    $matrixPointSize=$_POST['matrixPointSize'];
    $matrixMarginSize=$_POST['matrixMarginSize'];
    $errorCorrectionLevel=$_POST['errorCorrectionLevel'];
    $tpgs=$_POST['tpgs'];
}
$arrayCorrectionLevel=array('L'=>'L - Low (7%)','M'=>'M - Medium (15%)','Q'=>'Q - Quartile (25%)','H'=>'H - High (30%)');
$arrayTpgs=array('gif'=>'gif格式','png'=>'png格式','jpg格式');
?>
在线生成二维码
beginWidget ( 'CActiveForm', array ( 'id' => 'qrcode-form', 'htmlOptions' => array ( 'id' => 'view_table', 'class' => 'add-form padding-10', 'enctype' => 'multipart/form-data' ), 'enableAjaxValidation' => false ) ); ?>
'form-control'));?>
'form-control'));?>
'form-control','maxlength'=>150));?>
endWidget(); ?>
二维码
renderPartial('/component/duoshuo_common');?>
Salin selepas log masuk

最终效果如下:

3f0b9c2d73f1804985a366e3176e1d1.png

相关推荐:yii框架

以上是利用yii2生成二维码的具体步骤的详细内容。更多信息请关注PHP中文网其他相关文章!

Label berkaitan:
sumber:cnblogs.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!