国内流行的内容管理系统(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.

86 lines
3.9KB

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