用Thinkphp3.2結合phpqrcode產生二維碼

來源:互聯網
上載者:User
首先,下載phpqrcode,將其解壓到項目ThinkPHP\Library\Vendor目錄下。
Index_index.html(模板可自行配置)


    尺寸大小:                邊距大小:                     1px            2px            3px            5px            容錯層級:                Low (7%)        Medium (15%)        Quartile (25%)        High (30%)            二維碼內容:                二維碼logo:

控制器:IndexController.class.php


<?phpnamespace Home\Controller;    use Think\Controller;    class IndexController extends Controller {    public function index() {    $config = array( 'maxSize' => 3145728, 'rootPath' => './upload/', 'savePath' => '', 'saveName' => array('uniqid',''), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date','Ymd'), );    $upload = new \Think\Upload($config);    // 執行個體化上傳類 // 上傳單個檔案 $info = $upload->uploadOne($_FILES['test']);    if(!$info) {    // 上傳錯誤提示錯誤資訊 $qrcode_path_new = './Public/Home/images/code'.'_'.date("Ymdhis").'.png';    $content = $_POST['content'];    $matrixPointSize = $_POST['matrixPointSize'];    $matrixMarginSize = $_POST['matrixMarginSize'];    $errorCorrectionLevel = $_POST['errorCorrectionLevel'];    makecode_no_pic($content,$qrcode_path_new,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel);    $this->assign('img',$qrcode_path_new);}else {    $qrcode_path = "./upload/".$info['savepath'].$info['savename'];    $content = $_POST['content'];    $matrixPointSize = $_POST['matrixPointSize'];    $matrixMarginSize = $_POST['matrixMarginSize'];    $errorCorrectionLevel = $_POST['errorCorrectionLevel'];    $url = "./upload/qrcode/".date("YmdHis").".png";    makecode($qrcode_path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url);    $this->assign('img',$url);}$this->display();}}

然後在項目Application/Home/Common下建立function.php


<?phpfunction makecode($qrcode_path,$content,$matrixPointSize,$matrixMarginSize,$errorCorrectionLevel,$url) {    /** 參數詳情: * $qrcode_path:logo地址 * $content:需要產生二維碼的內容 * $matrixPointSize:二維碼尺寸大小 * $matrixMarginSize:產生二維碼的邊距 * $errorCorrectionLevel:容錯層級 * $url:產生的帶logo的二維碼地址 **/    ob_clean ();    Vendor('phpqrcode.phpqrcode');    $object = new \QRcode();    $qrcode_path_new = './Public/Home/images/code'.'_'.date("Ymdhis").'.png';    //定義產生二維碼的路徑及名稱 $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('phpqrcode.phpqrcode');    $object = new \QRcode();    $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);}

在沒有圖片上傳的情況下,產生我二維碼只是單純的二維碼,只有在有logo上傳的時候,才會產生帶logo的二維碼。

如,即沒有logo時產生的二維碼:

即在有圖片被上傳的時候產生的二維碼:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.