QROptionsTrait.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
  124. *
  125. * viewBox="0 0 x x"
  126. *
  127. * @see https://css-tricks.com/scale-svg/#article-header-id-3
  128. *
  129. * @var int
  130. */
  131. protected $svgViewBoxSize;
  132. /**
  133. * string substitute for dark
  134. *
  135. * @var string
  136. */
  137. protected $textDark = '🔴';
  138. /**
  139. * string substitute for light
  140. *
  141. * @var string
  142. */
  143. protected $textLight = '⭕';
  144. /**
  145. * markup substitute for dark (CSS value)
  146. *
  147. * @var string
  148. */
  149. protected $markupDark = '#000';
  150. /**
  151. * markup substitute for light (CSS value)
  152. *
  153. * @var string
  154. */
  155. protected $markupLight = '#fff';
  156. /**
  157. * toggle base64 or raw image data
  158. *
  159. * @var bool
  160. */
  161. protected $imageBase64 = true;
  162. /**
  163. * toggle transparency, not supported by jpg
  164. *
  165. * @var bool
  166. */
  167. protected $imageTransparent = true;
  168. /**
  169. * @see imagecolortransparent()
  170. *
  171. * @var array [R, G, B]
  172. */
  173. protected $imageTransparencyBG = [255, 255, 255];
  174. /**
  175. * @see imagepng()
  176. *
  177. * @var int
  178. */
  179. protected $pngCompression = -1;
  180. /**
  181. * @see imagejpeg()
  182. *
  183. * @var int
  184. */
  185. protected $jpegQuality = 85;
  186. /**
  187. * Imagick output format
  188. *
  189. * @see Imagick::setType()
  190. *
  191. * @var string
  192. */
  193. protected $imagickFormat = 'png';
  194. /**
  195. * Imagick background color (defaults to "transparent")
  196. *
  197. * @see \ImagickPixel::__construct()
  198. *
  199. * @var string
  200. */
  201. protected $imagickBG;
  202. /**
  203. * Module values map
  204. *
  205. * HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
  206. * IMAGE: [63, 127, 255] // R, G, B
  207. *
  208. * @var array
  209. */
  210. protected $moduleValues;
  211. /**
  212. * set/clamp some special values, call the parent setter otherwise
  213. *
  214. * @param string $property
  215. * @param mixed $value
  216. *
  217. * @return void
  218. */
  219. public function __set(string $property, $value):void{
  220. if($property === 'versionMin'){
  221. $this->setMinMaxVersion($value, $this->versionMax);
  222. return;
  223. }
  224. elseif($property === 'versionMax'){
  225. $this->setMinMaxVersion($this->versionMin, $value);
  226. return;
  227. }
  228. parent::__set($property, $value);
  229. }
  230. /**
  231. * clamp min/max version number
  232. *
  233. * @param int $versionMin
  234. * @param int $versionMax
  235. *
  236. * @return void
  237. */
  238. protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
  239. $min = \max(1, \min(40, $versionMin));
  240. $max = \max(1, \min(40, $versionMax));
  241. $this->versionMin = \min($min, $max);
  242. $this->versionMax = \max($min, $max);
  243. }
  244. /**
  245. * @param int $eccLevel
  246. *
  247. * @return void
  248. * @throws \chillerlan\QRCode\QRCodeException
  249. */
  250. protected function set_eccLevel(int $eccLevel):void{
  251. if(!isset(QRCode::ECC_MODES[$eccLevel])){
  252. throw new QRCodeException('Invalid error correct level: '.$eccLevel);
  253. }
  254. $this->eccLevel = $eccLevel;
  255. }
  256. /**
  257. * @param int $maskPattern
  258. *
  259. * @return void
  260. */
  261. protected function set_maskPattern(int $maskPattern):void{
  262. if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
  263. $this->maskPattern = \max(0, \min(7, $maskPattern));
  264. }
  265. }
  266. /**
  267. * @param mixed $imageTransparencyBG
  268. *
  269. * @return void
  270. * @throws \chillerlan\QRCode\QRCodeException
  271. */
  272. protected function set_imageTransparencyBG($imageTransparencyBG):void{
  273. // invalid value - set to white as default
  274. if(!\is_array($imageTransparencyBG) || \count($imageTransparencyBG) < 3){
  275. $this->imageTransparencyBG = [255, 255, 255];
  276. return;
  277. }
  278. foreach($imageTransparencyBG as $k => $v){
  279. if(!\is_numeric($v)){
  280. throw new QRCodeException('Invalid RGB value.');
  281. }
  282. // clamp the values
  283. $this->imageTransparencyBG[$k] = \max(0, \min(255, (int)$v));
  284. }
  285. // use the array values to not run into errors with the spread operator (...$arr)
  286. $this->imageTransparencyBG = \array_values($this->imageTransparencyBG);
  287. }
  288. /**
  289. * @param int $version
  290. *
  291. * @return void
  292. */
  293. protected function set_version(int $version):void{
  294. if($version !== QRCode::VERSION_AUTO){
  295. $this->version = \max(1, \min(40, $version));
  296. }
  297. }
  298. }