Issues (99)

src/Gateways/barcode/BCGDrawing.php (7 issues)

1
<?php
2
namespace tinymeng\code\Gateways\barcode;
3
use tinymeng\code\Gateways\barcode\drawer\BCGDrawJPG;
4
use tinymeng\code\Gateways\barcode\drawer\BCGDrawPNG;
5
6
/**
7
 *--------------------------------------------------------------------
8
 *
9
 * Holds the drawing $im
10
 * You can use get_im() to add other kind of form not held into these classes.
11
 *
12
 *--------------------------------------------------------------------
13
 * Copyright (C) Jean-Sebastien Goupil
14
 * http://www.barcodephp.com
15
 */
16
class BCGDrawing {
17
    const IMG_FORMAT_PNG = 1;
18
    const IMG_FORMAT_JPEG = 2;
19
    const IMG_FORMAT_GIF = 3;
20
    const IMG_FORMAT_WBMP = 4;
21
22
    private $w, $h;         // int
23
    private $color;         // BCGColor
24
    private $filename;      // char *
25
    private $im;            // {object}
26
    private $barcode;       // BCGBarcode
27
    private $dpi;           // float
28
    private $rotateDegree;  // float
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param int $w
34
     * @param int $h
35
     * @param string filename
0 ignored issues
show
The type tinymeng\code\Gateways\barcode\filename was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     * @param BCGColor $color
37
     */
38
    public function __construct($filename = null, BCGColor $color) {
39
        $this->im = null;
40
        $this->setFilename($filename);
41
        $this->color = $color;
42
        $this->dpi = null;
43
        $this->rotateDegree = 0.0;
44
    }
45
46
    /**
47
     * Destructor.
48
     */
49
    public function __destruct() {
50
        $this->destroy();
51
    }
52
53
    /**
54
     * Gets the filename.
55
     *
56
     * @return string
57
     */
58
    public function getFilename() {
59
        return $this->filename;
60
    }
61
62
    /**
63
     * Sets the filename.
64
     *
65
     * @param string $filaneme
66
     */
67
    public function setFilename($filename) {
68
        $this->filename = $filename;
69
    }
70
71
    /**
72
     * @return resource.
0 ignored issues
show
Documentation Bug introduced by
The doc comment resource. at position 0 could not be parsed: Unknown type name 'resource.' at position 0 in resource..
Loading history...
73
     */
74
    public function get_im() {
75
        return $this->im;
76
    }
77
78
    /**
79
     * Sets the image.
80
     *
81
     * @param resource $im
82
     */
83
    public function set_im($im) {
84
        $this->im = $im;
85
    }
86
87
    /**
88
     * Gets barcode for drawing.
89
     *
90
     * @return BCGBarcode
91
     */
92
    public function getBarcode() {
93
        return $this->barcode;
94
    }
95
96
    /**
97
     * Sets barcode for drawing.
98
     *
99
     * @param BCGBarcode $barcode
100
     */
101
    public function setBarcode(BCGBarcode $barcode) {
102
        $this->barcode = $barcode;
103
    }
104
105
    /**
106
     * Gets the DPI for supported filetype.
107
     *
108
     * @return float
109
     */
110
    public function getDPI() {
111
        return $this->dpi;
112
    }
113
114
    /**
115
     * Sets the DPI for supported filetype.
116
     *
117
     * @param float $dpi
118
     */
119
    public function setDPI($dpi) {
120
        $this->dpi = $dpi;
121
    }
122
123
    /**
124
     * Gets the rotation angle in degree clockwise.
125
     *
126
     * @return float
127
     */
128
    public function getRotationAngle() {
129
        return $this->rotateDegree;
130
    }
131
132
    /**
133
     * Sets the rotation angle in degree clockwise.
134
     *
135
     * @param float $degree
136
     */
137
    public function setRotationAngle($degree) {
138
        $this->rotateDegree = (float)$degree;
139
    }
140
141
    /**
142
     * Draws the barcode on the image $im.
143
     */
144
    public function draw() {
145
        $size = $this->barcode->getDimension(0, 0);
146
        $this->w = max(1, $size[0]);
147
        $this->h = max(1, $size[1]);
148
        $this->init();
149
        $this->barcode->draw($this->im);
150
    }
151
152
    /**
153
     * Saves $im into the file (many format available).
154
     *
155
     * @param int $image_style
156
     * @param int $quality
157
     */
158
    public function finish($image_style = self::IMG_FORMAT_PNG, $quality = 100) {
159
        $drawer = null;
160
161
        $im = $this->im;
162
        if ($this->rotateDegree > 0.0) {
163
            if (function_exists('imagerotate')) {
164
                $im = imagerotate($this->im, 360 - $this->rotateDegree, $this->color->allocate($this->im));
165
            } else {
166
                throw new BCGDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
167
            }
168
        }
169
170
        if ($image_style === self::IMG_FORMAT_PNG) {
171
            $drawer = new BCGDrawPNG($im);
172
            $drawer->setFilename($this->filename);
173
            $drawer->setDPI($this->dpi);
174
        } elseif ($image_style === self::IMG_FORMAT_JPEG) {
175
            $drawer = new BCGDrawJPG($im);
176
            $drawer->setFilename($this->filename);
177
            $drawer->setDPI($this->dpi);
178
            $drawer->setQuality($quality);
179
        } elseif ($image_style === self::IMG_FORMAT_GIF) {
180
            // Some PHP versions have a bug if passing 2nd argument as null.
181
            if ($this->filename === null || $this->filename === '') {
182
                imagegif($im);
183
            } else {
184
                imagegif($im, $this->filename);
185
            }
186
        } elseif ($image_style === self::IMG_FORMAT_WBMP) {
187
            imagewbmp($im, $this->filename);
188
        }
189
190
        if ($drawer !== null) {
191
            $drawer->draw();
192
        }
193
    }
194
195
    /**
196
     * Writes the Error on the picture.
197
     *
198
     * @param Exception $exception
0 ignored issues
show
The type tinymeng\code\Gateways\barcode\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
199
     */
200
    public function drawException($exception) {
201
        $this->w = 1;
202
        $this->h = 1;
203
        $this->init();
204
205
        // Is the image big enough?
206
        $w = imagesx($this->im);
207
        $h = imagesy($this->im);
208
209
        $text = 'Error: ' . $exception->getMessage();
210
211
        $width = imagefontwidth(2) * strlen($text);
212
        $height = imagefontheight(2);
213
        if ($width > $w || $height > $h) {
214
            $width = max($w, $width);
215
            $height = max($h, $height);
216
217
            // We change the size of the image
218
            $newimg = imagecreatetruecolor($width, $height);
219
            imagefill($newimg, 0, 0, imagecolorat($this->im, 0, 0));
220
            imagecopy($newimg, $this->im, 0, 0, 0, 0, $w, $h);
221
            $this->im = $newimg;
222
        }
223
224
        $black = new BCGColor('black');
225
        imagestring($this->im, 2, 0, 0, $text, $black->allocate($this->im));
226
    }
227
228
    /**
229
     * Free the memory of PHP (called also by destructor).
230
     */
231
    public function destroy() {
232
        @imagedestroy($this->im);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for imagedestroy(). 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

232
        /** @scrutinizer ignore-unhandled */ @imagedestroy($this->im);

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...
233
    }
234
235
    /**
236
     * Init Image and color background.
237
     */
238
    private function init() {
239
        if ($this->im === null) {
240
            $this->im = imagecreatetruecolor($this->w, $this->h)
241
            or die('Can\'t Initialize the GD Libraty');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
242
            imagefilledrectangle($this->im, 0, 0, $this->w - 1, $this->h - 1, $this->color->allocate($this->im));
0 ignored issues
show
It seems like $this->im can also be of type GdImage; however, parameter $im of tinymeng\code\Gateways\b...de\BCGColor::allocate() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

242
            imagefilledrectangle($this->im, 0, 0, $this->w - 1, $this->h - 1, $this->color->allocate(/** @scrutinizer ignore-type */ $this->im));
Loading history...
243
        }
244
    }
245
}
246
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...