QROptionsTrait.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * @see https://github.com/chillerlan/php-qrcode/issues/97 (changed default value to '')
  70. */
  71. protected string $dataModeOverride = '';
  72. /**
  73. * The output type
  74. *
  75. * - QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
  76. * - QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
  77. * - QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
  78. * - QRCode::OUTPUT_CUSTOM
  79. */
  80. protected string $outputType = QRCode::OUTPUT_IMAGE_PNG;
  81. /**
  82. * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
  83. */
  84. protected ?string $outputInterface = null;
  85. /**
  86. * /path/to/cache.file
  87. */
  88. protected ?string $cachefile = null;
  89. /**
  90. * newline string [HTML, SVG, TEXT]
  91. */
  92. protected string $eol = PHP_EOL;
  93. /**
  94. * size of a QR code pixel [SVG, IMAGE_*], HTML via CSS
  95. */
  96. protected int $scale = 5;
  97. /**
  98. * a common css class
  99. */
  100. protected string $cssClass = '';
  101. /**
  102. * SVG opacity
  103. */
  104. protected float $svgOpacity = 1.0;
  105. /**
  106. * anything between <defs>
  107. *
  108. * @see https://developer.mozilla.org/docs/Web/SVG/Element/defs
  109. */
  110. protected string $svgDefs = '<style>rect{shape-rendering:crispEdges}</style>';
  111. /**
  112. * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
  113. *
  114. * viewBox="0 0 x x"
  115. *
  116. * @see https://css-tricks.com/scale-svg/#article-header-id-3
  117. */
  118. protected ?int $svgViewBoxSize = null;
  119. /**
  120. * string substitute for dark
  121. */
  122. protected string $textDark = '██';
  123. /**
  124. * string substitute for light
  125. */
  126. protected string $textLight = '░░';
  127. /**
  128. * markup substitute for dark (CSS value)
  129. */
  130. protected string $markupDark = '#000';
  131. /**
  132. * markup substitute for light (CSS value)
  133. */
  134. protected string $markupLight = '#fff';
  135. /**
  136. * Return the image resource instead of a render if applicable.
  137. * This option overrides other output options, such as $cachefile and $imageBase64.
  138. *
  139. * Supported by the following modules:
  140. *
  141. * - QRImage: resource (PHP < 8), GdImage
  142. * - QRImagick: Imagick
  143. * - QRFpdf: FPDF
  144. *
  145. * @see \chillerlan\QRCode\Output\QROutputInterface::dump()
  146. *
  147. * @var bool
  148. */
  149. protected bool $returnResource = false;
  150. /**
  151. * toggle base64 or raw image data
  152. */
  153. protected bool $imageBase64 = true;
  154. /**
  155. * toggle transparency, not supported by jpg
  156. */
  157. protected bool $imageTransparent = true;
  158. /**
  159. * @see imagecolortransparent()
  160. *
  161. * [R, G, B]
  162. */
  163. protected array $imageTransparencyBG = [255, 255, 255];
  164. /**
  165. * @see imagepng()
  166. */
  167. protected int $pngCompression = -1;
  168. /**
  169. * @see imagejpeg()
  170. */
  171. protected int $jpegQuality = 85;
  172. /**
  173. * Imagick output format
  174. *
  175. * @see \Imagick::setType()
  176. */
  177. protected string $imagickFormat = 'png';
  178. /**
  179. * Imagick background color (defaults to "transparent")
  180. *
  181. * @see \ImagickPixel::__construct()
  182. */
  183. protected ?string $imagickBG = null;
  184. /**
  185. * Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt")
  186. *
  187. * @see \FPDF::__construct()
  188. */
  189. protected string $fpdfMeasureUnit = 'pt';
  190. /**
  191. * Module values map
  192. *
  193. * - HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
  194. * - IMAGE: [63, 127, 255] // R, G, B
  195. */
  196. protected ?array $moduleValues = null;
  197. /**
  198. * clamp min/max version number
  199. */
  200. protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
  201. $min = max(1, min(40, $versionMin));
  202. $max = max(1, min(40, $versionMax));
  203. $this->versionMin = min($min, $max);
  204. $this->versionMax = max($min, $max);
  205. }
  206. /**
  207. * sets the minimum version number
  208. */
  209. protected function set_versionMin(int $version):void{
  210. $this->setMinMaxVersion($version, $this->versionMax);
  211. }
  212. /**
  213. * sets the maximum version number
  214. */
  215. protected function set_versionMax(int $version):void{
  216. $this->setMinMaxVersion($this->versionMin, $version);
  217. }
  218. /**
  219. * sets the error correction level
  220. *
  221. * @throws \chillerlan\QRCode\QRCodeException
  222. */
  223. protected function set_eccLevel(int $eccLevel):void{
  224. if(!isset(QRCode::ECC_MODES[$eccLevel])){
  225. throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
  226. }
  227. $this->eccLevel = $eccLevel;
  228. }
  229. /**
  230. * sets/clamps the mask pattern
  231. */
  232. protected function set_maskPattern(int $maskPattern):void{
  233. if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
  234. $this->maskPattern = max(0, min(7, $maskPattern));
  235. }
  236. }
  237. /**
  238. * sets the transparency background color
  239. *
  240. * @throws \chillerlan\QRCode\QRCodeException
  241. */
  242. protected function set_imageTransparencyBG(array $imageTransparencyBG):void{
  243. // invalid value - set to white as default
  244. if(count($imageTransparencyBG) < 3){
  245. $this->imageTransparencyBG = [255, 255, 255];
  246. return;
  247. }
  248. foreach($imageTransparencyBG as $k => $v){
  249. // cut off exceeding items
  250. if($k > 2){
  251. break;
  252. }
  253. if(!is_numeric($v)){
  254. throw new QRCodeException('Invalid RGB value.');
  255. }
  256. // clamp the values
  257. $this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
  258. }
  259. // use the array values to not run into errors with the spread operator (...$arr)
  260. $this->imageTransparencyBG = array_values($this->imageTransparencyBG);
  261. }
  262. /**
  263. * sets/clamps the version number
  264. */
  265. protected function set_version(int $version):void{
  266. if($version !== QRCode::VERSION_AUTO){
  267. $this->version = max(1, min(40, $version));
  268. }
  269. }
  270. /**
  271. * sets the FPDF measurement unit
  272. *
  273. * @codeCoverageIgnore
  274. */
  275. protected function set_fpdfMeasureUnit(string $unit):void{
  276. $unit = strtolower($unit);
  277. if(in_array($unit, ['cm', 'in', 'mm', 'pt'], true)){
  278. $this->fpdfMeasureUnit = $unit;
  279. }
  280. // @todo throw or ignore silently?
  281. }
  282. }