QROptionsTrait.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. * set/clamp some special values, call the parent setter otherwise
  203. *
  204. * @param string $property
  205. * @param mixed $value
  206. *
  207. * @return void
  208. */
  209. public function __set(string $property, $value):void{
  210. if(in_array($property, ['eccLevel', 'maskPattern', 'imageTransparencyBG', 'version'], true)){
  211. $this->{'set_'.$property}($value);
  212. return;
  213. }
  214. elseif($property === 'versionMin'){
  215. $this->setMinMaxVersion($value, $this->versionMax);
  216. return;
  217. }
  218. elseif($property === 'versionMax'){
  219. $this->setMinMaxVersion($this->versionMin, $value);
  220. return;
  221. }
  222. parent::__set($property, $value);
  223. }
  224. /**
  225. * clamp min/max version number
  226. *
  227. * @param int $versionMin
  228. * @param int $versionMax
  229. *
  230. * @return void
  231. */
  232. protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
  233. $min = max(1, min(40, $versionMin));
  234. $max = max(1, min(40, $versionMax));
  235. $this->versionMin = min($min, $max);
  236. $this->versionMax = max($min, $max);
  237. }
  238. /**
  239. * @param int $eccLevel
  240. *
  241. * @return void
  242. * @throws \chillerlan\QRCode\QRCodeException
  243. */
  244. protected function set_eccLevel(int $eccLevel):void{
  245. if(!isset(QRCode::ECC_MODES[$eccLevel])){
  246. throw new QRCodeException('Invalid error correct level: '.$eccLevel);
  247. }
  248. $this->eccLevel = $eccLevel;
  249. }
  250. /**
  251. * @param int $maskPattern
  252. *
  253. * @return void
  254. */
  255. protected function set_maskPattern(int $maskPattern):void{
  256. if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
  257. $this->maskPattern = max(0, min(7, $maskPattern));
  258. }
  259. }
  260. /**
  261. * @param mixed $imageTransparencyBG
  262. *
  263. * @return void
  264. * @throws \chillerlan\QRCode\QRCodeException
  265. */
  266. protected function set_imageTransparencyBG($imageTransparencyBG):void{
  267. // invalid value - set to white as default
  268. if(!is_array($imageTransparencyBG) || count($imageTransparencyBG) < 3){
  269. $this->imageTransparencyBG = [255, 255, 255];
  270. return;
  271. }
  272. foreach($imageTransparencyBG as $k => $v){
  273. if(!is_numeric($v)){
  274. throw new QRCodeException('Invalid RGB value.');
  275. }
  276. // clamp the values
  277. $this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
  278. }
  279. // use the array values to not run into errors with the spread operator (...$arr)
  280. $this->imageTransparencyBG = array_values($this->imageTransparencyBG);
  281. }
  282. /**
  283. * @param int $version
  284. *
  285. * @return void
  286. */
  287. protected function set_version(int $version):void{
  288. if($version !== QRCode::VERSION_AUTO){
  289. $this->version = max(1, min(40, $version));
  290. }
  291. }
  292. }