qrcode.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. //set it to writable location, a place for temp generated PNG files
  3. $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
  4. //html PNG location prefix
  5. $PNG_WEB_DIR = 'temp/';
  6. include "phpqrcode/qrlib.php";
  7. //ofcourse we need rights to create temp dir
  8. if (!file_exists($PNG_TEMP_DIR))
  9. mkdir($PNG_TEMP_DIR);
  10. $filename = $PNG_TEMP_DIR.'test.png';
  11. //processing form input
  12. //remember to sanitize user input in real-life solution !!!
  13. $errorCorrectionLevel = 'L';
  14. if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
  15. $errorCorrectionLevel = $_REQUEST['level'];
  16. $matrixPointSize = 4;
  17. if (isset($_REQUEST['size']))
  18. $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);
  19. if (isset($_REQUEST['data'])) {
  20. //it's very important!
  21. if (trim($_REQUEST['data']) == '')
  22. die('data cannot be empty! <a href="?">back</a>');
  23. // user data
  24. $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
  25. QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  26. } else {
  27. //default data
  28. //echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';
  29. QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  30. }