index.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Exemplatory usage
  6. *
  7. * PHP QR Code is distributed under LGPL 3
  8. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. echo "<h1>PHP QR Code</h1><hr/>";
  25. //set it to writable location, a place for temp generated PNG files
  26. $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
  27. //html PNG location prefix
  28. $PNG_WEB_DIR = 'temp/';
  29. include "qrlib.php";
  30. //ofcourse we need rights to create temp dir
  31. if (!file_exists($PNG_TEMP_DIR))
  32. mkdir($PNG_TEMP_DIR);
  33. $filename = $PNG_TEMP_DIR.'test.png';
  34. //processing form input
  35. //remember to sanitize user input in real-life solution !!!
  36. $errorCorrectionLevel = 'L';
  37. if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
  38. $errorCorrectionLevel = $_REQUEST['level'];
  39. $matrixPointSize = 4;
  40. if (isset($_REQUEST['size']))
  41. $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);
  42. if (isset($_REQUEST['data'])) {
  43. //it's very important!
  44. if (trim($_REQUEST['data']) == '')
  45. die('data cannot be empty! <a href="?">back</a>');
  46. // user data
  47. $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
  48. QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  49. } else {
  50. //default data
  51. echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';
  52. QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  53. }
  54. //display generated file
  55. echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';
  56. //config form
  57. echo '<form action="index.php" method="post">
  58. Data:&nbsp;<input name="data" value="'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code :)').'" />&nbsp;
  59. ECC:&nbsp;<select name="level">
  60. <option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option>
  61. <option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option>
  62. <option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option>
  63. <option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option>
  64. </select>&nbsp;
  65. Size:&nbsp;<select name="size">';
  66. for($i=1;$i<=10;$i++)
  67. echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>';
  68. echo '</select>&nbsp;
  69. <input type="submit" value="GENERATE"></form><hr/>';
  70. // benchmark
  71. QRtools::timeBenchmark();