QRencode.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Main encoder classes.
  6. *
  7. * Based on libqrencode C library distributed under LGPL 2.1
  8. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  9. *
  10. * PHP QR Code is distributed under LGPL 3
  11. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 3 of the License, or any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. namespace PHPQRCode;
  28. use Exception;
  29. class QRencode {
  30. public $casesensitive = true;
  31. public $eightbit = false;
  32. public $version = 0;
  33. public $size = 3;
  34. public $margin = 4;
  35. public $structured = 0; // not supported yet
  36. public $level = Constants::QR_ECLEVEL_L;
  37. public $hint = Constants::QR_MODE_8;
  38. //----------------------------------------------------------------------
  39. public static function factory($level = Constants::QR_ECLEVEL_L, $size = 3, $margin = 4)
  40. {
  41. $enc = new QRencode();
  42. $enc->size = $size;
  43. $enc->margin = $margin;
  44. switch ($level.'') {
  45. case '0':
  46. case '1':
  47. case '2':
  48. case '3':
  49. $enc->level = $level;
  50. break;
  51. case 'l':
  52. case 'L':
  53. $enc->level = Constants::QR_ECLEVEL_L;
  54. break;
  55. case 'm':
  56. case 'M':
  57. $enc->level = Constants::QR_ECLEVEL_M;
  58. break;
  59. case 'q':
  60. case 'Q':
  61. $enc->level = Constants::QR_ECLEVEL_Q;
  62. break;
  63. case 'h':
  64. case 'H':
  65. $enc->level = Constants::QR_ECLEVEL_H;
  66. break;
  67. }
  68. return $enc;
  69. }
  70. //----------------------------------------------------------------------
  71. public function encodeRAW($intext, $outfile = false)
  72. {
  73. $code = new QRcode();
  74. if($this->eightbit) {
  75. $code->encodeString8bit($intext, $this->version, $this->level);
  76. } else {
  77. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  78. }
  79. return $code->data;
  80. }
  81. //----------------------------------------------------------------------
  82. public function encode($intext, $outfile = false)
  83. {
  84. $code = new QRcode();
  85. if($this->eightbit) {
  86. $code->encodeString8bit($intext, $this->version, $this->level);
  87. } else {
  88. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  89. }
  90. QRtools::markTime('after_encode');
  91. if ($outfile!== false) {
  92. file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
  93. } else {
  94. return QRtools::binarize($code->data);
  95. }
  96. }
  97. //----------------------------------------------------------------------
  98. public function encodePNG($intext, $outfile = false,$saveandprint=false)
  99. {
  100. try {
  101. ob_start();
  102. $tab = $this->encode($intext);
  103. $err = ob_get_contents();
  104. ob_end_clean();
  105. if ($err != '')
  106. QRtools::log($outfile, "ERROR: " . $err);
  107. $maxSize = (int)(Constants::QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
  108. QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
  109. } catch (Exception $e) {
  110. echo $e->getMessage();
  111. die();
  112. QRtools::log($outfile, $e->getMessage());
  113. }
  114. }
  115. }