QROptionsTrait.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Trait QROptionsTrait
  4. *
  5. * @filesource QROptionsTrait.php
  6. * @created 10.03.2018
  7. * @package chillerlan\QRCode
  8. * @author smiley <smiley@chillerlan.net>
  9. * @copyright 2018 smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode;
  13. trait QROptionsTrait{
  14. /**
  15. * QR Code version number
  16. *
  17. * [1 ... 40] or QRCode::VERSION_AUTO
  18. *
  19. * @var int
  20. */
  21. protected $version = QRCode::VERSION_AUTO;
  22. /**
  23. * Minimum QR version (if $version = QRCode::VERSION_AUTO)
  24. *
  25. * @var int
  26. */
  27. protected $versionMin = 1;
  28. /**
  29. * Maximum QR version
  30. *
  31. * @var int
  32. */
  33. protected $versionMax = 40;
  34. /**
  35. * Error correct level
  36. *
  37. * QRCode::ECC_X where X is
  38. * L => 7%
  39. * M => 15%
  40. * Q => 25%
  41. * H => 30%
  42. *
  43. * @var int
  44. */
  45. protected $eccLevel = QRCode::ECC_L;
  46. /**
  47. * Mask Pattern to use
  48. *
  49. * [0...7] or QRCode::MASK_PATTERN_AUTO
  50. *
  51. * @var int
  52. */
  53. protected $maskPattern = QRCode::MASK_PATTERN_AUTO;
  54. /**
  55. * Add a "quiet zone" (margin) according to the QR code spec
  56. *
  57. * @var bool
  58. */
  59. protected $addQuietzone = true;
  60. /**
  61. * Size of the quiet zone
  62. *
  63. * internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules
  64. *
  65. * @var int
  66. */
  67. protected $quietzoneSize = 4;
  68. /**
  69. * QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
  70. * QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
  71. * QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
  72. * QRCode::OUTPUT_CUSTOM
  73. *
  74. * @var string
  75. */
  76. protected $outputType = QRCode::OUTPUT_IMAGE_PNG;
  77. /**
  78. * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
  79. *
  80. * @var string
  81. */
  82. protected $outputInterface;
  83. /**
  84. * /path/to/cache.file
  85. *
  86. * @var string
  87. */
  88. protected $cachefile;
  89. /**
  90. * newline string [HTML, SVG, TEXT]
  91. *
  92. * @var string
  93. */
  94. protected $eol = PHP_EOL;
  95. /**
  96. * size of a QR code pixel [SVG, IMAGE_*]
  97. * HTML -> via CSS
  98. *
  99. * @var int
  100. */
  101. protected $scale = 5;
  102. /**
  103. * a common css class
  104. *
  105. * @var string
  106. */
  107. protected $cssClass;
  108. /**
  109. * SVG opacity
  110. *
  111. * @var float
  112. */
  113. protected $svgOpacity = 1.0;
  114. /**
  115. * anything between <defs>
  116. *
  117. * @see https://developer.mozilla.org/docs/Web/SVG/Element/defs
  118. *
  119. * @var string
  120. */
  121. protected $svgDefs = '<style>rect{shape-rendering:crispEdges}</style>';
  122. /**
  123. * string substitute for dark
  124. *
  125. * @var string
  126. */
  127. protected $textDark = '🔴';
  128. /**
  129. * string substitute for light
  130. *
  131. * @var string
  132. */
  133. protected $textLight = '⭕';
  134. /**
  135. * markup substitute for dark (CSS value)
  136. *
  137. * @var string
  138. */
  139. protected $markupDark = '#000';
  140. /**
  141. * markup substitute for light (CSS value)
  142. *
  143. * @var string
  144. */
  145. protected $markupLight = '#fff';
  146. /**
  147. * toggle base64 or raw image data
  148. *
  149. * @var bool
  150. */
  151. protected $imageBase64 = true;
  152. /**
  153. * toggle transparency, not supported by jpg
  154. *
  155. * @var bool
  156. */
  157. protected $imageTransparent = true;
  158. /**
  159. * @see imagecolortransparent()
  160. *
  161. * @var array [R, G, B]
  162. */
  163. protected $imageTransparencyBG = [255, 255, 255];
  164. /**
  165. * @see imagepng()
  166. *
  167. * @var int
  168. */
  169. protected $pngCompression = -1;
  170. /**
  171. * @see imagejpeg()
  172. *
  173. * @var int
  174. */
  175. protected $jpegQuality = 85;
  176. /**
  177. * Imagick output format
  178. *
  179. * @see Imagick::setType()
  180. *
  181. * @var string
  182. */
  183. protected $imagickFormat = 'png';
  184. /**
  185. * Imagick background color (defaults to "transparent")
  186. *
  187. * @see \ImagickPixel::__construct()
  188. *
  189. * @var string
  190. */
  191. protected $imagickBG;
  192. /**
  193. * Module values map
  194. *
  195. * HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
  196. * IMAGE: [63, 127, 255] // R, G, B
  197. *
  198. * @var array
  199. */
  200. protected $moduleValues;
  201. /**
  202. * Sets the options, called internally by the constructor
  203. *
  204. * @return void
  205. * @throws \chillerlan\QRCode\QRCodeException
  206. */
  207. public function QROptionsTrait():void{
  208. if(!array_key_exists($this->eccLevel, QRCode::ECC_MODES)){
  209. throw new QRCodeException('Invalid error correct level: '.$this->eccLevel);
  210. }
  211. if(!is_array($this->imageTransparencyBG) || count($this->imageTransparencyBG) < 3){
  212. $this->imageTransparencyBG = [255, 255, 255];
  213. }
  214. $this->version = (int)$this->version;
  215. // clamp min/max version number
  216. $max = $this->versionMax;
  217. $min = $this->versionMin;
  218. $this->versionMin = (int)min($min, $max);
  219. $this->versionMax = (int)max($min, $max);
  220. }
  221. }