QROptionsTrait.php 6.5 KB

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