php二维码生成以及下载实现

这篇文章主要介绍了php二维码生产以及下载实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

本文实例为大家分享了php二维码生成以及下载的具体代码,供大家参考,具体内容如下

  1. <?php
  2. //引入phpqrcode库文件
  3. define('IN_ECS', true);
  4. require(dirname(__FILE__) . '/includes/init.php');
  5. include('includes/phpqrcode.php');
  6. // 二维码数据
  7. $data = 'http://www.baidu.com';
  8. $filename = 'shopEwm/'.'baidu.png';
  9. //down_file('baidu.png',BASE_PATH);
  10. setShopEwm($data,$filename);
  11. //生成二维码图片
  12. function setShopEwm($data,$filename){
  13. // 纠错级别:L、M、Q、H
  14. $errorCorrectionLevel = 'L';
  15. // 点的大小:1到10
  16. $matrixPointSize = 4;
  17. //创建一个二维码文件
  18. QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  19. //输入二维码到浏览器
  20. //QRcode::png($data);
  21. }
  22. //下载二维码图片
  23. function down_file($file_name){
  24. $file_sub_dir = str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/shopEwm/";
  25. //原因 php文件函数,比较古老,需要对中文转码 gb2312
  26. $file_name=iconv("utf-8","gb2312",$file_name);
  27. //绝对路径
  28. $file_path=$file_sub_dir.$file_name;
  29. //1.打开文件
  30. if(!file_exists($file_path)){
  31. echo "文件不存在!";
  32. return ;
  33. }
  34. $fp=fopen($file_path,"r");
  35. //2.处理文件
  36. //获取下载文件的大小
  37. $file_size=filesize($file_path);
  38. /* if($file_size>30){
  39. echo "<script language='javascript'>window.alert('过大')</script>";
  40. return ;
  41. } */
  42. //返回的文件
  43. header("Content-type: application/octet-stream");
  44. //按照字节大小返回
  45. header("Accept-Ranges: bytes");
  46. //返回文件大小
  47. header("Accept-Length: $file_size");
  48. //这里客户端的弹出对话框,对应的文件名
  49. header("Content-Disposition: attachment; filename=".$file_name);
  50. //向客户端回送数据
  51. $buffer=1024;
  52. //为了下载的安全,我们最好做一个文件字节读取计数器
  53. $file_count=0;
  54. //这句话用于判断文件是否结束
  55. while(!feof($fp) && ($file_size-$file_count>0) ){
  56. $file_data=fread($fp,$buffer);
  57. //统计读了多少个字节
  58. $file_count+=$buffer;
  59. //把部分数据回送给浏览器;
  60. echo $file_data;
  61. }
  62. //关闭文件
  63. fclose($fp);
  64. }