国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
4.0KB

  1. <?php
  2. /**
  3. * PHP QR Code porting
  4. *
  5. * @porting author dwi.setiyadi@gmail.com
  6. * @original author http://phpqrcode.sourceforge.net/
  7. *
  8. * @version 1.0
  9. */
  10. class DedeQrcode
  11. {
  12. var $cacheable = true;
  13. var $cachedir = '/cache/';
  14. var $errorlog = '/logs/';
  15. var $quality = true;
  16. var $size = 1024;
  17. function __construct($config = array()) {
  18. $this->initialize($config);
  19. }
  20. public function initialize($config = array()) {
  21. $this->cacheable = (isset($config['cacheable'])) ? $config['cacheable'] : $this->cacheable;
  22. $this->cachedir = (isset($config['cachedir'])) ? $config['cachedir'] : DEDEDATA.$this->cachedir;
  23. $this->errorlog = (isset($config['errorlog'])) ? $config['errorlog'] : DEDEDATA.$this->errorlog;
  24. $this->quality = (isset($config['quality'])) ? $config['quality'] : $this->quality;
  25. $this->size = (isset($config['size'])) ? $config['size'] : $this->size;
  26. // use cache - more disk reads but less CPU power, masks and format templates are stored there
  27. if (!defined('QR_CACHEABLE')) define('QR_CACHEABLE', $this->cacheable);
  28. // used when QR_CACHEABLE === true
  29. if (!defined('QR_CACHE_DIR')) define('QR_CACHE_DIR', $this->cachedir);
  30. // default error logs dir
  31. if (!defined('QR_LOG_DIR')) define('QR_LOG_DIR', $this->errorlog);
  32. // if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
  33. if ($this->quality) {
  34. if (!defined('QR_FIND_BEST_MASK')) define('QR_FIND_BEST_MASK', true);
  35. } else {
  36. if (!defined('QR_FIND_BEST_MASK')) define('QR_FIND_BEST_MASK', false);
  37. if (!defined('QR_DEFAULT_MASK')) define('QR_DEFAULT_MASK', $this->quality);
  38. }
  39. // if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
  40. if (!defined('QR_FIND_FROM_RANDOM')) define('QR_FIND_FROM_RANDOM', false);
  41. // maximum allowed png image width (in pixels), tune to make sure GD and PHP can handle such big images
  42. if (!defined('QR_PNG_MAXIMUM_SIZE')) define('QR_PNG_MAXIMUM_SIZE', $this->size);
  43. // call original library
  44. require_once dirname(__FILE__)."/qrcode/qrconst.php";
  45. require_once dirname(__FILE__)."/qrcode/qrtools.php";
  46. require_once dirname(__FILE__)."/qrcode/qrspec.php";
  47. require_once dirname(__FILE__)."/qrcode/qrimage.php";
  48. require_once dirname(__FILE__)."/qrcode/qrinput.php";
  49. require_once dirname(__FILE__)."/qrcode/qrbitstream.php";
  50. require_once dirname(__FILE__)."/qrcode/qrsplit.php";
  51. require_once dirname(__FILE__)."/qrcode/qrrscode.php";
  52. require_once dirname(__FILE__)."/qrcode/qrmask.php";
  53. require_once dirname(__FILE__)."/qrcode/qrencode.php";
  54. }
  55. public function generate($params = array()) {
  56. if (isset($params['black'])
  57. && is_array($params['black'])
  58. && count($params['black']) == 3
  59. && array_filter($params['black'], 'is_int') === $params['black']) {
  60. QRimage::$black = $params['black'];
  61. }
  62. if (isset($params['white'])
  63. && is_array($params['white'])
  64. && count($params['white']) == 3
  65. && array_filter($params['white'], 'is_int') === $params['white']) {
  66. QRimage::$white = $params['white'];
  67. }
  68. $params['data'] = (isset($params['data'])) ? $params['data'] : 'QR Code Library';
  69. if (isset($params['savename'])) {
  70. $level = 'L';
  71. if (isset($params['level']) && in_array($params['level'], array('L','M','Q','H'))) $level = $params['level'];
  72. $size = 4;
  73. if (isset($params['size'])) $size = min(max((int)$params['size'], 1), 10);
  74. QRcode::png($params['data'], $params['savename'], $level, $size, 2);
  75. return $params['savename'];
  76. } else {
  77. $level = 'L';
  78. if (isset($params['level']) && in_array($params['level'], array('L','M','Q','H'))) $level = $params['level'];
  79. $size = 4;
  80. if (isset($params['size'])) $size = min(max((int)$params['size'], 1), 10);
  81. QRcode::png($params['data'], NULL, $level, $size, 2);
  82. }
  83. }
  84. }
  85. /* end of file */