QROptionsTrait.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. use function array_values, count, is_array, is_numeric, max, min, sprintf;
  14. trait QROptionsTrait{
  15. /**
  16. * QR Code version number
  17. *
  18. * [1 ... 40] or QRCode::VERSION_AUTO
  19. *
  20. * @var int
  21. */
  22. protected $version = QRCode::VERSION_AUTO;
  23. /**
  24. * Minimum QR version (if $version = QRCode::VERSION_AUTO)
  25. *
  26. * @var int
  27. */
  28. protected $versionMin = 1;
  29. /**
  30. * Maximum QR version
  31. *
  32. * @var int
  33. */
  34. protected $versionMax = 40;
  35. /**
  36. * Error correct level
  37. *
  38. * QRCode::ECC_X where X is
  39. * L => 7%
  40. * M => 15%
  41. * Q => 25%
  42. * H => 30%
  43. *
  44. * @var int
  45. */
  46. protected $eccLevel = QRCode::ECC_L;
  47. /**
  48. * Mask Pattern to use
  49. *
  50. * [0...7] or QRCode::MASK_PATTERN_AUTO
  51. *
  52. * @var int
  53. */
  54. protected $maskPattern = QRCode::MASK_PATTERN_AUTO;
  55. /**
  56. * Add a "quiet zone" (margin) according to the QR code spec
  57. *
  58. * @var bool
  59. */
  60. protected $addQuietzone = true;
  61. /**
  62. * Size of the quiet zone
  63. *
  64. * internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules
  65. *
  66. * @var int
  67. */
  68. protected $quietzoneSize = 4;
  69. /**
  70. * QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
  71. * QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
  72. * QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
  73. * QRCode::OUTPUT_CUSTOM
  74. *
  75. * @var string
  76. */
  77. protected $outputType = QRCode::OUTPUT_IMAGE_PNG;
  78. /**
  79. * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
  80. *
  81. * @var string
  82. */
  83. protected $outputInterface;
  84. /**
  85. * /path/to/cache.file
  86. *
  87. * @var string
  88. */
  89. protected $cachefile;
  90. /**
  91. * newline string [HTML, SVG, TEXT]
  92. *
  93. * @var string
  94. */
  95. protected $eol = PHP_EOL;
  96. /**
  97. * size of a QR code pixel [SVG, IMAGE_*]
  98. * HTML -> via CSS
  99. *
  100. * @var int
  101. */
  102. protected $scale = 5;
  103. /**
  104. * a common css class
  105. *
  106. * @var string
  107. */
  108. protected $cssClass;
  109. /**
  110. * SVG opacity
  111. *
  112. * @var float
  113. */
  114. protected $svgOpacity = 1.0;
  115. /**
  116. * anything between <defs>
  117. *
  118. * @see https://developer.mozilla.org/docs/Web/SVG/Element/defs
  119. *
  120. * @var string
  121. */
  122. protected $svgDefs = '<style>rect{shape-rendering:crispEdges}</style>';
  123. /**
  124. * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
  125. *
  126. * viewBox="0 0 x x"
  127. *
  128. * @see https://css-tricks.com/scale-svg/#article-header-id-3
  129. *
  130. * @var int
  131. */
  132. protected $svgViewBoxSize;
  133. /**
  134. * string substitute for dark
  135. *
  136. * @var string
  137. */
  138. protected $textDark = '🔴';
  139. /**
  140. * string substitute for light
  141. *
  142. * @var string
  143. */
  144. protected $textLight = '⭕';
  145. /**
  146. * markup substitute for dark (CSS value)
  147. *
  148. * @var string
  149. */
  150. protected $markupDark = '#000';
  151. /**
  152. * markup substitute for light (CSS value)
  153. *
  154. * @var string
  155. */
  156. protected $markupLight = '#fff';
  157. /**
  158. * toggle base64 or raw image data
  159. *
  160. * @var bool
  161. */
  162. protected $imageBase64 = true;
  163. /**
  164. * toggle transparency, not supported by jpg
  165. *
  166. * @var bool
  167. */
  168. protected $imageTransparent = true;
  169. /**
  170. * @see imagecolortransparent()
  171. *
  172. * @var array [R, G, B]
  173. */
  174. protected $imageTransparencyBG = [255, 255, 255];
  175. /**
  176. * @see imagepng()
  177. *
  178. * @var int
  179. */
  180. protected $pngCompression = -1;
  181. /**
  182. * @see imagejpeg()
  183. *
  184. * @var int
  185. */
  186. protected $jpegQuality = 85;
  187. /**
  188. * Imagick output format
  189. *
  190. * @see Imagick::setType()
  191. *
  192. * @var string
  193. */
  194. protected $imagickFormat = 'png';
  195. /**
  196. * Imagick background color (defaults to "transparent")
  197. *
  198. * @see \ImagickPixel::__construct()
  199. *
  200. * @var string
  201. */
  202. protected $imagickBG;
  203. /**
  204. * Module values map
  205. *
  206. * HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
  207. * IMAGE: [63, 127, 255] // R, G, B
  208. *
  209. * @var array
  210. */
  211. protected $moduleValues;
  212. /**
  213. * clamp min/max version number
  214. *
  215. * @param int $versionMin
  216. * @param int $versionMax
  217. *
  218. * @return void
  219. */
  220. protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
  221. $min = max(1, min(40, $versionMin));
  222. $max = max(1, min(40, $versionMax));
  223. $this->versionMin = min($min, $max);
  224. $this->versionMax = max($min, $max);
  225. }
  226. /**
  227. * @param int $version
  228. *
  229. * @return void
  230. */
  231. protected function set_versionMin(int $version):void{
  232. $this->setMinMaxVersion($version, $this->versionMax);
  233. }
  234. /**
  235. * @param int $version
  236. *
  237. * @return void
  238. */
  239. protected function set_versionMax(int $version):void{
  240. $this->setMinMaxVersion($this->versionMin, $version);
  241. }
  242. /**
  243. * @param int $eccLevel
  244. *
  245. * @return void
  246. * @throws \chillerlan\QRCode\QRCodeException
  247. */
  248. protected function set_eccLevel(int $eccLevel):void{
  249. if(!isset(QRCode::ECC_MODES[$eccLevel])){
  250. throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
  251. }
  252. $this->eccLevel = $eccLevel;
  253. }
  254. /**
  255. * @param int $maskPattern
  256. *
  257. * @return void
  258. */
  259. protected function set_maskPattern(int $maskPattern):void{
  260. if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
  261. $this->maskPattern = max(0, min(7, $maskPattern));
  262. }
  263. }
  264. /**
  265. * @param mixed $imageTransparencyBG
  266. *
  267. * @return void
  268. * @throws \chillerlan\QRCode\QRCodeException
  269. */
  270. protected function set_imageTransparencyBG($imageTransparencyBG):void{
  271. // invalid value - set to white as default
  272. if(!is_array($imageTransparencyBG) || count($imageTransparencyBG) < 3){
  273. $this->imageTransparencyBG = [255, 255, 255];
  274. return;
  275. }
  276. foreach($imageTransparencyBG as $k => $v){
  277. if(!is_numeric($v)){
  278. throw new QRCodeException('Invalid RGB value.');
  279. }
  280. // clamp the values
  281. $this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
  282. }
  283. // use the array values to not run into errors with the spread operator (...$arr)
  284. $this->imageTransparencyBG = array_values($this->imageTransparencyBG);
  285. }
  286. /**
  287. * @param int $version
  288. *
  289. * @return void
  290. */
  291. protected function set_version(int $version):void{
  292. if($version !== QRCode::VERSION_AUTO){
  293. $this->version = max(1, min(40, $version));
  294. }
  295. }
  296. }