QROptionsTrait.php 6.7 KB

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