You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QrCode.php 46 KiB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. <?php
  2. /*
  3. * (c) Jeroen van den Enden <info@endroid.nl>
  4. *
  5. * This source file is subject to the MIT license that is bundled
  6. * with this source code in the file LICENSE.
  7. */
  8. namespace Endroid\QrCode;
  9. use Endroid\QrCode\Exceptions\DataDoesntExistsException;
  10. use Endroid\QrCode\Exceptions\FreeTypeLibraryMissingException;
  11. use Endroid\QrCode\Exceptions\ImageFunctionFailedException;
  12. use Endroid\QrCode\Exceptions\ImageTypeInvalidException;
  13. use Endroid\QrCode\Exceptions\VersionTooLargeException;
  14. use Endroid\QrCode\Exceptions\ImageSizeTooLargeException;
  15. use Endroid\QrCode\Exceptions\ImageFunctionUnknownException;
  16. use ReflectionFunction;
  17. /**
  18. * Generate QR Code.
  19. */
  20. class QrCode
  21. {
  22. /** @const int Error Correction Level Low (7%) */
  23. const LEVEL_LOW = 1;
  24. /** @const int Error Correction Level Medium (15%) */
  25. const LEVEL_MEDIUM = 0;
  26. /** @const int Error Correction Level Quartile (25%) */
  27. const LEVEL_QUARTILE = 3;
  28. /** @const int Error Correction Level High (30%) */
  29. const LEVEL_HIGH = 2;
  30. /** @const string Image type png */
  31. const IMAGE_TYPE_PNG = 'png';
  32. /** @const string Image type gif */
  33. const IMAGE_TYPE_GIF = 'gif';
  34. /** @const string Image type jpeg */
  35. const IMAGE_TYPE_JPEG = 'jpeg';
  36. /** @const string Image type wbmp */
  37. const IMAGE_TYPE_WBMP = 'wbmp';
  38. /** @const int Horizontal label alignment to the center of image */
  39. const LABEL_HALIGN_CENTER = 0;
  40. /** @const int Horizontal label alignment to the left side of image */
  41. const LABEL_HALIGN_LEFT = 1;
  42. /** @const int Horizontal label alignment to the left border of QR Code */
  43. const LABEL_HALIGN_LEFT_BORDER = 2;
  44. /** @const int Horizontal label alignment to the left side of QR Code */
  45. const LABEL_HALIGN_LEFT_CODE = 3;
  46. /** @const int Horizontal label alignment to the right side of image */
  47. const LABEL_HALIGN_RIGHT = 4;
  48. /** @const int Horizontal label alignment to the right border of QR Code */
  49. const LABEL_HALIGN_RIGHT_BORDER = 5;
  50. /** @const int Horizontal label alignment to the right side of QR Code */
  51. const LABEL_HALIGN_RIGHT_CODE = 6;
  52. /** @const int Vertical label alignment to the top */
  53. const LABEL_VALIGN_TOP = 1;
  54. /** @const int Vertical label alignment to the top and hide border */
  55. const LABEL_VALIGN_TOP_NO_BORDER = 2;
  56. /** @const int Vertical label alignment to the middle*/
  57. const LABEL_VALIGN_MIDDLE = 3;
  58. /** @const int Vertical label alignment to the bottom */
  59. const LABEL_VALIGN_BOTTOM = 4;
  60. /** @var string */
  61. protected $logo = null;
  62. protected $logo_size = 48;
  63. /** @var string */
  64. protected $text = '';
  65. /** @var int */
  66. protected $size = 0;
  67. /** @var int */
  68. protected $padding = 16;
  69. /** @var bool */
  70. protected $draw_quiet_zone = false;
  71. /** @var bool */
  72. protected $draw_border = false;
  73. /** @var array */
  74. protected $color_foreground = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0];
  75. /** @var array */
  76. protected $color_background = ['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0];
  77. /** @var string */
  78. protected $label = '';
  79. /** @var int */
  80. protected $label_font_size = 16;
  81. /** @var string */
  82. protected $label_font_path = '';
  83. /** @var int */
  84. protected $label_halign = self::LABEL_HALIGN_CENTER;
  85. /** @var int */
  86. protected $label_valign = self::LABEL_VALIGN_MIDDLE;
  87. /** @var resource */
  88. protected $image = null;
  89. /** @var int */
  90. protected $version;
  91. /** @var int */
  92. protected $error_correction = self::LEVEL_MEDIUM;
  93. /** @var array */
  94. protected $error_corrections_available = [
  95. self::LEVEL_LOW,
  96. self::LEVEL_MEDIUM,
  97. self::LEVEL_QUARTILE,
  98. self::LEVEL_HIGH,
  99. ];
  100. /** @var int */
  101. protected $module_size;
  102. /** @var string */
  103. protected $image_type = self::IMAGE_TYPE_PNG;
  104. /** @var array */
  105. protected $image_types_available = [
  106. self::IMAGE_TYPE_GIF,
  107. self::IMAGE_TYPE_PNG,
  108. self::IMAGE_TYPE_JPEG,
  109. self::IMAGE_TYPE_WBMP,
  110. ];
  111. /** @var string */
  112. protected $image_path;
  113. /** @var string */
  114. protected $path;
  115. /** @var int */
  116. protected $structure_append_n;
  117. /** @var int */
  118. protected $structure_append_m;
  119. /** @var int */
  120. protected $structure_append_parity;
  121. /** @var string */
  122. protected $structure_append_original_data;
  123. /**
  124. * Class constructor.
  125. *
  126. * @param string $text
  127. */
  128. public function __construct($text = '')
  129. {
  130. $this->setPath(__DIR__.'/../assets/data');
  131. $this->setImagePath(__DIR__.'/../assets/image');
  132. $this->setLabelFontPath(__DIR__.'/../assets/font/opensans.ttf');
  133. $this->setText($text);
  134. }
  135. /**
  136. * Set structure append.
  137. *
  138. * @param int $n
  139. * @param int $m
  140. * @param int $parity Parity
  141. * @param string $original_data Original data
  142. *
  143. * @return QrCode
  144. */
  145. public function setStructureAppend($n, $m, $parity, $original_data)
  146. {
  147. $this->structure_append_n = $n;
  148. $this->structure_append_m = $m;
  149. $this->structure_append_parity = $parity;
  150. $this->structure_append_original_data = $original_data;
  151. return $this;
  152. }
  153. /**
  154. * Set QR Code version.
  155. *
  156. * @param int $version QR Code version
  157. *
  158. * @return QrCode
  159. */
  160. public function setVersion($version)
  161. {
  162. if ($version <= 40 && $version >= 0) {
  163. $this->version = $version;
  164. }
  165. return $this;
  166. }
  167. /**
  168. * Return QR Code version.
  169. *
  170. * @return int
  171. */
  172. public function getVersion()
  173. {
  174. return $this->version;
  175. }
  176. /**
  177. * Set QR Code error correction level.
  178. *
  179. * @param mixed $error_correction Error Correction Level
  180. *
  181. * @return QrCode
  182. */
  183. public function setErrorCorrection($error_correction)
  184. {
  185. if (!is_numeric($error_correction)) {
  186. $level_constant = 'Endroid\QrCode\QrCode::LEVEL_'.strtoupper($error_correction);
  187. $error_correction = constant($level_constant);
  188. }
  189. if (in_array($error_correction, $this->error_corrections_available)) {
  190. $this->error_correction = $error_correction;
  191. }
  192. return $this;
  193. }
  194. /**
  195. * Return QR Code error correction level.
  196. *
  197. * @return int
  198. */
  199. public function getErrorCorrection()
  200. {
  201. return $this->error_correction;
  202. }
  203. /**
  204. * Set QR Code module size.
  205. *
  206. * @param int $module_size Module size
  207. *
  208. * @return QrCode
  209. */
  210. public function setModuleSize($module_size)
  211. {
  212. $this->module_size = $module_size;
  213. return $this;
  214. }
  215. /**
  216. * Return QR Code module size.
  217. *
  218. * @return int
  219. */
  220. public function getModuleSize()
  221. {
  222. return $this->module_size;
  223. }
  224. /**
  225. * Set image type for rendering.
  226. *
  227. * @param string $image_type Image type
  228. *
  229. * @return QrCode
  230. *
  231. * @throws ImageTypeInvalidException
  232. */
  233. public function setImageType($image_type)
  234. {
  235. if (!in_array($image_type, $this->image_types_available)) {
  236. throw new ImageTypeInvalidException('QRCode: image type '.$image_type.' is invalid.');
  237. }
  238. $this->image_type = $image_type;
  239. return $this;
  240. }
  241. /**
  242. * Return image type for rendering.
  243. *
  244. * @return string
  245. */
  246. public function getImageType()
  247. {
  248. return $this->image_type;
  249. }
  250. /**
  251. * Set image type for rendering via extension.
  252. *
  253. * @param string $extension Image extension
  254. *
  255. * @return QrCode
  256. */
  257. public function setExtension($extension)
  258. {
  259. if ($extension == 'jpg') {
  260. $this->setImageType('jpeg');
  261. } else {
  262. $this->setImageType($extension);
  263. }
  264. return $this;
  265. }
  266. /**
  267. * Set path to the images directory.
  268. *
  269. * @param string $image_path Image directory
  270. *
  271. * @return QrCode
  272. */
  273. public function setImagePath($image_path)
  274. {
  275. $this->image_path = $image_path;
  276. return $this;
  277. }
  278. /**
  279. * Return path to the images directory.
  280. *
  281. * @return string
  282. */
  283. public function getImagePath()
  284. {
  285. return $this->image_path;
  286. }
  287. /**
  288. * Set path to the data directory.
  289. *
  290. * @param string $path Data directory
  291. *
  292. * @return QrCode
  293. */
  294. public function setPath($path)
  295. {
  296. $this->path = $path;
  297. return $this;
  298. }
  299. /**
  300. * Return path to the data directory.
  301. *
  302. * @return string
  303. */
  304. public function getPath()
  305. {
  306. return $this->path;
  307. }
  308. /**
  309. * Set logo in QR Code.
  310. *
  311. * @param string $logo Logo Path
  312. *
  313. * @throws Exceptions\DataDoesntExistsException
  314. *
  315. * @return QrCode
  316. */
  317. public function setLogo($logo)
  318. {
  319. if (!file_exists($logo)) {
  320. throw new DataDoesntExistsException("$logo file does not exist");
  321. }
  322. $this->logo = $logo;
  323. return $this;
  324. }
  325. /**
  326. * Set logo size in QR Code(default 48).
  327. *
  328. * @param int $logo_size Logo Size
  329. *
  330. * @return QrCode
  331. */
  332. public function setLogoSize($logo_size)
  333. {
  334. $this->logo_size = $logo_size;
  335. return $this;
  336. }
  337. /**
  338. * Set text to hide in QR Code.
  339. *
  340. * @param string $text Text to hide
  341. *
  342. * @return QrCode
  343. */
  344. public function setText($text)
  345. {
  346. $this->text = $text;
  347. return $this;
  348. }
  349. /**
  350. * Return text that will be hid in QR Code.
  351. *
  352. * @return string
  353. */
  354. public function getText()
  355. {
  356. return $this->text;
  357. }
  358. /**
  359. * Set QR Code size (width).
  360. *
  361. * @param int $size Width of the QR Code
  362. *
  363. * @return QrCode
  364. */
  365. public function setSize($size)
  366. {
  367. $this->size = $size;
  368. return $this;
  369. }
  370. /**
  371. * Return QR Code size (width).
  372. *
  373. * @return int
  374. */
  375. public function getSize()
  376. {
  377. return $this->size;
  378. }
  379. /**
  380. * Set padding around the QR Code.
  381. *
  382. * @param int $padding Padding around QR Code
  383. *
  384. * @return QrCode
  385. */
  386. public function setPadding($padding)
  387. {
  388. $this->padding = $padding;
  389. return $this;
  390. }
  391. /**
  392. * Return padding around the QR Code.
  393. *
  394. * @return int
  395. */
  396. public function getPadding()
  397. {
  398. return $this->padding;
  399. }
  400. /**
  401. * Set draw required four-module wide margin.
  402. *
  403. * @param bool $draw_quiet_zone State of required four-module wide margin drawing
  404. *
  405. * @return QrCode
  406. */
  407. public function setDrawQuietZone($draw_quiet_zone)
  408. {
  409. $this->draw_quiet_zone = $draw_quiet_zone;
  410. return $this;
  411. }
  412. /**
  413. * Return draw required four-module wide margin.
  414. *
  415. * @return bool
  416. */
  417. public function getDrawQuietZone()
  418. {
  419. return $this->draw_quiet_zone;
  420. }
  421. /**
  422. * Set draw border around QR Code.
  423. *
  424. * @param bool $draw_border State of border drawing
  425. *
  426. * @return QrCode
  427. */
  428. public function setDrawBorder($draw_border)
  429. {
  430. $this->draw_border = $draw_border;
  431. return $this;
  432. }
  433. /**
  434. * Return draw border around QR Code.
  435. *
  436. * @return bool
  437. */
  438. public function getDrawBorder()
  439. {
  440. return $this->draw_border;
  441. }
  442. /**
  443. * Set QR Code label (text).
  444. *
  445. * @param int|string $label Label to print under QR code
  446. *
  447. * @return QrCode
  448. */
  449. public function setLabel($label)
  450. {
  451. $this->label = $label;
  452. return $this;
  453. }
  454. /**
  455. * Return QR Code label (text).
  456. *
  457. * @return string
  458. */
  459. public function getLabel()
  460. {
  461. return $this->label;
  462. }
  463. /**
  464. * Set QR Code label font size.
  465. *
  466. * @param int $label_font_size Font size of the QR code label
  467. *
  468. * @return QrCode
  469. */
  470. public function setLabelFontSize($label_font_size)
  471. {
  472. $this->label_font_size = $label_font_size;
  473. return $this;
  474. }
  475. /**
  476. * Return QR Code label font size.
  477. *
  478. * @return int
  479. */
  480. public function getLabelFontSize()
  481. {
  482. return $this->label_font_size;
  483. }
  484. /**
  485. * Set QR Code label font path.
  486. *
  487. * @param int $label_font_path Path to the QR Code label's TTF font file
  488. *
  489. * @return QrCode
  490. */
  491. public function setLabelFontPath($label_font_path)
  492. {
  493. $this->label_font_path = $label_font_path;
  494. return $this;
  495. }
  496. /**
  497. * Return path to the QR Code label's TTF font file.
  498. *
  499. * @return string
  500. */
  501. public function getLabelFontPath()
  502. {
  503. return $this->label_font_path;
  504. }
  505. /**
  506. * Set label horizontal alignment.
  507. *
  508. * @param int $label_halign Label horizontal alignment
  509. *
  510. * @return QrCode
  511. */
  512. public function setLabelHalign($label_halign)
  513. {
  514. $this->label_halign = $label_halign;
  515. return $this;
  516. }
  517. /**
  518. * Return label horizontal alignment.
  519. *
  520. * @return int
  521. */
  522. public function getLabelHalign()
  523. {
  524. return $this->label_halign;
  525. }
  526. /**
  527. * Set label vertical alignment.
  528. *
  529. * @param int $label_valign Label vertical alignment
  530. *
  531. * @return QrCode
  532. */
  533. public function setLabelValign($label_valign)
  534. {
  535. $this->label_valign = $label_valign;
  536. return $this;
  537. }
  538. /**
  539. * Return label vertical alignment.
  540. *
  541. * @return int
  542. */
  543. public function getLabelValign()
  544. {
  545. return $this->label_valign;
  546. }
  547. /**
  548. * Set foreground color of the QR Code.
  549. *
  550. * @param array $color_foreground RGB color
  551. *
  552. * @return QrCode
  553. */
  554. public function setForegroundColor($color_foreground)
  555. {
  556. if (!isset($color_foreground['a'])) {
  557. $color_foreground['a'] = 0;
  558. }
  559. $this->color_foreground = $color_foreground;
  560. return $this;
  561. }
  562. /**
  563. * Return foreground color of the QR Code.
  564. *
  565. * @return array
  566. */
  567. public function getForegroundColor()
  568. {
  569. return $this->color_foreground;
  570. }
  571. /**
  572. * Set background color of the QR Code.
  573. *
  574. * @param array $color_background RGB color
  575. *
  576. * @return QrCode
  577. */
  578. public function setBackgroundColor($color_background)
  579. {
  580. if (!isset($color_background['a'])) {
  581. $color_background['a'] = 0;
  582. }
  583. $this->color_background = $color_background;
  584. return $this;
  585. }
  586. /**
  587. * Return background color of the QR Code.
  588. *
  589. * @return array
  590. */
  591. public function getBackgroundColor()
  592. {
  593. return $this->color_background;
  594. }
  595. /**
  596. * Return the image resource.
  597. *
  598. * @return resource
  599. */
  600. public function getImage()
  601. {
  602. if (empty($this->image)) {
  603. $this->create();
  604. }
  605. return $this->image;
  606. }
  607. /**
  608. * Return the data URI.
  609. *
  610. * @return string
  611. */
  612. public function getDataUri()
  613. {
  614. if (empty($this->image)) {
  615. $this->create();
  616. }
  617. ob_start();
  618. call_user_func('image'.$this->image_type, $this->image);
  619. $contents = ob_get_clean();
  620. return 'data:image/'.$this->image_type.';base64,'.base64_encode($contents);
  621. }
  622. /**
  623. * Render the QR Code then save it to given file name.
  624. *
  625. * @param string $filename File name of the QR Code
  626. *
  627. * @return QrCode
  628. */
  629. public function save($filename)
  630. {
  631. $this->render($filename);
  632. return $this;
  633. }
  634. /**
  635. * Render the QR Code then save it to given file name or
  636. * output it to the browser when file name omitted.
  637. *
  638. * @param null|string $filename File name of the QR Code
  639. * @param null|string $format Format of the file (png, jpeg, jpg, gif, wbmp)
  640. *
  641. * @throws ImageFunctionUnknownException
  642. * @throws ImageFunctionFailedException
  643. *
  644. * @return QrCode
  645. */
  646. public function render($filename = null, $format = 'png')
  647. {
  648. $this->create();
  649. if ($format == 'jpg') {
  650. $format = 'jpeg';
  651. }
  652. if (!in_array($format, $this->image_types_available)) {
  653. $format = $this->image_type;
  654. }
  655. if (!function_exists('image'.$format)) {
  656. throw new ImageFunctionUnknownException('QRCode: function image'.$format.' does not exists.');
  657. }
  658. if ($filename === null) {
  659. $success = call_user_func('image'.$format, $this->image);
  660. } else {
  661. $success = call_user_func_array('image'.$format, [$this->image, $filename]);
  662. }
  663. if ($success === false) {
  664. throw new ImageFunctionFailedException('QRCode: function image'.$format.' failed.');
  665. }
  666. return $this;
  667. }
  668. /**
  669. * Returns the content type corresponding to the image type.
  670. *
  671. * @return string
  672. */
  673. public function getContentType()
  674. {
  675. $contentType = 'image/'.$this->image_type;
  676. return $contentType;
  677. }
  678. /**
  679. * Create QR Code and return its content.
  680. *
  681. * @param string|null $format Image type (gif, png, wbmp, jpeg)
  682. *
  683. * @throws ImageFunctionUnknownException
  684. * @throws ImageFunctionFailedException
  685. *
  686. * @return string
  687. */
  688. public function get($format = null)
  689. {
  690. $this->create();
  691. if ($format == 'jpg') {
  692. $format = 'jpeg';
  693. }
  694. if (!in_array($format, $this->image_types_available)) {
  695. $format = $this->image_type;
  696. }
  697. if (!function_exists('image'.$format)) {
  698. throw new ImageFunctionUnknownException('QRCode: function image'.$format.' does not exists.');
  699. }
  700. ob_start();
  701. $success = call_user_func('image'.$format, $this->image);
  702. if ($success === false) {
  703. throw new ImageFunctionFailedException('QRCode: function image'.$format.' failed.');
  704. }
  705. $content = ob_get_clean();
  706. return $content;
  707. }
  708. /**
  709. * Create the image.
  710. *
  711. * @throws Exceptions\DataDoesntExistsException
  712. * @throws Exceptions\VersionTooLargeException
  713. * @throws Exceptions\ImageSizeTooLargeException
  714. * @throws \OverflowException
  715. */
  716. public function create()
  717. {
  718. $image_path = $this->image_path;
  719. $path = $this->path;
  720. $version_ul = 40;
  721. $qrcode_data_string = $this->text;//Previously from $_GET["d"];
  722. $qrcode_error_correct = $this->error_correction;//Previously from $_GET["e"];
  723. $qrcode_module_size = $this->module_size;//Previously from $_GET["s"];
  724. $qrcode_version = $this->version;//Previously from $_GET["v"];
  725. $qrcode_image_type = $this->image_type;//Previously from $_GET["t"];
  726. $qrcode_structureappend_n = $this->structure_append_n;//Previously from $_GET["n"];
  727. $qrcode_structureappend_m = $this->structure_append_m;//Previously from $_GET["m"];
  728. $qrcode_structureappend_parity = $this->structure_append_parity;//Previously from $_GET["p"];
  729. $qrcode_structureappend_originaldata = $this->structure_append_original_data;//Previously from $_GET["o"];
  730. if ($qrcode_module_size > 0) {
  731. } else {
  732. if ($qrcode_image_type == 'jpeg') {
  733. $qrcode_module_size = 8;
  734. } else {
  735. $qrcode_module_size = 4;
  736. }
  737. }
  738. $data_length = strlen($qrcode_data_string);
  739. if ($data_length <= 0) {
  740. throw new DataDoesntExistsException('QRCode: data does not exist.');
  741. }
  742. $data_counter = 0;
  743. if ($qrcode_structureappend_n > 1
  744. && $qrcode_structureappend_n <= 16
  745. && $qrcode_structureappend_m > 0
  746. && $qrcode_structureappend_m <= 16) {
  747. $data_value[0] = 3;
  748. $data_bits[0] = 4;
  749. $data_value[1] = $qrcode_structureappend_m - 1;
  750. $data_bits[1] = 4;
  751. $data_value[2] = $qrcode_structureappend_n - 1;
  752. $data_bits[2] = 4;
  753. $originaldata_length = strlen($qrcode_structureappend_originaldata);
  754. if ($originaldata_length > 1) {
  755. $qrcode_structureappend_parity = 0;
  756. $i = 0;
  757. while ($i < $originaldata_length) {
  758. $qrcode_structureappend_parity = ($qrcode_structureappend_parity ^ ord(substr($qrcode_structureappend_originaldata, $i, 1)));
  759. ++$i;
  760. }
  761. }
  762. $data_value[3] = $qrcode_structureappend_parity;
  763. $data_bits[3] = 8;
  764. $data_counter = 4;
  765. }
  766. $data_bits[$data_counter] = 4;
  767. /* --- determine encode mode */
  768. if (preg_match('/[^0-9]/', $qrcode_data_string) != 0) {
  769. if (preg_match("/[^0-9A-Z \$\*\%\+\.\/\:\-]/", $qrcode_data_string) != 0) {
  770. /* --- 8bit byte mode */
  771. $codeword_num_plus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  772. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  773. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
  774. $data_value[$data_counter] = 4;
  775. ++$data_counter;
  776. $data_value[$data_counter] = $data_length;
  777. $data_bits[$data_counter] = 8; /* #version 1-9 */
  778. $codeword_num_counter_value = $data_counter;
  779. ++$data_counter;
  780. $i = 0;
  781. while ($i < $data_length) {
  782. $data_value[$data_counter] = ord(substr($qrcode_data_string, $i, 1));
  783. $data_bits[$data_counter] = 8;
  784. ++$data_counter;
  785. ++$i;
  786. }
  787. } else {
  788. /* ---- alphanumeric mode */
  789. $codeword_num_plus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  790. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  791. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4];
  792. $data_value[$data_counter] = 2;
  793. ++$data_counter;
  794. $data_value[$data_counter] = $data_length;
  795. $data_bits[$data_counter] = 9; /* #version 1-9 */
  796. $codeword_num_counter_value = $data_counter;
  797. $alphanumeric_character_hash = ['0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
  798. '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14,
  799. 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23,
  800. 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31,
  801. 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39,
  802. '+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44];
  803. $i = 0;
  804. ++$data_counter;
  805. while ($i < $data_length) {
  806. if (($i % 2) == 0) {
  807. $data_value[$data_counter] = $alphanumeric_character_hash[substr($qrcode_data_string, $i, 1)];
  808. $data_bits[$data_counter] = 6;
  809. } else {
  810. $data_value[$data_counter] = $data_value[$data_counter] * 45 + $alphanumeric_character_hash[substr($qrcode_data_string, $i, 1)];
  811. $data_bits[$data_counter] = 11;
  812. ++$data_counter;
  813. }
  814. ++$i;
  815. }
  816. }
  817. } else {
  818. /* ---- numeric mode */
  819. $codeword_num_plus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  820. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  821. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4];
  822. $data_value[$data_counter] = 1;
  823. ++$data_counter;
  824. $data_value[$data_counter] = $data_length;
  825. $data_bits[$data_counter] = 10; /* #version 1-9 */
  826. $codeword_num_counter_value = $data_counter;
  827. $i = 0;
  828. ++$data_counter;
  829. while ($i < $data_length) {
  830. if (($i % 3) == 0) {
  831. $data_value[$data_counter] = substr($qrcode_data_string, $i, 1);
  832. $data_bits[$data_counter] = 4;
  833. } else {
  834. $data_value[$data_counter] = $data_value[$data_counter] * 10 + substr($qrcode_data_string, $i, 1);
  835. if (($i % 3) == 1) {
  836. $data_bits[$data_counter] = 7;
  837. } else {
  838. $data_bits[$data_counter] = 10;
  839. ++$data_counter;
  840. }
  841. }
  842. ++$i;
  843. }
  844. }
  845. if (array_key_exists($data_counter, $data_bits) && $data_bits[$data_counter] > 0) {
  846. ++$data_counter;
  847. }
  848. $i = 0;
  849. $total_data_bits = 0;
  850. while ($i < $data_counter) {
  851. $total_data_bits += $data_bits[$i];
  852. ++$i;
  853. }
  854. $ecc_character_hash = [
  855. 'L' => '1',
  856. 'l' => '1',
  857. 'M' => '0',
  858. 'm' => '0',
  859. 'Q' => '3',
  860. 'q' => '3',
  861. 'H' => '2',
  862. 'h' => '2',
  863. ];
  864. if (!is_numeric($qrcode_error_correct)) {
  865. $ec = @$ecc_character_hash[$qrcode_error_correct];
  866. } else {
  867. $ec = $qrcode_error_correct;
  868. }
  869. if (!$ec) {
  870. $ec = 0;
  871. }
  872. $max_data_bits = 0;
  873. $max_data_bits_array = [
  874. 0, 128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728,
  875. 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352,
  876. 5712, 6256, 6880, 7312, 8000, 8496, 9024, 9544, 10136, 10984,
  877. 11640, 12328, 13048, 13800, 14496, 15312, 15936, 16816, 17728, 18672,
  878. 152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192,
  879. 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888,
  880. 7456, 8048, 8752, 9392, 10208, 10960, 11744, 12248, 13048, 13880,
  881. 14744, 15640, 16568, 17528, 18448, 19472, 20528, 21616, 22496, 23648,
  882. 72, 128, 208, 288, 368, 480, 528, 688, 800, 976,
  883. 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080,
  884. 3248, 3536, 3712, 4112, 4304, 4768, 5024, 5288, 5608, 5960,
  885. 6344, 6760, 7208, 7688, 7888, 8432, 8768, 9136, 9776, 10208,
  886. 104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232,
  887. 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880,
  888. 4096, 4544, 4912, 5312, 5744, 6032, 6464, 6968, 7288, 7880,
  889. 8264, 8920, 9368, 9848, 10288, 10832, 11408, 12016, 12656, 13328
  890. ];
  891. if (!is_numeric($qrcode_version)) {
  892. $qrcode_version = 0;
  893. }
  894. if (!$qrcode_version) {
  895. /* #--- auto version select */
  896. $i = 1 + 40 * $ec;
  897. $j = $i + 39;
  898. $qrcode_version = 1;
  899. while ($i <= $j) {
  900. if (($max_data_bits_array[$i]) >= $total_data_bits + $codeword_num_plus[$qrcode_version]) {
  901. $max_data_bits = $max_data_bits_array[$i];
  902. break;
  903. }
  904. ++$i;
  905. ++$qrcode_version;
  906. }
  907. } else {
  908. $max_data_bits = $max_data_bits_array[$qrcode_version + 40 * $ec];
  909. }
  910. if ($qrcode_version > $version_ul) {
  911. throw new VersionTooLargeException('QRCode : version too large');
  912. }
  913. $total_data_bits += $codeword_num_plus[$qrcode_version];
  914. $data_bits[$codeword_num_counter_value] += $codeword_num_plus[$qrcode_version];
  915. $max_codewords_array = [0, 26, 44, 70, 100, 134, 172, 196, 242,
  916. 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156,
  917. 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465,
  918. 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706];
  919. $max_codewords = $max_codewords_array[$qrcode_version];
  920. $max_modules_1side = 17 + ($qrcode_version << 2);
  921. $matrix_remain_bit = [0, 0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3,
  922. 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0];
  923. /* ---- read version ECC data file */
  924. $byte_num = $matrix_remain_bit[$qrcode_version] + ($max_codewords << 3);
  925. $filename = $path.'/qrv'.$qrcode_version.'_'.$ec.'.dat';
  926. $fp1 = fopen($filename, 'rb');
  927. $matx = fread($fp1, $byte_num);
  928. $maty = fread($fp1, $byte_num);
  929. $masks = fread($fp1, $byte_num);
  930. $fi_x = fread($fp1, 15);
  931. $fi_y = fread($fp1, 15);
  932. $rs_ecc_codewords = ord(fread($fp1, 1));
  933. $rso = fread($fp1, 128);
  934. fclose($fp1);
  935. $matrix_x_array = unpack('C*', $matx);
  936. $matrix_y_array = unpack('C*', $maty);
  937. $mask_array = unpack('C*', $masks);
  938. $rs_block_order = unpack('C*', $rso);
  939. $format_information_x2 = unpack('C*', $fi_x);
  940. $format_information_y2 = unpack('C*', $fi_y);
  941. $format_information_x1 = [0, 1, 2, 3, 4, 5, 7, 8, 8, 8, 8, 8, 8, 8, 8];
  942. $format_information_y1 = [8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 4, 3, 2, 1, 0];
  943. $max_data_codewords = ($max_data_bits >> 3);
  944. $filename = $path.'/rsc'.$rs_ecc_codewords.'.dat';
  945. $fp0 = fopen($filename, 'rb');
  946. $i = 0;
  947. $rs_cal_table_array = [];
  948. while ($i < 256) {
  949. $rs_cal_table_array[$i] = fread($fp0, $rs_ecc_codewords);
  950. ++$i;
  951. }
  952. fclose($fp0);
  953. /* --- set terminator */
  954. if ($total_data_bits <= $max_data_bits - 4) {
  955. $data_value[$data_counter] = 0;
  956. $data_bits[$data_counter] = 4;
  957. } else {
  958. if ($total_data_bits < $max_data_bits) {
  959. $data_value[$data_counter] = 0;
  960. $data_bits[$data_counter] = $max_data_bits - $total_data_bits;
  961. } else {
  962. if ($total_data_bits > $max_data_bits) {
  963. throw new \OverflowException('QRCode: overflow error');
  964. }
  965. }
  966. }
  967. /* ----divide data by 8bit */
  968. $i = 0;
  969. $codewords_counter = 0;
  970. $codewords[0] = 0;
  971. $remaining_bits = 8;
  972. while ($i <= $data_counter) {
  973. $buffer = @$data_value[$i];
  974. $buffer_bits = @$data_bits[$i];
  975. $flag = 1;
  976. while ($flag) {
  977. if ($remaining_bits > $buffer_bits) {
  978. $codewords[$codewords_counter] = ((@$codewords[$codewords_counter] << $buffer_bits) | $buffer);
  979. $remaining_bits -= $buffer_bits;
  980. $flag = 0;
  981. } else {
  982. $buffer_bits -= $remaining_bits;
  983. $codewords[$codewords_counter] = (($codewords[$codewords_counter] << $remaining_bits) | ($buffer >> $buffer_bits));
  984. if ($buffer_bits == 0) {
  985. $flag = 0;
  986. } else {
  987. $buffer = ($buffer & ((1 << $buffer_bits) - 1));
  988. $flag = 1;
  989. }
  990. ++$codewords_counter;
  991. if ($codewords_counter < $max_data_codewords - 1) {
  992. $codewords[$codewords_counter] = 0;
  993. }
  994. $remaining_bits = 8;
  995. }
  996. }
  997. ++$i;
  998. }
  999. if ($remaining_bits != 8) {
  1000. $codewords[$codewords_counter] = $codewords[$codewords_counter] << $remaining_bits;
  1001. } else {
  1002. --$codewords_counter;
  1003. }
  1004. /* ---- set padding character */
  1005. if ($codewords_counter < $max_data_codewords - 1) {
  1006. $flag = 1;
  1007. while ($codewords_counter < $max_data_codewords - 1) {
  1008. ++$codewords_counter;
  1009. if ($flag == 1) {
  1010. $codewords[$codewords_counter] = 236;
  1011. } else {
  1012. $codewords[$codewords_counter] = 17;
  1013. }
  1014. $flag = $flag * (-1);
  1015. }
  1016. }
  1017. /* ---- RS-ECC prepare */
  1018. $i = 0;
  1019. $j = 0;
  1020. $rs_block_number = 0;
  1021. $rs_temp[0] = '';
  1022. while ($i < $max_data_codewords) {
  1023. $rs_temp[$rs_block_number] .= chr($codewords[$i]);
  1024. ++$j;
  1025. if ($j >= $rs_block_order[$rs_block_number + 1] - $rs_ecc_codewords) {
  1026. $j = 0;
  1027. ++$rs_block_number;
  1028. $rs_temp[$rs_block_number] = '';
  1029. }
  1030. ++$i;
  1031. }
  1032. /*
  1033. #
  1034. # RS-ECC main
  1035. #
  1036. */
  1037. $rs_block_number = 0;
  1038. $rs_block_order_num = count($rs_block_order);
  1039. while ($rs_block_number < $rs_block_order_num) {
  1040. $rs_codewords = $rs_block_order[$rs_block_number + 1];
  1041. $rs_data_codewords = $rs_codewords - $rs_ecc_codewords;
  1042. $rstemp = $rs_temp[$rs_block_number].str_repeat(chr(0), $rs_ecc_codewords);
  1043. $padding_data = str_repeat(chr(0), $rs_data_codewords);
  1044. $j = $rs_data_codewords;
  1045. while ($j > 0) {
  1046. $first = ord(substr($rstemp, 0, 1));
  1047. if ($first) {
  1048. $left_chr = substr($rstemp, 1);
  1049. $cal = $rs_cal_table_array[$first].$padding_data;
  1050. $rstemp = $left_chr ^ $cal;
  1051. } else {
  1052. $rstemp = substr($rstemp, 1);
  1053. }
  1054. --$j;
  1055. }
  1056. $codewords = array_merge($codewords, unpack('C*', $rstemp));
  1057. ++$rs_block_number;
  1058. }
  1059. /* ---- flash matrix */
  1060. $matrix_content = [];
  1061. $i = 0;
  1062. while ($i < $max_modules_1side) {
  1063. $j = 0;
  1064. while ($j < $max_modules_1side) {
  1065. $matrix_content[$j][$i] = 0;
  1066. ++$j;
  1067. }
  1068. ++$i;
  1069. }
  1070. /* --- attach data */
  1071. $i = 0;
  1072. while ($i < $max_codewords) {
  1073. $codeword_i = $codewords[$i];
  1074. $j = 8;
  1075. while ($j >= 1) {
  1076. $codeword_bits_number = ($i << 3) + $j;
  1077. $matrix_content[ $matrix_x_array[$codeword_bits_number] ][ $matrix_y_array[$codeword_bits_number] ] = ((255 * ($codeword_i & 1)) ^ $mask_array[$codeword_bits_number]);
  1078. $codeword_i = $codeword_i >> 1;
  1079. --$j;
  1080. }
  1081. ++$i;
  1082. }
  1083. $matrix_remain = $matrix_remain_bit[$qrcode_version];
  1084. while ($matrix_remain) {
  1085. $remain_bit_temp = $matrix_remain + ($max_codewords << 3);
  1086. $matrix_content[ $matrix_x_array[$remain_bit_temp] ][ $matrix_y_array[$remain_bit_temp] ] = (255 ^ $mask_array[$remain_bit_temp]);
  1087. --$matrix_remain;
  1088. }
  1089. #--- mask select
  1090. $min_demerit_score = 0;
  1091. $hor_master = '';
  1092. $ver_master = '';
  1093. $k = 0;
  1094. while ($k < $max_modules_1side) {
  1095. $l = 0;
  1096. while ($l < $max_modules_1side) {
  1097. $hor_master = $hor_master.chr($matrix_content[$l][$k]);
  1098. $ver_master = $ver_master.chr($matrix_content[$k][$l]);
  1099. ++$l;
  1100. }
  1101. ++$k;
  1102. }
  1103. $i = 0;
  1104. $all_matrix = $max_modules_1side * $max_modules_1side;
  1105. $mask_number = 0;
  1106. while ($i < 8) {
  1107. $demerit_n1 = 0;
  1108. $ptn_temp = [];
  1109. $bit = 1 << $i;
  1110. $bit_r = (~$bit) & 255;
  1111. $bit_mask = str_repeat(chr($bit), $all_matrix);
  1112. $hor = $hor_master & $bit_mask;
  1113. $ver = $ver_master & $bit_mask;
  1114. $ver_shift1 = $ver.str_repeat(chr(170), $max_modules_1side);
  1115. $ver_shift2 = str_repeat(chr(170), $max_modules_1side).$ver;
  1116. $ver_shift1_0 = $ver.str_repeat(chr(0), $max_modules_1side);
  1117. $ver_shift2_0 = str_repeat(chr(0), $max_modules_1side).$ver;
  1118. $ver_or = chunk_split(~($ver_shift1 | $ver_shift2), $max_modules_1side, chr(170));
  1119. $ver_and = chunk_split(~($ver_shift1_0 & $ver_shift2_0), $max_modules_1side, chr(170));
  1120. $hor = chunk_split(~$hor, $max_modules_1side, chr(170));
  1121. $ver = chunk_split(~$ver, $max_modules_1side, chr(170));
  1122. $hor = $hor.chr(170).$ver;
  1123. $n1_search = '/'.str_repeat(chr(255), 5).'+|'.str_repeat(chr($bit_r), 5).'+/';
  1124. $n3_search = chr($bit_r).chr(255).chr($bit_r).chr($bit_r).chr($bit_r).chr(255).chr($bit_r);
  1125. $demerit_n3 = substr_count($hor, $n3_search) * 40;
  1126. $demerit_n4 = floor(abs(((100 * (substr_count($ver, chr($bit_r)) / ($byte_num))) - 50) / 5)) * 10;
  1127. $n2_search1 = '/'.chr($bit_r).chr($bit_r).'+/';
  1128. $n2_search2 = '/'.chr(255).chr(255).'+/';
  1129. $demerit_n2 = 0;
  1130. preg_match_all($n2_search1, $ver_and, $ptn_temp);
  1131. foreach ($ptn_temp[0] as $str_temp) {
  1132. $demerit_n2 += (strlen($str_temp) - 1);
  1133. }
  1134. $ptn_temp = [];
  1135. preg_match_all($n2_search2, $ver_or, $ptn_temp);
  1136. foreach ($ptn_temp[0] as $str_temp) {
  1137. $demerit_n2 += (strlen($str_temp) - 1);
  1138. }
  1139. $demerit_n2 *= 3;
  1140. $ptn_temp = [];
  1141. preg_match_all($n1_search, $hor, $ptn_temp);
  1142. foreach ($ptn_temp[0] as $str_temp) {
  1143. $demerit_n1 += (strlen($str_temp) - 2);
  1144. }
  1145. $demerit_score = $demerit_n1 + $demerit_n2 + $demerit_n3 + $demerit_n4;
  1146. if ($demerit_score <= $min_demerit_score || $i == 0) {
  1147. $mask_number = $i;
  1148. $min_demerit_score = $demerit_score;
  1149. }
  1150. ++$i;
  1151. }
  1152. $mask_content = 1 << $mask_number;
  1153. # --- format information
  1154. $format_information_value = (($ec << 3) | $mask_number);
  1155. $format_information_array = ['101010000010010', '101000100100101',
  1156. '101111001111100', '101101101001011', '100010111111001', '100000011001110',
  1157. '100111110010111', '100101010100000', '111011111000100', '111001011110011',
  1158. '111110110101010', '111100010011101', '110011000101111', '110001100011000',
  1159. '110110001000001', '110100101110110', '001011010001001', '001001110111110',
  1160. '001110011100111', '001100111010000', '000011101100010', '000001001010101',
  1161. '000110100001100', '000100000111011', '011010101011111', '011000001101000',
  1162. '011111100110001', '011101000000110', '010010010110100', '010000110000011',
  1163. '010111011011010', '010101111101101'];
  1164. $i = 0;
  1165. while ($i < 15) {
  1166. $content = substr($format_information_array[$format_information_value], $i, 1);
  1167. $matrix_content[$format_information_x1[$i]][$format_information_y1[$i]] = $content * 255;
  1168. $matrix_content[$format_information_x2[$i + 1]][$format_information_y2[$i + 1]] = $content * 255;
  1169. ++$i;
  1170. }
  1171. $mib = $max_modules_1side;
  1172. if ($this->draw_quiet_zone) {
  1173. $mib += 8;
  1174. }
  1175. if ($this->size == 0) {
  1176. $this->size = $mib * $qrcode_module_size;
  1177. if ($this->size > 1480) {
  1178. throw new ImageSizeTooLargeException('QRCode: image size too large');
  1179. }
  1180. }
  1181. $image_width = $this->size + $this->padding * 2;
  1182. $image_height = $this->size + $this->padding * 2;
  1183. if (!empty($this->label)) {
  1184. if (!function_exists('imagettfbbox')) {
  1185. throw new FreeTypeLibraryMissingException('QRCode: missing function "imagettfbbox". Did you install the FreeType library?');
  1186. }
  1187. $font_box = imagettfbbox($this->label_font_size, 0, $this->label_font_path, $this->label);
  1188. $label_width = (int) $font_box[2] - (int) $font_box[0];
  1189. $label_height = (int) $font_box[0] - (int) $font_box[7];
  1190. if ($this->label_valign == self::LABEL_VALIGN_MIDDLE) {
  1191. $image_height += $label_height + $this->padding;
  1192. } else {
  1193. $image_height += $label_height;
  1194. }
  1195. }
  1196. $output_image = imagecreate($image_width, $image_height);
  1197. imagecolorallocate($output_image, 255, 255, 255);
  1198. $image_path = $image_path.'/qrv'.$qrcode_version.'.png';
  1199. $base_image = imagecreatefrompng($image_path);
  1200. $code_size = $this->size;
  1201. $module_size = function ($size = 1) use ($code_size, $base_image) {
  1202. return round($code_size / imagesx($base_image) * $size);
  1203. };
  1204. $col[1] = imagecolorallocate($base_image, 0, 0, 0);
  1205. $col[0] = imagecolorallocate($base_image, 255, 255, 255);
  1206. $i = 4;
  1207. $mxe = 4 + $max_modules_1side;
  1208. $ii = 0;
  1209. while ($i < $mxe) {
  1210. $j = 4;
  1211. $jj = 0;
  1212. while ($j < $mxe) {
  1213. if ($matrix_content[$ii][$jj] & $mask_content) {
  1214. imagesetpixel($base_image, $i, $j, $col[1]);
  1215. }
  1216. ++$j;
  1217. ++$jj;
  1218. }
  1219. ++$i;
  1220. ++$ii;
  1221. }
  1222. if ($this->draw_quiet_zone == true) {
  1223. imagecopyresampled($output_image, $base_image, $this->padding, $this->padding, 0, 0, $this->size, $this->size, $mib, $mib);
  1224. } else {
  1225. imagecopyresampled($output_image, $base_image, $this->padding, $this->padding, 4, 4, $this->size, $this->size, $mib, $mib);
  1226. }
  1227. if ($this->draw_border == true) {
  1228. $border_width = $this->padding;
  1229. $border_height = $this->size + $this->padding - 1;
  1230. $border_color = imagecolorallocate($output_image, 0, 0, 0);
  1231. imagerectangle($output_image, $border_width, $border_width, $border_height, $border_height, $border_color);
  1232. }
  1233. if (!empty($this->label)) {
  1234. // Label horizontal alignment
  1235. switch ($this->label_halign) {
  1236. case self::LABEL_HALIGN_LEFT:
  1237. $font_x = 0;
  1238. break;
  1239. case self::LABEL_HALIGN_LEFT_BORDER:
  1240. $font_x = $this->padding;
  1241. break;
  1242. case self::LABEL_HALIGN_LEFT_CODE:
  1243. if ($this->draw_quiet_zone == true) {
  1244. $font_x = $this->padding + $module_size(4);
  1245. } else {
  1246. $font_x = $this->padding;
  1247. }
  1248. break;
  1249. case self::LABEL_HALIGN_RIGHT:
  1250. $font_x = $this->size + ($this->padding * 2) - $label_width;
  1251. break;
  1252. case self::LABEL_HALIGN_RIGHT_BORDER:
  1253. $font_x = $this->size + $this->padding - $label_width;
  1254. break;
  1255. case self::LABEL_HALIGN_RIGHT_CODE:
  1256. if ($this->draw_quiet_zone == true) {
  1257. $font_x = $this->size + $this->padding - $label_width - $module_size(4);
  1258. } else {
  1259. $font_x = $this->size + $this->padding - $label_width;
  1260. }
  1261. break;
  1262. default:
  1263. $font_x = floor($image_width - $label_width) / 2;
  1264. }
  1265. // Label vertical alignment
  1266. switch ($this->label_valign) {
  1267. case self::LABEL_VALIGN_TOP_NO_BORDER:
  1268. $font_y = $image_height - $this->padding - 1;
  1269. break;
  1270. case self::LABEL_VALIGN_BOTTOM:
  1271. $font_y = $image_height;
  1272. break;
  1273. default:
  1274. $font_y = $image_height - $this->padding;
  1275. }
  1276. $label_bg_x1 = $font_x - $module_size(2);
  1277. $label_bg_y1 = $font_y - $label_height;
  1278. $label_bg_x2 = $font_x + $label_width + $module_size(2);
  1279. $label_bg_y2 = $font_y;
  1280. $color = imagecolorallocate($output_image, 0, 0, 0);
  1281. $label_bg_color = imagecolorallocate($output_image, 255, 255, 255);
  1282. imagefilledrectangle($output_image, $label_bg_x1, $label_bg_y1, $label_bg_x2, $label_bg_y2, $label_bg_color);
  1283. imagettftext($output_image, $this->label_font_size, 0, $font_x, $font_y, $color, $this->label_font_path, $this->label);
  1284. }
  1285. $imagecolorset_function = new ReflectionFunction('imagecolorset');
  1286. $allow_alpha = $imagecolorset_function->getNumberOfParameters() == 6;
  1287. if ($this->color_background != null) {
  1288. $index = imagecolorclosest($output_image, 255, 255, 255);
  1289. if ($allow_alpha) {
  1290. imagecolorset($output_image, $index, $this->color_background['r'], $this->color_background['g'], $this->color_background['b'], $this->color_background['a']);
  1291. } else {
  1292. imagecolorset($output_image, $index, $this->color_background['r'], $this->color_background['g'], $this->color_background['b']);
  1293. }
  1294. }
  1295. if ($this->color_foreground != null) {
  1296. $index = imagecolorclosest($output_image, 0, 0, 0);
  1297. if ($allow_alpha) {
  1298. imagecolorset($output_image, $index, $this->color_foreground['r'], $this->color_foreground['g'], $this->color_foreground['b'], $this->color_foreground['a']);
  1299. } else {
  1300. imagecolorset($output_image, $index, $this->color_foreground['r'], $this->color_foreground['g'], $this->color_foreground['b']);
  1301. }
  1302. }
  1303. if (!empty($this->logo)) {
  1304. $output_image_org = $output_image;
  1305. $output_image = imagecreatetruecolor($image_width, $image_height);
  1306. imagecopy($output_image, $output_image_org, 0, 0, 0, 0, $image_width, $image_height);
  1307. $image_info = getimagesize($this->logo);
  1308. if ($image_info !== false) {
  1309. $image_type = strtolower(substr(image_type_to_extension($image_info [2]), 1));
  1310. $logo_image = call_user_func('imagecreatefrom'.$image_type, $this->logo);
  1311. } else {
  1312. $logo_image = call_user_func('imagecreatefrom'.$this->image_type, $this->logo);
  1313. }
  1314. if (!$logo_image) {
  1315. throw new ImageFunctionFailedException('imagecreatefrom'.$this->image_type.' '.$this->logo.' failed');
  1316. }
  1317. $src_w = imagesx($logo_image);
  1318. $src_h = imagesy($logo_image);
  1319. $dst_x = ($image_width - $this->logo_size) / 2;
  1320. $dst_y = ($this->size + $this->padding * 2 - $this->logo_size) / 2;
  1321. $successful = imagecopyresampled($output_image, $logo_image, $dst_x, $dst_y, 0, 0, $this->logo_size, $this->logo_size, $src_w, $src_h);
  1322. if (!$successful) {
  1323. throw new ImageFunctionFailedException('add logo [image'.$this->format.'] failed.');
  1324. }
  1325. imagedestroy($logo_image);
  1326. }
  1327. $this->image = $output_image;
  1328. }
  1329. }