php生成logo二維碼
1. 為什麼我用phprqcode 生成二維碼帶logo的時候,就無法讀取信息
使用 jQuery.qrcode.js實現。
jQuery.qrcode.js 是一個能夠在客戶端生成矩陣二維碼QRCode 的jquery插件,使用它可以很方便的在頁面上生成二維條碼。此插件是能夠獨立使用的,體積也比較 小,使用gzip壓縮後才不到4kb。因為它是直接在客戶端生成的條碼, 所以不會有圖片下載的過程,能夠實現快速生成。它是基於一個多語言的類庫封裝的,也不依賴於其他額外的服務。
好處:使用jquery-qrcode的好處,不需要在伺服器端生成多餘的二維碼圖片,二維碼直接通過JavaScript直接在客戶端生成,有效減少帶寬,以及維護成本。
2. PHP可以生成傳參二維碼嗎,
php可以利用谷歌的api生成二維碼。
舉例如下:
include 'phpqrcode.php';
$value = 'http://www.helloweba.com'; //二維碼內容
$errorCorrectionLevel = 'L';//容錯級別
$matrixPointSize = 6;//生成圖片大小
//生成二維碼圖片
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo.png';//准備好的logo圖片
$QR = 'qrcode.png';//已經生成的原始二維碼圖
if ($logo !== FALSE) {
$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;
//重新組合圖片並調整大小
imageresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//輸出圖片
imagepng($QR, 'helloweba.png');
echo '<img src="helloweba.png">';
3. php生成二維碼的幾種方式整理及使用實例
本文整理了一些php生成二維碼的方式:1.google開放api;2.php類庫PHP QR Code;3.libqrencode;4.QRcode Perl CGI & PHP scripts感興趣的朋友可以參考下哈
1.google開放api
$urlToEncode="http://bbs.lewanchina.com";
generateQRfromGoogle($urlToEncode);
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
{
$url = urlencode($url);
echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$size.'" widhtHeight="'.$size.'"/>';
}
2.php類庫PHP QR Code
PHP QR Code is open source (LGPL) library for generating QR Code,
2-dimensional barcode. Based on libqrencode C library,
provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2).
Implemented purely in PHP, with no external dependencies (except GD2 if needed).
<?
include "./phpqrcode/phpqrcode.php";
$value="http://www.weste.net";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>
4. 為什麼用phpqrcode 生成的帶logo的二維碼不能掃不能跳轉
Thinkphp中沒有二維碼相關的庫,因此我們可以通過整合phpqrcode來完成生成二維碼的功能。
下載phpqrcode
下載地址:http://phpqrcode.sourceforge.net/
整合到Thinkphp框架
在「ThinkPHP\Library\Vendor\」下新建目錄phpqrcode,將壓縮包內容解壓到該文件夾下。
調用phpqrcode生成二維碼
在IndexController控制器下添加如下方法:
public function qrcode($url="www..com",$level=3,$size=4) { Vendor('phpqrcode.phpqrcode'); $errorCorrectionLevel =intval($level) ;//容錯級別 $matrixPointSize = intval($size);//生成圖片大小 //生成二維碼圖片 $object = new \QRcode(); $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2); }
訪問:http://127.0.0.1/Index/qrcode即可看到生成的二維碼。
生成帶logo的二維碼
先調用phpqrcode生成一張二維碼,再使用php的image相關函數將logo圖片添加到生成的二維碼圖片上。
include 'phpqrcode.php'; $value = 'http://www.cnblogs.com/txw1958/'; //二維碼內容 $errorCorrectionLevel = 'L';//容錯級別 $matrixPointSize = 6;//生成圖片大小 //生成二維碼圖片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'logo.png';//准備好的logo圖片 $QR = 'qrcode.png';//已經生成的原始二維碼圖 if ($logo !== FALSE) { $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; //重新組合圖片並調整大小 imageresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } //輸出圖片 imagepng($QR, 'helloweixin.png'); echo '<img src="helloweixin.png">';
5. php 如何形成二維碼
使用PHPQRCode類庫創建二維碼
使用舉例瀏覽器輸出:
<?
include"phpqrcode/phpqrcode.php";
$errorCorrectionLevel="L";
$matrixPointSize="4";
QRcode::png($value,false,$errorCorrectionLevel,$matrixPointSize);
exit;
?>
文件輸出二維碼
include('phpqrcode/phpqrcode.php');
//二維碼數據
//生成的文件名
$filename='1111.png';
//糾錯級別:L、M、Q、H
$errorCorrectionLevel='L';
//點的大小:1到10
$matrixPointSize=4;
QRcode::png($data,$filename,$errorCorrectionLevel,$matrixPointSize,2);
生成中間帶logo的二維碼
<?php
include('phpqrcode/phpqrcode.php');
$errorCorrectionLevel='L';
$matrixPointSize=6;
QRcode::png($value,'xiangyang.png',$errorCorrectionLevel,$matrixPointSize,2);
echo"QRcodegenerated"."<br/>";
$logo='logo.png';
$QR='xiangyang.png';
if($logo!==FALSE)
{
$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_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;
imageresampled($QR,$logo,$from_width,$from_width,0,0,$logo_qr_width,$logo_qr_height,$logo_width,$logo_height);
}
imagepng($QR,'xiangyanglog.png');
?>
自行下載phpqrcode.