@@ -36,13 +36,14 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
3636 $ matrix = $ this ->getMatrix ($ qrCode );
3737
3838 $ baseBlockSize = RoundBlockSizeMode::None === $ qrCode ->getRoundBlockSizeMode () ? 10 : intval ($ matrix ->getBlockSize ());
39- $ baseImage = imagecreatetruecolor ($ matrix ->getBlockCount () * $ baseBlockSize , $ matrix ->getBlockCount () * $ baseBlockSize );
39+ /** @var int<1, max> $baseImageSize */
40+ $ baseImageSize = $ matrix ->getBlockCount () * $ baseBlockSize ;
41+ $ baseImage = imagecreatetruecolor ($ baseImageSize , $ baseImageSize );
4042
4143 if (!$ baseImage ) {
4244 throw new \Exception ('Unable to generate image: please check if the GD extension is enabled and configured correctly ' );
4345 }
4446
45- /** @var int $foregroundColor */
4647 $ foregroundColor = imagecolorallocatealpha (
4748 $ baseImage ,
4849 $ qrCode ->getForegroundColor ()->getRed (),
@@ -51,9 +52,16 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
5152 $ qrCode ->getForegroundColor ()->getAlpha ()
5253 );
5354
54- /** @var int $transparentColor */
55+ if (false === $ foregroundColor ) {
56+ throw new \Exception ('Unable to generate image: please check if the GD extension is enabled and configured correctly ' );
57+ }
58+
5559 $ transparentColor = imagecolorallocatealpha ($ baseImage , 255 , 255 , 255 , 127 );
5660
61+ if (false === $ transparentColor ) {
62+ throw new \Exception ('Unable to generate image: please check if the GD extension is enabled and configured correctly ' );
63+ }
64+
5765 imagefill ($ baseImage , 0 , 0 , $ transparentColor );
5866
5967 for ($ rowIndex = 0 ; $ rowIndex < $ matrix ->getBlockCount (); ++$ rowIndex ) {
@@ -71,21 +79,23 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
7179 }
7280 }
7381
82+ /** @var int<1, max> $targetWidth */
7483 $ targetWidth = $ matrix ->getOuterSize ();
84+ /** @var int<1, max> $targetHeight */
7585 $ targetHeight = $ matrix ->getOuterSize ();
7686
7787 if ($ label instanceof LabelInterface) {
7888 $ labelImageData = LabelImageData::createForLabel ($ label );
7989 $ targetHeight += $ labelImageData ->getHeight () + $ label ->getMargin ()->getTop () + $ label ->getMargin ()->getBottom ();
8090 }
8191
92+ /** @var int<1, max> $targetHeight */
8293 $ targetImage = imagecreatetruecolor ($ targetWidth , $ targetHeight );
8394
8495 if (!$ targetImage ) {
8596 throw new \Exception ('Unable to generate image: please check if the GD extension is enabled and configured correctly ' );
8697 }
8798
88- /** @var int $backgroundColor */
8999 $ backgroundColor = imagecolorallocatealpha (
90100 $ targetImage ,
91101 $ qrCode ->getBackgroundColor ()->getRed (),
@@ -94,6 +104,10 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
94104 $ qrCode ->getBackgroundColor ()->getAlpha ()
95105 );
96106
107+ if (false === $ backgroundColor ) {
108+ throw new \Exception ('Unable to generate image: please check if the GD extension is enabled and configured correctly ' );
109+ }
110+
97111 imagefill ($ targetImage , 0 , 0 , $ backgroundColor );
98112
99113 imagecopyresampled (
@@ -138,8 +152,12 @@ private function addLogo(LogoInterface $logo, GdResult $result): GdResult
138152 $ matrix = $ result ->getMatrix ();
139153
140154 if ($ logoImageData ->getPunchoutBackground ()) {
141- /** @var int $transparent */
142155 $ transparent = imagecolorallocatealpha ($ targetImage , 255 , 255 , 255 , 127 );
156+
157+ if (false === $ transparent ) {
158+ throw new \Exception ('Unable to allocate color: please check if the GD extension is enabled and configured correctly ' );
159+ }
160+
143161 imagealphablending ($ targetImage , false );
144162 $ xOffsetStart = intval ($ matrix ->getOuterSize () / 2 - $ logoImageData ->getWidth () / 2 );
145163 $ yOffsetStart = intval ($ matrix ->getOuterSize () / 2 - $ logoImageData ->getHeight () / 2 );
@@ -172,7 +190,6 @@ private function addLabel(LabelInterface $label, GdResult $result): GdResult
172190
173191 $ labelImageData = LabelImageData::createForLabel ($ label );
174192
175- /** @var int $textColor */
176193 $ textColor = imagecolorallocatealpha (
177194 $ targetImage ,
178195 $ label ->getTextColor ()->getRed (),
@@ -181,6 +198,10 @@ private function addLabel(LabelInterface $label, GdResult $result): GdResult
181198 $ label ->getTextColor ()->getAlpha ()
182199 );
183200
201+ if (false === $ textColor ) {
202+ throw new \Exception ('Unable to allocate color: please check if the GD extension is enabled and configured correctly ' );
203+ }
204+
184205 $ x = intval (imagesx ($ targetImage ) / 2 - $ labelImageData ->getWidth () / 2 );
185206 $ y = imagesy ($ targetImage ) - $ label ->getMargin ()->getBottom ();
186207
0 commit comments