@@ -56,11 +56,86 @@ class Generator
5656
5757 protected $ gradient ;
5858
59+ /**
60+ * Holds an image string that will be merged with the QrCode.
61+ *
62+ * @var null|string
63+ */
64+ protected $ imageMerge = null ;
65+
66+ /**
67+ * The percentage that a merged image should take over the source image.
68+ *
69+ * @var float
70+ */
71+ protected $ imagePercentage = .2 ;
72+
73+ /**
74+ * Creates a new datatype object and then generates a QrCode.
75+ *
76+ * @param $method
77+ * @param $arguments
78+ */
79+ public function __call ($ method , $ arguments )
80+ {
81+ $ dataType = $ this ->createClass ($ method );
82+ $ dataType ->create ($ arguments );
83+
84+ return $ this ->generate (strval ($ dataType ));
85+ }
86+
5987 public function generate (string $ text , string $ filename = null )
6088 {
61- $ render = $ this ->getRenderer ();
89+ $ qrCode = $ this ->getWriter ($ this ->getRenderer ())->writeString ($ text , $ this ->encoding , $ this ->errorCorrection );
90+
91+ if ($ this ->imageMerge !== null && $ this ->format === 'png ' ) {
92+ $ merger = new ImageMerge (new Image ($ qrCode ), new Image ($ this ->imageMerge ));
93+ $ qrCode = $ merger ->merge ($ this ->imagePercentage );
94+ }
95+
96+ if ($ filename ) {
97+ file_put_contents ($ filename , $ qrCode );
98+ return ;
99+ }
100+
101+ return $ qrCode ;
102+ }
103+
104+ /**
105+ * Merges an image with the center of the QrCode.
106+ *
107+ * @param $filepath string The filepath to an image
108+ * @param $percentage float The amount that the merged image should be placed over the qrcode.
109+ * @param $absolute boolean Whether to use an absolute filepath or not.
110+ *
111+ * @return $this
112+ */
113+ public function merge ($ filepath , $ percentage = .2 , $ absolute = false )
114+ {
115+ if (function_exists ('base_path ' ) && !$ absolute ) {
116+ $ filepath = base_path ().$ filepath ;
117+ }
118+
119+ $ this ->imageMerge = file_get_contents ($ filepath );
120+ $ this ->imagePercentage = $ percentage ;
62121
63- return $ this ->getWriter ($ render )->writeString ($ text , $ this ->encoding , $ this ->errorCorrection );
122+ return $ this ;
123+ }
124+
125+ /**
126+ * Merges an image string with the center of the QrCode, does not check for correct format.
127+ *
128+ * @param $content string The string contents of an image.
129+ * @param $percentage float The amount that the merged image should be placed over the qrcode.
130+ *
131+ * @return $this
132+ */
133+ public function mergeString ($ content , $ percentage = .2 )
134+ {
135+ $ this ->imageMerge = $ content ;
136+ $ this ->imagePercentage = $ percentage ;
137+
138+ return $ this ;
64139 }
65140
66141 public function size (int $ pixels ): self
@@ -240,4 +315,38 @@ protected function createColor(int $red, int $green, int $blue, ?int $alpha = nu
240315
241316 return new Alpha ($ alpha , new Rgb ($ red , $ green , $ blue ));
242317 }
318+
319+ /**
320+ * Creates a new DataType class dynamically.
321+ *
322+ * @param string $method
323+ *
324+ * @return SimpleSoftwareIO\QrCode\DataTypes\DataTypeInterface
325+ */
326+ protected function createClass ($ method )
327+ {
328+ $ class = $ this ->formatClass ($ method );
329+
330+ if (!class_exists ($ class )) {
331+ throw new \BadMethodCallException ();
332+ }
333+
334+ return new $ class ();
335+ }
336+
337+ /**
338+ * Formats the method name correctly.
339+ *
340+ * @param $method
341+ *
342+ * @return string
343+ */
344+ protected function formatClass ($ method )
345+ {
346+ $ method = ucfirst ($ method );
347+
348+ $ class = "SimpleSoftwareIO\QrCode\DataTypes \\" .$ method ;
349+
350+ return $ class ;
351+ }
243352}
0 commit comments