3
3
* Class QRImageWithText
4
4
*
5
5
* example for additional text
6
- * @link https://github.com/chillerlan/php-qrcode/issues/35
6
+ *
7
+ * @link https://github.com/chillerlan/php-qrcode/issues/35
7
8
*
8
9
* @filesource QRImageWithText.php
9
10
* @created 22.06.2019
10
11
* @package chillerlan\QRCodeExamples
11
12
* @author smiley <[email protected] >
12
13
* @copyright 2019 smiley
13
14
* @license MIT
15
+ *
16
+ * @noinspection PhpComposerExtensionStubsInspection
14
17
*/
15
18
16
19
namespace chillerlan \QRCodeExamples ;
17
20
18
21
use chillerlan \QRCode \Output \QRImage ;
22
+ use function base64_encode , imagechar , imagecolorallocate , imagecolortransparent , imagecopymerge , imagecreatetruecolor ,
23
+ imagedestroy , imagefilledrectangle , imagefontwidth , in_array , round , str_split , strlen ;
19
24
20
25
class QRImageWithText extends QRImage{
21
26
@@ -26,14 +31,14 @@ class QRImageWithText extends QRImage{
26
31
* @return string
27
32
*/
28
33
public function dump (string $ file = null , string $ text = null ):string {
29
- $ this ->image = \ imagecreatetruecolor ($ this ->length , $ this ->length );
30
- $ background = \ imagecolorallocate ($ this ->image , ...$ this ->options ->imageTransparencyBG );
34
+ $ this ->image = imagecreatetruecolor ($ this ->length , $ this ->length );
35
+ $ background = imagecolorallocate ($ this ->image , ...$ this ->options ->imageTransparencyBG );
31
36
32
- if ((bool )$ this ->options ->imageTransparent && \ in_array ($ this ->options ->outputType , $ this ::TRANSPARENCY_TYPES , true )){
33
- \ imagecolortransparent ($ this ->image , $ background );
37
+ if ((bool )$ this ->options ->imageTransparent && in_array ($ this ->options ->outputType , $ this ::TRANSPARENCY_TYPES , true )){
38
+ imagecolortransparent ($ this ->image , $ background );
34
39
}
35
40
36
- \ imagefilledrectangle ($ this ->image , 0 , 0 , $ this ->length , $ this ->length , $ background );
41
+ imagefilledrectangle ($ this ->image , 0 , 0 , $ this ->length , $ this ->length , $ background );
37
42
38
43
foreach ($ this ->matrix ->matrix () as $ y => $ row ){
39
44
foreach ($ row as $ x => $ M_TYPE ){
@@ -49,7 +54,7 @@ public function dump(string $file = null, string $text = null):string{
49
54
$ imageData = $ this ->dumpImage ($ file );
50
55
51
56
if ((bool )$ this ->options ->imageBase64 ){
52
- $ imageData = 'data:image/ ' .$ this ->options ->outputType .';base64, ' .\ base64_encode ($ imageData );
57
+ $ imageData = 'data:image/ ' .$ this ->options ->outputType .';base64, ' .base64_encode ($ imageData );
53
58
}
54
59
55
60
return $ imageData ;
@@ -63,36 +68,36 @@ protected function addText(string $text):void{
63
68
$ qrcode = $ this ->image ;
64
69
65
70
// options things
66
- $ textSize = 3 ; // see imagefontheight() and imagefontwidth()
71
+ $ textSize = 3 ; // see imagefontheight() and imagefontwidth()
67
72
$ textBG = [200 , 200 , 200 ];
68
73
$ textColor = [50 , 50 , 50 ];
69
74
70
75
$ bgWidth = $ this ->length ;
71
76
$ bgHeight = $ bgWidth + 20 ; // 20px extra space
72
77
73
78
// create a new image with additional space
74
- $ this ->image = \ imagecreatetruecolor ($ bgWidth , $ bgHeight );
75
- $ background = \ imagecolorallocate ($ this ->image , ...$ textBG );
79
+ $ this ->image = imagecreatetruecolor ($ bgWidth , $ bgHeight );
80
+ $ background = imagecolorallocate ($ this ->image , ...$ textBG );
76
81
77
82
// allow transparency
78
- if ((bool )$ this ->options ->imageTransparent && \ in_array ($ this ->options ->outputType , $ this ::TRANSPARENCY_TYPES , true )){
79
- \ imagecolortransparent ($ this ->image , $ background );
83
+ if ((bool )$ this ->options ->imageTransparent && in_array ($ this ->options ->outputType , $ this ::TRANSPARENCY_TYPES , true )){
84
+ imagecolortransparent ($ this ->image , $ background );
80
85
}
81
86
82
87
// fill the background
83
- \ imagefilledrectangle ($ this ->image , 0 , 0 , $ bgWidth , $ bgHeight , $ background );
88
+ imagefilledrectangle ($ this ->image , 0 , 0 , $ bgWidth , $ bgHeight , $ background );
84
89
85
90
// copy over the qrcode
86
- \ imagecopymerge ($ this ->image , $ qrcode , 0 , 0 , 0 , 0 , $ this ->length , $ this ->length , 100 );
87
- \ imagedestroy ($ qrcode );
91
+ imagecopymerge ($ this ->image , $ qrcode , 0 , 0 , 0 , 0 , $ this ->length , $ this ->length , 100 );
92
+ imagedestroy ($ qrcode );
88
93
89
- $ fontColor = \ imagecolorallocate ($ this ->image , ...$ textColor );
90
- $ w = \ imagefontwidth ($ textSize );
91
- $ x = \ round (($ bgWidth - \ strlen ($ text ) * $ w ) / 2 );
94
+ $ fontColor = imagecolorallocate ($ this ->image , ...$ textColor );
95
+ $ w = imagefontwidth ($ textSize );
96
+ $ x = round (($ bgWidth - strlen ($ text ) * $ w ) / 2 );
92
97
93
98
// loop through the string and draw the letters
94
- foreach (\ str_split ($ text ) as $ i => $ chr ){
95
- \ imagechar ($ this ->image , $ textSize , $ i * $ w + $ x , $ this ->length , $ chr , $ fontColor );
99
+ foreach (str_split ($ text ) as $ i => $ chr ){
100
+ imagechar ($ this ->image , $ textSize , $ i * $ w + $ x , $ this ->length , $ chr , $ fontColor );
96
101
}
97
102
}
98
103
0 commit comments