/Ciqrcode.php

http://github.com/dwisetiyadi/CodeIgniter-PHP-QR-Code · PHP · 106 lines · 69 code · 18 blank · 19 comment · 28 complexity · 56a4d29d53a1de4403adcbb1f49ddcf2 MD5 · raw file

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