QROptionsTrait.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * Trait QROptionsTrait
  4. *
  5. * @created 10.03.2018
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2018 smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpUnused
  11. */
  12. namespace chillerlan\QRCode;
  13. use chillerlan\QRCode\Output\QROutputInterface;
  14. use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
  15. use chillerlan\QRCode\Decoder\{GDLuminanceSource, IMagickLuminanceSource};
  16. use function array_values, count, extension_loaded, 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 Version::AUTO
  25. */
  26. protected int $version = 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 (no value in using, mostly for unit testing purposes)
  50. *
  51. * [0...7] or MaskPattern::PATTERN_AUTO
  52. */
  53. protected int $maskPattern = MaskPattern::AUTO;
  54. /**
  55. * Add a "quiet zone" (margin) according to the QR code spec
  56. *
  57. * @see https://www.qrcode.com/en/howto/code.html
  58. */
  59. protected bool $addQuietzone = true;
  60. /**
  61. * Size of the quiet zone
  62. *
  63. * internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules
  64. */
  65. protected int $quietzoneSize = 4;
  66. /**
  67. * The output type
  68. *
  69. * - QROutputInterface::MARKUP_XXXX where XXXX = HTML, SVG
  70. * - QROutputInterface::GDIMAGE_XXX where XXX = PNG, GIF, JPG
  71. * - QROutputInterface::STRING_XXXX where XXXX = TEXT, JSON
  72. * - QROutputInterface::IMAGICK
  73. * - QROutputInterface::EPS
  74. * - QROutputInterface::FPDF
  75. * - QROutputInterface::CUSTOM
  76. */
  77. protected string $outputType = QROutputInterface::MARKUP_SVG;
  78. /**
  79. * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
  80. */
  81. protected ?string $outputInterface = null;
  82. /**
  83. * /path/to/cache.file
  84. */
  85. protected ?string $cachefile = null;
  86. /**
  87. * newline string [HTML, SVG, TEXT]
  88. */
  89. protected string $eol = PHP_EOL;
  90. /**
  91. * size of a QR code pixel [SVG, IMAGE_*], HTML via CSS
  92. */
  93. protected int $scale = 5;
  94. /**
  95. * a common css class
  96. */
  97. protected string $cssClass = 'qrcode';
  98. /**
  99. * SVG opacity
  100. */
  101. protected float $svgOpacity = 1.0;
  102. /**
  103. * anything between <defs>
  104. *
  105. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
  106. */
  107. protected string $svgDefs = '';
  108. /**
  109. * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
  110. *
  111. * viewBox="0 0 x x"
  112. *
  113. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
  114. * @see https://css-tricks.com/scale-svg/#article-header-id-3
  115. */
  116. protected ?int $svgViewBoxSize = null;
  117. /**
  118. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
  119. */
  120. protected string $svgPreserveAspectRatio = 'xMidYMid';
  121. /**
  122. * optional "width" attribute with the specified value (note that the value is not checked!)
  123. *
  124. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/width
  125. */
  126. protected ?string $svgWidth = null;
  127. /**
  128. * optional "height" attribute with the specified value (note that the value is not checked!)
  129. *
  130. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/height
  131. */
  132. protected ?string $svgHeight = null;
  133. /**
  134. * whether to connect the paths for the several module types to avoid weird glitches when using gradients etc.
  135. *
  136. * @see https://github.com/chillerlan/php-qrcode/issues/57
  137. */
  138. protected bool $connectPaths = false;
  139. /**
  140. * specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true
  141. */
  142. protected array $excludeFromConnect = [];
  143. /**
  144. * specify whether to draw the modules as filled circles
  145. *
  146. * a note for GDImage output:
  147. *
  148. * if QROptions::$scale is less or equal than 20, the image will be upscaled internally, then the modules will be drawn
  149. * using imagefilledellipse() and then scaled back to the expected size using IMG_BICUBIC which in turn produces
  150. * unexpected outcomes in combination with transparency - to avoid this, set scale to a value greater than 20.
  151. *
  152. * @see https://github.com/chillerlan/php-qrcode/issues/23
  153. * @see https://github.com/chillerlan/php-qrcode/discussions/122
  154. */
  155. protected bool $drawCircularModules = false;
  156. /**
  157. * specifies the radius of the modules when $svgDrawCircularModules is set to true
  158. */
  159. protected float $circleRadius = 0.45;
  160. /**
  161. * specifies which module types to exclude when $svgDrawCircularModules is set to true
  162. */
  163. protected array $keepAsSquare = [];
  164. /**
  165. * string substitute for dark
  166. */
  167. protected string $textDark = '🔴';
  168. /**
  169. * string substitute for light
  170. */
  171. protected string $textLight = '⭕';
  172. /**
  173. * markup substitute for dark (CSS value)
  174. */
  175. protected string $markupDark = '#000';
  176. /**
  177. * markup substitute for light (CSS value)
  178. */
  179. protected string $markupLight = '#fff';
  180. /**
  181. * Return the image resource instead of a render if applicable.
  182. * This option overrides other output options, such as $cachefile and $imageBase64.
  183. *
  184. * Supported by the following modules:
  185. *
  186. * - QRImage: resource (PHP < 8), GdImage
  187. * - QRImagick: Imagick
  188. * - QRFpdf: FPDF
  189. *
  190. * @see \chillerlan\QRCode\Output\QROutputInterface::dump()
  191. *
  192. * @var bool
  193. */
  194. protected bool $returnResource = false;
  195. /**
  196. * toggle base64 or raw image data
  197. */
  198. protected bool $imageBase64 = true;
  199. /**
  200. * toggle background transparency
  201. *
  202. * - In GdImage mode (png, gif) it sets imagecolortransparent() with QROptions::$imageTransparencyBG.
  203. * It also sets the "normal" background color without transparency switch.
  204. *
  205. * - In SVG mode (as of v5), it won't render the "light" modules,
  206. * as opacity/transparency can easily be set with css properties.
  207. *
  208. * - It has no effect in the FPDF and Imagick output modules.
  209. *
  210. * @see \chillerlan\QRCode\QROptions::$imageTransparencyBG
  211. * @see https://github.com/chillerlan/php-qrcode/discussions/121
  212. */
  213. protected bool $imageTransparent = true;
  214. /**
  215. * Sets the background color in GD mode.
  216. *
  217. * When QROptions::$imageTransparent is set to true, this color is set as transparent in imagecolortransparent()
  218. *
  219. * @see \chillerlan\QRCode\Output\QRGdImage
  220. * @see \chillerlan\QRCode\QROptions::$imageTransparent
  221. * @see imagecolortransparent()
  222. *
  223. * [R, G, B]
  224. */
  225. protected array $imageTransparencyBG = [255, 255, 255];
  226. /**
  227. * @see imagepng()
  228. */
  229. protected int $pngCompression = -1;
  230. /**
  231. * @see imagejpeg()
  232. */
  233. protected int $jpegQuality = 85;
  234. /**
  235. * Imagick output format
  236. *
  237. * @see \Imagick::setImageFormat()
  238. * @see https://www.imagemagick.org/script/formats.php
  239. */
  240. protected string $imagickFormat = 'png32';
  241. /**
  242. * Imagick background color (defaults to "transparent")
  243. *
  244. * @see \ImagickPixel::__construct()
  245. */
  246. protected ?string $imagickBG = null;
  247. /**
  248. * Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt")
  249. *
  250. * @see \FPDF::__construct()
  251. */
  252. protected string $fpdfMeasureUnit = 'pt';
  253. /**
  254. * Module values map
  255. *
  256. * - HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
  257. * - IMAGE: [63, 127, 255] // R, G, B
  258. */
  259. protected ?array $moduleValues = null;
  260. /**
  261. * use Imaagick (if available) when reading QR Codes
  262. */
  263. protected bool $readerUseImagickIfAvailable = false;
  264. /**
  265. * grayscale the image before reading
  266. */
  267. protected bool $readerGrayscale = false;
  268. /**
  269. * increase the contrast before reading
  270. *
  271. * note that applying contrast works different in GD and Imagick, so mileage may vary
  272. */
  273. protected bool $readerIncreaseContrast = false;
  274. /**
  275. * Toggles logo space creation
  276. */
  277. protected bool $addLogoSpace = false;
  278. /**
  279. * width of the logo space
  280. */
  281. protected int $logoSpaceWidth = 0;
  282. /**
  283. * height of the logo space
  284. */
  285. protected int $logoSpaceHeight = 0;
  286. /**
  287. * optional horizontal start position of the logo space (top left corner)
  288. */
  289. protected ?int $logoSpaceStartX = null;
  290. /**
  291. * optional vertical start position of the logo space (top left corner)
  292. */
  293. protected ?int $logoSpaceStartY = null;
  294. /**
  295. * clamp min/max version number
  296. */
  297. protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
  298. $min = max(1, min(40, $versionMin));
  299. $max = max(1, min(40, $versionMax));
  300. $this->versionMin = min($min, $max);
  301. $this->versionMax = max($min, $max);
  302. }
  303. /**
  304. * sets the minimum version number
  305. */
  306. protected function set_versionMin(int $version):void{
  307. $this->setMinMaxVersion($version, $this->versionMax);
  308. }
  309. /**
  310. * sets the maximum version number
  311. */
  312. protected function set_versionMax(int $version):void{
  313. $this->setMinMaxVersion($this->versionMin, $version);
  314. }
  315. /**
  316. * sets/clamps the version number
  317. */
  318. protected function set_version(int $version):void{
  319. if($version !== Version::AUTO){
  320. $this->version = max(1, min(40, $version));
  321. }
  322. }
  323. /**
  324. * sets the error correction level
  325. *
  326. * @throws \chillerlan\QRCode\QRCodeException
  327. */
  328. protected function set_eccLevel(int $eccLevel):void{
  329. if(!in_array($eccLevel, [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H], true)){
  330. throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
  331. }
  332. $this->eccLevel = $eccLevel;
  333. }
  334. /**
  335. * sets/clamps the mask pattern
  336. */
  337. protected function set_maskPattern(int $maskPattern):void{
  338. if($maskPattern !== MaskPattern::AUTO){
  339. $this->maskPattern = max(0, min(7, $maskPattern));
  340. }
  341. }
  342. /**
  343. * sets/clamps the quiet zone size
  344. */
  345. protected function set_quietzoneSize(int $quietzoneSize):void{
  346. $this->quietzoneSize = max(0, min($quietzoneSize, 75));
  347. }
  348. /**
  349. * sets the transparency background color
  350. *
  351. * @throws \chillerlan\QRCode\QRCodeException
  352. */
  353. protected function set_imageTransparencyBG(array $imageTransparencyBG):void{
  354. // invalid value - set to white as default
  355. if(count($imageTransparencyBG) < 3){
  356. $this->imageTransparencyBG = [255, 255, 255];
  357. return;
  358. }
  359. foreach($imageTransparencyBG as $k => $v){
  360. // cut off exceeding items
  361. if($k > 2){
  362. break;
  363. }
  364. if(!is_numeric($v)){
  365. throw new QRCodeException('Invalid RGB value.');
  366. }
  367. // clamp the values
  368. $this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
  369. }
  370. // use the array values to not run into errors with the spread operator (...$arr)
  371. $this->imageTransparencyBG = array_values($this->imageTransparencyBG);
  372. }
  373. /**
  374. * sets the FPDF measurement unit
  375. *
  376. * @codeCoverageIgnore
  377. */
  378. protected function set_fpdfMeasureUnit(string $unit):void{
  379. $unit = strtolower($unit);
  380. if(in_array($unit, ['cm', 'in', 'mm', 'pt'], true)){
  381. $this->fpdfMeasureUnit = $unit;
  382. }
  383. // @todo throw or ignore silently?
  384. }
  385. /**
  386. * enables Imagick for the QR Code reader if the extension is available
  387. */
  388. protected function set_readerUseImagickIfAvailable(bool $useImagickIfAvailable):void{
  389. $this->readerUseImagickIfAvailable = $useImagickIfAvailable && extension_loaded('imagick');
  390. }
  391. /**
  392. * returns the FQCN of the luminance source class to use in the reader (GD or Imagick)
  393. *
  394. * @see \chillerlan\QRCode\Decoder\LuminanceSourceInterface
  395. */
  396. public function getLuminanceSourceFQCN():string{
  397. // i still hate this
  398. return $this->readerUseImagickIfAvailable
  399. ? IMagickLuminanceSource::class
  400. : GDLuminanceSource::class;
  401. }
  402. /**
  403. * clamp the logo space values between 0 and maximum length (177 modules at version 40)
  404. */
  405. protected function clampLogoSpaceValue(int $value):int{
  406. return (int)max(0, min(177, $value));
  407. }
  408. /**
  409. * clamp/set logo space width
  410. */
  411. protected function set_logoSpaceWidth(int $value):void{
  412. $this->logoSpaceWidth = $this->clampLogoSpaceValue($value);
  413. }
  414. /**
  415. * clamp/set logo space height
  416. */
  417. protected function set_logoSpaceHeight(int $value):void{
  418. $this->logoSpaceHeight = $this->clampLogoSpaceValue($value);
  419. }
  420. /**
  421. * clamp/set horizontal logo space start
  422. */
  423. protected function set_logoSpaceStartX(?int $value):void{
  424. $this->logoSpaceStartX = $value === null ? null : $this->clampLogoSpaceValue($value);
  425. }
  426. /**
  427. * clamp/set vertical logo space start
  428. */
  429. protected function set_logoSpaceStartY(?int $value):void{
  430. $this->logoSpaceStartY = $value === null ? null : $this->clampLogoSpaceValue($value);
  431. }
  432. /**
  433. * clamp/set SVG circle radius
  434. */
  435. protected function set_circleRadius(float $circleRadius):void{
  436. $this->circleRadius = max(0.1, min(0.75, $circleRadius));
  437. }
  438. }