从PHP生成批量QR码

时间:2015-01-23 09:42:59

标签: php qr-code

我正在使用PHP QR码库,它可以正常生成一个QR码。我需要的是改变我的PHP以显示从文本字段(如下所示)生成的大量QR码,这些QR码将以逗号分隔。这是我的脚本:

echo"

PHP二维码


";

//set it to writable location, a place for temp generated PNG files
$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

//html PNG location prefix
$PNG_WEB_DIR = 'temp/';

include "qrlib.php";    

//ofcourse we need rights to create temp dir
if (!file_exists($PNG_TEMP_DIR))
    mkdir($PNG_TEMP_DIR);


$filename = $PNG_TEMP_DIR.'test.png';


//processing form input
//remember to sanitize user input in real-life solution !!!
$errorCorrectionLevel = 'L';
if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
    $errorCorrectionLevel = $_REQUEST['level'];    

$matrixPointSize = 4;
if (isset($_REQUEST['size']))
    $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);


if (isset($_REQUEST['data'])) { 

    //it's very important!
    if (trim($_REQUEST['data']) == '')
        die('data cannot be empty! <a href="?">back</a>');

    // user data
    $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
    QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2); 


} else {    

    //default data
    echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';    
    QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);


}    

//display generated file
echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>'; 

echo $ filename;

//config form
echo '<form action="index.php" method="post">
    Data:&nbsp;<input name="data" value="'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code :)').'" />&nbsp;
    ECC:&nbsp;<select name="level">
        <option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option>
        <option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option>
        <option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option>
        <option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option>
    </select>&nbsp;
    Size:&nbsp;<select name="size">';

for($i=1;$i<=10;$i++)
    echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>';

echo '</select>&nbsp;
    <input type="submit" value="GENERATE"></form><hr/>';

我需要的是输入字段,名为&#39; value&#39;我将能够键入一系列由逗号分隔的信息,然后显示从我指定的逗号分隔列表生成的QR条形码列表。

由于

1 个答案:

答案 0 :(得分:1)

在循环之前声明一个空数组:$ array = array();在每个循环期间添加它{$ array [] = $ addedpng;如果您在循环查询结果集中获取png名称 如果你不能将它作为数组处理,并且需要csv字符串,我只需要做类似的事情: $x = 'DONTNEEDTHISTEXT'; foreach($png as $addedpng){$x .= ', '.$addedpng; }
$onlypngs = str_replace("DONTNEEDTHISTEXT, ", "", $x);