Utilisez Thinkphp3.2 combiné avec phpqrcode pour générer un code QR

一个新手
Libérer: 2023-03-16 12:24:02
original
2481 Les gens l'ont consulté

Tout d'abord, téléchargez phpqrcode et extrayez-le dans le répertoire ThinkPHPLibraryVendor du projet.
Index_index.html (le modèle peut être configuré par vous-même)


    尺寸大小:
            
    边距大小:
         
            1px
            2px
            3px
            5px
        
    容错级别:
        
        Low (7%)
        Medium (15%)
        Quartile (25%)
        High (30%)
        
    二维码内容:
            
    二维码logo:
Copier après la connexion

Contrôleur : IndexController.class .php


<?php
namespace Home\Controller;
    use Think\Controller;
    class IndexController extends Controller {
    public function index() {
    $config = array( &#39;maxSize&#39; => 3145728, &#39;rootPath&#39; => &#39;./upload/&#39;, &#39;savePath&#39; => &#39;&#39;, &#39;saveName&#39; => array(&#39;uniqid&#39;,&#39;&#39;), &#39;exts&#39; => array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;), &#39;autoSub&#39; => true, &#39;subName&#39; => array(&#39;date&#39;,&#39;Ymd&#39;), );
    $upload = new \Think\Upload($config);
    // 实例化上传类 // 上传单个文件 $info = $upload->uploadOne($_FILES[&#39;test&#39;]);
    if(!$info) {
    // 上传错误提示错误信息 $qrcode_path_new = &#39;./Public/Home/images/code&#39;.&#39;_&#39;.date("Ymdhis").&#39;.png&#39;;
    $content = $_POST[&#39;content&#39;];
    $matrixPointSize = $_POST[&#39;matrixPointSize&#39;];
    $matrixMarginSize = $_POST[&#39;matrixMarginSize&#39;];
    $errorCorrectionLevel = $_POST[&#39;errorCorrectionLevel&#39;];
    makecode_no_pic($content,$qrcode_path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel);
    $this->assign(&#39;img&#39;,$qrcode_path_new);
}
else {
    $qrcode_path = "./upload/".$info[&#39;savepath&#39;].$info[&#39;savename&#39;];
    $content = $_POST[&#39;content&#39;];
    $matrixPointSize = $_POST[&#39;matrixPointSize&#39;];
    $matrixMarginSize = $_POST[&#39;matrixMarginSize&#39;];
    $errorCorrectionLevel = $_POST[&#39;errorCorrectionLevel&#39;];
    $url = "./upload/qrcode/".date("YmdHis").".png";
    makecode($qrcode_path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url);
    $this->assign(&#39;img&#39;,$url);
}
$this->display();
}
}
Copier après la connexion

Créez ensuite un nouveau function.php sous le projet Application/Home/Common


<?phpfunction makecode($qrcode_path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url) {
    /** 参数详情: * $qrcode_path:logo地址 * $content:需要生成二维码的内容 * $matrixPointSize:二维码尺寸大小 * $matrixMarginSize:生成二维码的边距 * $errorCorrectionLevel:容错级别 * $url:生成的带logo的二维码地址 **/
    ob_clean ();
    Vendor(&#39;phpqrcode.phpqrcode&#39;);
    $object = new \QRcode();
    $qrcode_path_new = &#39;./Public/Home/images/code&#39;.&#39;_&#39;.date("Ymdhis").&#39;.png&#39;;
    //定义生成二维码的路径及名称 $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
    $QR = imagecreatefromstring(file_get_contents($qrcode_path_new));
    //imagecreatefromstring:创建一个图像资源从字符串中的图像流 $logo = imagecreatefromstring(file_get_contents($qrcode_path));
    $QR_width = imagesx($QR);
    // 获取图像宽度函数 $QR_height = imagesy($QR);
    //获取图像高度函数 $logo_width = imagesx($logo);
    // 获取图像宽度函数 $logo_height = imagesy($logo);
    //获取图像高度函数 $logo_qr_width = $QR_width / 4;
    //logo的宽度 $scale = $logo_width / $logo_qr_width;
    //计算比例 $logo_qr_height = $logo_height / $scale;
    //计算logo高度 $from_width = ($QR_width - $logo_qr_width) / 2;
    //规定logo的坐标位置 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    /** imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h ) * 参数详情: * $dst_image:目标图象连接资源。 * $src_image:源图象连接资源。 * $dst_x:目标 X 坐标点。 * $dst_y:目标 Y 坐标点。 * $src_x:源的 X 坐标点。 * $src_y:源的 Y 坐标点。 * $dst_w:目标宽度。 * $dst_h:目标高度。 * $src_w:源图象的宽度。 * $src_h:源图象的高度。 **/
    Header("Content-type: image/png");
    //$url:定义生成带logo的二维码的地址及名称 imagepng($QR,$url);
}
function makecode_no_pic($content,$qrcode_path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel) {
    ob_clean ();
    Vendor(&#39;phpqrcode.phpqrcode&#39;);
    $object = new \QRcode();
    $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
}
Copier après la connexion

Lorsqu'aucune image n'est téléchargée, le code QR généré n'est qu'un simple code QR. Ce n'est que lorsqu'un logo est téléchargé que le code QR avec le logo sera généré.

Comme indiqué ci-dessous, le code QR généré lorsqu'il n'y a pas de logo :

L'image suivante est le suivant Le code QR généré lorsqu'une photo est téléchargée :

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!