简体   繁体   English

强制下载PHP生成的QR码图片

[英]Force Download of PHP generated QR code Image

I am using Endroid\\QrCode to generate a QR Code with PHP. 我正在使用Endroid \\ QrCode与PHP生成QR码。 This all works fine but I'll post the code anyway. 一切正常,但是我还是会发布代码。 I am trying to add a download button to my page to make it more user friendly than having to right-click the image and choose Save As from the context menu. 我试图在页面上添加一个下载按钮,以使其更加用户友好,而不是必须右键单击图像并从上下文菜单中选择“ 另存为 ”。 But instead of downloading the image (.png) file it downloads the current page as a .htm file. 但与其下载图像(.png)文件,不如将其下载为.htm文件。 Is there a different way this has to be done with the image being PHP generated and not just a raw png image? 是否有其他方法可以用PHP生成的图像而不是原始的png图像来完成?

PHP: PHP:

<?php require_once '../control.php';
require_once '../vendor/autoload.php';

use Endroid\QrCode\QrCode;

$qrCode = new QrCode();


if($_GET['url'] != ''){
    $qrCode->setText($_GET['url']);
} else {
    $qrCode->setText(SITEHTMLROOT);
}
if($_GET['size'] != ''){
    $qrCode->setSize($_GET['size']);
} else {
    $qrCode->setSize(200);
}
if($_GET['padding'] != ''){
    $qrCode->setPadding($_GET['padding']);
} else {
    $qrCode->setPadding(10);
}
if($_GET['label'] != ''){
    $qrCode->setLabel($_GET['label']);
}
if($_GET['text_size'] != ''){
    $qrCode->setLabelFontSize($_GET['text_size']);
} else {
    $qrCode->setLabelFontSize(16);
}

$qrCode
    ->setErrorCorrection('high')
    ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
    ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
    ->render()
;
?>

Javascript: Javascript:

var getQRCode = function () {
  var url = $('#site_url').val();
  var size = $('#qr_size').val();
  var padding = $('#qr_padding').val();
  var label = $('#qr_label').val();
  var text_size = $('#qr_text_size').val();
  var QRcode = $('#final_QRCode');
  var get = '?url=' + url +'&&size=' + size + '&&padding=' + padding + '&&label=' + 
    label + '&&text_size=' + text_size;
  QRcode.attr('src', htmlroot + 'cgi/qrcode.php' + get);
  $('#qr_code_download').attr('src',  htmlroot + 'cgi/qrcode.php' + get);
};

HTML button and image: HTML按钮和图片:

<label>Your QR Code</label>
<div class="row"><img src="" id="final_QRCode" /></div>
<a href="#" id="qr_code_download" download="My QR Code">
  <div class="button special fit">Download This QR Code</div></a>

Image URL format: 图片网址格式:

http://www.example.com/qrcode.php?url=myurl.html&size=300&padding=10&label=&text_size=16

To ensure the correct URL is being fetched check the browser's developer tools network tab. 为确保获取正确的URL,请检查浏览器的开发人员工具的“网络”标签。 I would suggest the following modification to your javascript code should help - adding CDATA brackets means you can use any characters inside without breaking HTML validation, and URL's should have single ampersands to separate parameters not double ampersands. 我建议对您的javascript代码进行以下修改应该会有所帮助-添加CDATA括号意味着您可以在其中使用任何字符而不会破坏HTML验证,并且URL应该使用单“&”号分隔参数,而不是“&”号。

<script type="text/javascript"><![CDATA[
var getQRCode = function () {
  var url = $('#site_url').val();
  var size = $('#qr_size').val();
  var padding = $('#qr_padding').val();
  var label = $('#qr_label').val();
  var text_size = $('#qr_text_size').val();
  var QRcode = $('#final_QRCode');
  var get = '?url=' + url +'&size=' + size + '&padding=' + padding + '&label=' + label + 
    '&text_size=' + text_size;
  QRcode.attr('src', htmlroot + 'cgi/qrcode.php' + get);
  $('#qr_code_download').attr('href',  htmlroot + 'cgi/qrcode.php' + get);
};
]]></script>

In addition it may be worth adding some HTTP headers into your PHP script to ensure the browser understands the type of content it is receiving, just before the QR code rendering statements, and then deliberately omit the closing PHP tag as this can allow trailing whitespace to interfere with the output: 另外,可能值得在PHP脚本中添加一些HTTP标头,以确保浏览器在QR代码呈现语句之前了解浏览器正在接收的内容类型,然后故意省略关闭的PHP标记,因为这可以允许尾随空白干扰输出:

<?php
header( 'Content-type: image/png' );
$qrCode->setErrorCorrection('high')
  ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
  ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
  ->render();
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM