如何从MySQL数据创建QR码?

时间:2014-10-02 12:50:34

标签: php mysql joomla qr-code

我需要一个脚本,使用存储在MySQL数据库中的用户数据创建QR码。每次用户访问他的数据时都应该加载它。我做了一些研究,并找到了一个可以满足我需求的图书馆:http://phpqrcode.sourceforge.net/。我已经抓住了一个例子来在我的网站(http://phpqrcode.sourceforge.net/examples/index.php?example=025)中测试它并调整代码:

<?php
include('../libraries/phpqrcode/qrlib.php'); 
include('configuration.php'); 

// how to build raw content - QRCode with simple Business Card (VCard) 

$tempDir = EXAMPLE_TMP_SERVERPATH; 

// here our data 
$name = 'John Doe'; 
$phone = '(049)012-345-678'; 

// we building raw data 
$codeContents = 'BEGIN:VCARD'."\n"; 
$codeContents .= 'FN:'.$name."\n"; 
$codeContents .= 'TEL;WORK;VOICE:'.$phone."\n"; 
$codeContents .= 'END:VCARD'; 

// generating 
QRcode::png($codeContents, $tempDir.'025.png', QR_ECLEVEL_L, 3); 

// displaying 
echo '<img src="'.EXAMPLE_TMP_URLRELPATH.'025.png" />'; 
?>

然而,它给了我这个错误:

  

警告:include(../ libraries / phpqrcode / qrlib.php):无法打开   stream:没有这样的文件或目录   /home/u909072349/public_html/plugins/system/sourcerer/helper.php(632)   :第7行的运行时创建函数警告:   include(../ libraries / phpqrcode / qrlib.php):无法打开流:否   这样的文件或目录   /home/u909072349/public_html/plugins/system/sourcerer/helper.php(632)   :第7行的运行时创建函数警告:include():失败   打开'../libraries/phpqrcode/qrlib.php'列入   (include_path ='。:/ usr / lib / php')in   /home/u909072349/public_html/plugins/system/sourcerer/helper.php(632)   :第7行的运行时创建函数致命错误:无法重新声明   在/home/u909072349/public_html/configuration.php上的JConfig类   第2行   我检查了qrlib.php文件,这是内容:

<?php
/*
 * PHP QR Code encoder
 *
 * Root library file, prepares environment and includes dependencies
 *
 * Based on libqrencode C library distributed under LGPL 2.1
 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
 *
 * PHP QR Code is distributed under LGPL 3
 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

    $QR_BASEDIR = dirname(__FILE__).DIRECTORY_SEPARATOR;

    // Required libs

    include $QR_BASEDIR."qrconst.php";
    include $QR_BASEDIR."qrconfig.php";
    include $QR_BASEDIR."qrtools.php";
    include $QR_BASEDIR."qrspec.php";
    include $QR_BASEDIR."qrimage.php";
    include $QR_BASEDIR."qrinput.php";
    include $QR_BASEDIR."qrbitstream.php";
    include $QR_BASEDIR."qrsplit.php";
    include $QR_BASEDIR."qrrscode.php";
    include $QR_BASEDIR."qrmask.php";
    include $QR_BASEDIR."qrencode.php";

我检查了这些库,它们都在同一个/ public_html / libraries / phpqrcode文件夹中。另外,configuration.php文件位于/ public_html /中。那么,有人能指出我的问题吗?一旦解决了,我就会继续前进。 谢谢!

达尼

1 个答案:

答案 0 :(得分:1)

在包含文件时,您尚未定义基本路径。

尝试使用以下内容:

include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php'); 
include('configuration.php'); 

我不确定为什么需要包含configuration.php文件,但这不是一个好主意。如果您需要从此文件中获取任何值,Joomla拥有自己的API来实现此目的。

更新

您尚未正确定义路径。使用以下内容:

<?php

   include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php');

   $tempDir = JPATH_SITE . '/images/';   
   $codeContents = 'This Goes From File';
   $fileName     = 'qr_'.md5($codeContents).'.png';

   $pngAbsoluteFilePath = $tempDir.$fileName;
   $urlRelativeFilePath = JUri::root() .'images/' . $fileName;

   if (!file_exists($pngAbsoluteFilePath)) {
      QRcode::png($codeContents, $pngAbsoluteFilePath);
   }
   else {
      echo "Not working!";
   }

   echo '<img src="'.$urlRelativeFilePath.'" />';

?>

希望这有帮助