Issues (4542)

config/qrcode/qrimage.php (5 issues)

1
<?php
2
/*
3
 * PHP QR Code encoder
4
 *
5
 * Image output of code using GD2
6
 *
7
 * PHP QR Code is distributed under LGPL 3
8
 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
    define('QR_IMAGE', true);
26
27
    class qrimage
28
    {
29
        //----------------------------------------------------------------------
30
        public static function png($frame, $filename, $pixelPerPoint, $outerFrame, $saveandprint, $back_color, $fore_color)
31
        {
32
            $image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
33
34
            if ($filename === false) {
35
                header('Content-type: image/png');
36
                imagepng($image);
37
            } else {
38
                if ($saveandprint === true) {
39
                    imagepng($image, $filename);
40
                    header('Content-type: image/png');
41
                    imagepng($image);
42
                } else {
43
                    @imagepng($image, $filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for imagepng(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

43
                    /** @scrutinizer ignore-unhandled */ @imagepng($image, $filename);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
                }
45
            }
46
47
            imagedestroy($image);
48
        }
49
50
        //----------------------------------------------------------------------
51
        public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
52
        {
53
            $image = self::image($frame, $pixelPerPoint, $outerFrame);
54
55
            if ($filename === false) {
56
                header('Content-type: image/jpeg');
57
                imagejpeg($image, null, $q);
58
            } else {
59
                imagejpeg($image, $filename, $q);
60
            }
61
62
            imagedestroy($image);
63
        }
64
65
        //----------------------------------------------------------------------
66
        private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
67
        {
68
            $h = count($frame);
69
            $w = strlen($frame[0]);
70
71
            $imgW = $w + 2 * $outerFrame;
72
            $imgH = $h + 2 * $outerFrame;
73
74
            $base_image = imagecreate($imgW, $imgH);
75
76
            // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
77
            $r1 = round((($fore_color & 0xFF0000) >> 16), 5);
78
            $b1 = round((($fore_color & 0x00FF00) >> 8), 5);
79
            $g1 = round(($fore_color & 0x0000FF), 5);
80
81
            // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
82
            $r2 = round((($back_color & 0xFF0000) >> 16), 5);
83
            $b2 = round((($back_color & 0x00FF00) >> 8), 5);
84
            $g2 = round(($back_color & 0x0000FF), 5);
85
86
            $col[0] = imagecolorallocate($base_image, $r2, $b2, $g2);
0 ignored issues
show
$b2 of type double is incompatible with the type integer expected by parameter $green of imagecolorallocate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $col[0] = imagecolorallocate($base_image, $r2, /** @scrutinizer ignore-type */ $b2, $g2);
Loading history...
$r2 of type double is incompatible with the type integer expected by parameter $red of imagecolorallocate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $col[0] = imagecolorallocate($base_image, /** @scrutinizer ignore-type */ $r2, $b2, $g2);
Loading history...
$g2 of type double is incompatible with the type integer expected by parameter $blue of imagecolorallocate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $col[0] = imagecolorallocate($base_image, $r2, $b2, /** @scrutinizer ignore-type */ $g2);
Loading history...
Comprehensibility Best Practice introduced by
$col was never initialized. Although not strictly required by PHP, it is generally a good practice to add $col = array(); before regardless.
Loading history...
87
            $col[1] = imagecolorallocate($base_image, $r1, $b1, $g1);
88
89
            imagefill($base_image, 0, 0, $col[0]);
90
91
            for ($y = 0; $y < $h; $y++) {
92
                for ($x = 0; $x < $w; $x++) {
93
                    if ($frame[$y][$x] == '1') {
94
                        imagesetpixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]);
95
                    }
96
                }
97
            }
98
99
            $target_image = imagecreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
100
            imagecopyresized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
101
            imagedestroy($base_image);
102
103
            return $target_image;
104
        }
105
    }
106