PHP驱动的QR码生成器

时间:2012-09-28 00:12:12

标签: php

此QR代码生成器仅允许您为一个内容字段生成。我想添加一种方法来生成您可以使用php生成的所有信息:http://zxing.appspot.com/generator/

我希望用户能够编码MECARD和vCard

这是我到目前为止所做的:

<div id="generator">
    <form target="qrcode-frame" action="gen.php" method="post">
      <fieldset>
        <legend>Size:</legend>
         <input type="radio" name="size" value="150x150" checked>150x150<br>
         <input type="radio" name="size" value="200x200">200x200<br>
         <input type="radio" name="size" value="250x250">250x250<br>
         <input type="radio" name="size" value="300x300">300x300<br>
      </fieldset>
      <fieldset>
        <legend>Encoding:</legend>
        <input type="radio" name="encoding" value="UTF-8" checked>UTF-8<br>
        <input type="radio" name="encoding" value="Shift_JIS">Shift_JIS<br>
        <input type="radio" name="encoding" value="ISO-8859-1">ISO-8859-1<br>
      </fieldset>
      <fieldset>
        <legend>Content:</legend>
        <textarea name="content"></textarea>
      </fieldset>         
      <fieldset>
        <legend>Error correction:</legend>
        <select name="correction">
            <option value="L" selected>L</option>
            <option value="M">M</option>
            <option value="Q">Q</option>
            <option value="H">H</option>
        </select>
      </fieldset>         
      <input type="submit" value="Generate"></input>
    </form>
</div>  
<div id="result">
    <iframe name="qrcode-frame" frameborder="0"  id="qrcode" src="gen.php" height="315px;" width="350px"></iframe>
</div>

并在另一个文件中:

    <?php
if(isset($_REQUEST['content'])){
    //capture from the form
    $size          = $_REQUEST['size'];
    $content       = $_REQUEST['content'];
    $correction    = strtoupper($_REQUEST['correction']);
    $encoding      = $_REQUEST['encoding'];

    //form google chart api link
    $rootUrl = "https://chart.googleapis.com/chart?cht=qr&chs=$size&chl=$content&choe=$encoding&chld=$correction";

    //print out the image
    echo '<img src="'.$rootUrl.'">';
}
?>

这很有效,但非常有限。

由于

3 个答案:

答案 0 :(得分:1)

我们有重要的参数

cht=qr                                  Required -> Specifies a QR code
chs=<width>x<height>                    Required -> Image size
chl=<data>                              Required -> more than 2K bytes (minus the other URL characters), you will have to send your data using POST
choe=<output_encoding>                  Optional -> UTF-8 , Shift_JIS , ISO-8859-1
chld=<error_correction_level>|<margin>  Optional ->

error_correction_level
L - [Default] Allows recovery of up to 7% data loss
M - Allows recovery of up to 15% data loss
Q - Allows recovery of up to 25% data loss
H - Allows recovery of up to 30% data loss

margin - The width of the white border around the data portion of the code. 

more details here

所以我们有

1-url

$data = 'http://chart.apis.google.com/chart?';  // url

2-参数

$data .= 'cht=qr&chs=' . $Barcode_size . '&chld=' . $Error_correction . '&choe=' .$Character_encoding . '&chl=' . $Encoding . ':'; // parameters

3-我们的数据

$data .= 'name:' . $Name . ';Company:' . $Company . ';Title:' . $Title . ';Phone_number:' . $Phone_number . ';Email:' . $Email . ';Address:' . $Address . ';Website:' . $Website . ';Memo:' . $Memo . ';;'; 

结果将是那样的

   http://chart.apis.google.com/chart?cht=qr&chs=120x120&chld=L&choe=UTF-8&chl=MECARD:name:MyName;Company:MyCo;Title:ttt;Phone_number:ttt;Email:ttt;Address:ttt;Website:tt;Memo:ttt;;

注意数据:

名称MyName - &gt;写得像那样 - &gt;名:MYNAME;

数据名称:数据值; //你只能发送没有名字的值

在行的末尾你会找到两个分号

答案 1 :(得分:0)

您可以在https://developers.google.com/chart/infographics/docs/qr_codes#details

找到详细信息

以及此处的示例http://www.cognation.net/QR/Google_api.htm

如果你需要html代码和php代码,请用你需要创建的数据写一个评论

我会为你创建一个例子。

答案 2 :(得分:0)

这个例子对我很好 你必须处理错误(空文本框)

示例:

必须包含jquery.js html:

<html>

<head>

<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<script src="jquery.js" type="text/javascript">    </script>

<script language="JavaScript" type="text/javascript" >

function get_url()
{

$.ajax(
{
type: 'POST',
url: 'gen.php',
dataType: "html",
data: {Name: $('#Name').val(), Company: $('#Company').val(),Title: $('#Title').val(), Phone_number: $('#Phone_number').val(),Email: $('#Email').val(), Address: $('#Address').val(),Address_2 : $('#Address_2').val(), Website: $('#Website').val(),Memo: $('#Memo').val(), Encoding: $('#Encoding option:selected').val(), Barcode_size: $('#Barcode_size  option:selected').val(), Error_correction: $('#Error_correction option:selected').val(), Character_encoding: $('#Character_encoding option:selected').val()},
success: function(msg) {
$('#myurl').val(msg);
$('#img').attr('src',msg);

} });

}



</script>

<title>
</title>


</head>


<body>




<form action="javascript:get_url();">
<table cellspacing="0" cellpadding="0" id="mainpanel">    <tbody>
<tr>
<td align="left" style="vertical-align: top;">
<table id="leftpanel">    <colgroup>    <col>    </colgroup>    <tbody>
<tr>
<td>&nbsp;    </td>
</tr>
<tr>
<td>
<table>    <colgroup>    <col class="firstColumn">    <col class="secondColumn">    </colgroup>    <tbody>
<tr>
<td class="firstColumn">Name
</td>
<td class="secondColumn">
<input type="text" id="Name">
</td>
</tr>
<tr>
<td class="firstColumn">Company
</td>
<td class="secondColumn">
<input type="text" id="Company">
</td>
</tr>
<tr>
<td class="firstColumn">Title
</td>
<td class="secondColumn">
<input type="text" id="Title">
</td>
</tr>
<tr>
<td class="firstColumn">Phone number
</td>
<td class="secondColumn">
<input type="text" id="Phone_number">
</td>
</tr>
<tr>
<td class="firstColumn">Email
</td>
<td class="secondColumn">
<input type="text" id="Email">
</td>
</tr>
<tr>
<td class="firstColumn">Address
</td>
<td class="secondColumn">
<input type="text" id="Address">
</td>
</tr>
<tr>
<td class="firstColumn">Address 2
</td>
<td class="secondColumn">
<input type="text" id="Address_2">
</td>
</tr>
<tr>
<td class="firstColumn">Website
</td>
<td class="secondColumn">
<input type="text" id="Website">
</td>
</tr>
<tr>
<td class="firstColumn">Memo
</td>
<td class="secondColumn">
<input type="text" id="Memo">
</td>
</tr>
<tr>
<td class="firstColumn">Encoding
</td>
<td class="secondColumn">
<select id="Encoding">

<option selected value="MECARD">MECARD    </option>

<option value="vCard">vCard    </option>
</select>
</td>
</tr>    </tbody>
</table>
</td>
</tr>
<tr>
<td>

<span id="errorMessageID">&nbsp;
</span>
</td>
</tr>
<tr>
<td>
<table>    <colgroup>    <col class="firstColumn">    <col class="secondColumn">    </colgroup>    <tbody>
<tr>
<td class="firstColumn">Barcode size
</td>
<td class="secondColumn">
<select id="Barcode_size">

<option selected value="120">Small    </option>

<option value="230">Medium    </option>

<option value="350">Large    </option>
</select>
</td>
</tr>
<tr>
<td class="firstColumn">Error correction
</td>
<td class="secondColumn">
<select id="Error_correction">

<option selected value="L">L    </option>

<option value="M">M    </option>

<option value="Q">Q    </option>

<option value="H">H    </option>
</select>
</td>
</tr>
<tr>
<td class="firstColumn">Character encoding
</td>
<td class="secondColumn">
<select id="Character_encoding">

<option selected value="UTF-8">UTF-8    </option>

<option value="ISO-8859-1">ISO-8859-1    </option>

<option value="Shift_JIS">Shift_JIS    </option>
</select>
</td>
</tr>    </tbody>
</table>
</td>
</tr>
<tr>
<td>
<table>    <colgroup>    <col class="firstColumn">    <col class="secondColumn">    </colgroup>    <tbody>
<tr>
<td class="firstColumn">&nbsp;
</td>
<td class="secondColumn">    <button type="submit" >Generate →    </button>
</td>
</tr>    </tbody>
</table>
</td>
</tr>    </tbody>
</table>
</td>
<td align="left" style="vertical-align: top;">
<table cellspacing="0" cellpadding="0">    <tbody>
<tr>
<td align="left" style="vertical-align: top;">

<div id="imageresult">

<div id="innerresult">    <img id="img" src="" >
</div>
</div>
</td>
</tr>
<tr>
<td align="left" style="vertical-align: top;">

<div class="gwt-HTML" id="downloadText" style="">
    embed with this URL:
</div>
</td>
</tr>
<tr>
<td align="left" style="vertical-align: top;">
<input name="myurl" id="myurl" type="text" class="gwt-TextBox" id="urlresult" style="">
</td>
</tr>
<tr>
<td align="left" style="vertical-align: top;">&nbsp;    </td>
</tr>    </tbody>
</table>
</td>
</tr>    </tbody>
</table>
</form>


</body>
</html>

php:

<?php
$post = (!empty ($_POST)) ? true : false;
if (!$post) {
    die();
}
$data = '';
@ $Name = $_POST['Name'];
@ $Company = $_POST['Company'];
@ $Title = $_POST['Title'];
@ $Phone_number = $_POST['Phone_number'];
@ $Email = $_POST['Email'];
@ $Address = $_POST['Address'];
@ $Address_2 = $_POST['Address_2'];
@ $Website = $_POST['Website'];
@ $Memo = $_POST['Memo'];
@ $Encoding = $_POST['Encoding'];
@ $Barcode_size = $_POST['Barcode_size'];
@ $Error_correction = $_POST['Error_correction'];
@ $Character_encoding = $_POST['Character_encoding'];
$Barcode_size = $Barcode_size . "x" . $Barcode_size;
$data = 'http://chart.apis.google.com/chart?';  // url
$data .= 'cht=qr&chs=' . $Barcode_size . '&chld=' . $Error_correction . '&choe=' .$Character_encoding . '&chl=' . $Encoding . ':'; // required data
$data .= 'name:' . $Name . ';Company:' . $Company . ';Title:' . $Title . ';Phone_number:' . $Phone_number . ';Email:' . $Email . ';Address:' . $Address . ';Website:' . $Website . ';Memo:' . $Memo . ';;'; // your data   -> name :value ; and at the end of the line -> ;
echo $data;
?>