CakePHP 2.x 二维码生成和下载

CakePHP 2.x QR Code generation and download

Objective:

生成一组字符串的二维码图片并下载。

到目前为止我尝试过的:

我使用了来自以下 link 的 QrCodeHelper:QR Code Helper

到目前为止,我只是从随机字符串开发一个简单的测试 QR 码。

在视图 view.ctp 中,我写了:

  echo $this->QrCode->text("ABCD"); // This generates a QR code image

在控制器中,我写了:

  public function view() {
      $this->response->type('Content-Type: image/png');
      $this->response->download('qrcode.png');
  }

加载页面时,正在下载图像 qrcode.png,但显示错误:"Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer"

照片查看器没有问题。

我知道之前有人问过类似的问题:CakePHP: download generated QR Code

但是并没有解决我的问题

如有任何帮助,我们将不胜感激。

提前致谢!

您使用过的二维码助手可能不太适合您的需要。 Helpers 旨在帮助查看 - 它正在正确地做,但在这种情况下你 want/need 在控制器中使用 helper,这实际上不起作用(而且,让事情变得更复杂的是这个特殊的 Helper returns 图像标签中的二维码)。

您可以通过使用 PHP QR Code

相对轻松地实现此目的

下载 PHPQRCode 并将文件放入 Cake 安装的 /vendors/phpqrcode/ 中,并在控制器中包含以下内容(注意:未经测试)

public function view($text = 'ABCD') {

    // Don't render a view file
    $this->autoRender = false; 

    // Tell the borwser to download the file (slug it, too)
    $this->response->download(Inflector::slug($text) . '.png');

    // Import the vendor lib
    App::import("Vendor", "phpqrcode/qrlib");

    // Bombs away
    QRcode::png($text);

}

有关 PHP 二维码的更多信息:http://phpqrcode.sourceforge.net/

你可以在你的图书馆下载二维码App/lib

使用的 class 的名称必须与文件

的名称相同

并像这样包含您的文件:

?> 不是你可以简单地使用你的二维码。