Issues (99)

src/Gateways/barcode/BCGFontPhp.php (3 issues)

1
<?php
2
namespace tinymeng\code\Gateways\barcode;
3
4
/**
5
 *--------------------------------------------------------------------
6
 *
7
 * Holds font for PHP.
8
 *
9
 *--------------------------------------------------------------------
10
 * Copyright (C) Jean-Sebastien Goupil
11
 * http://www.barcodephp.com
12
 */
13
class BCGFontPhp implements BCGFont {
14
    private $font;
15
    private $text;
16
    private $rotationAngle;
17
    private $backgroundColor;
18
    private $foregroundColor;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param int $font
24
     */
25
    public function __construct($font) {
26
        $this->font = max(0, intval($font));
27
        $this->backgroundColor = new BCGColor('white');
28
        $this->foregroundColor = new BCGColor('black');
29
        $this->setRotationAngle(0);
30
    }
31
32
    /**
33
     * Gets the text associated to the font.
34
     *
35
     * @return string
36
     */
37
    public function getText() {
38
        return $this->text;
39
    }
40
41
    /**
42
     * Sets the text associated to the font.
43
     *
44
     * @param string text
0 ignored issues
show
The type tinymeng\code\Gateways\barcode\text 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...
45
     */
46
    public function setText($text) {
47
        $this->text = $text;
48
    }
49
50
    /**
51
     * Gets the rotation in degree.
52
     *
53
     * @return int
54
     */
55
    public function getRotationAngle() {
56
        return (360 - $this->rotationAngle) % 360;
57
    }
58
59
    /**
60
     * Sets the rotation in degree.
61
     *
62
     * @param int
63
     */
64
    public function setRotationAngle($rotationAngle) {
65
        $this->rotationAngle = (int)$rotationAngle;
66
        if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
67
            $this->rotationAngle = 0;
68
        }
69
70
        $this->rotationAngle = (360 - $this->rotationAngle) % 360;
71
    }
72
73
    /**
74
     * Gets the background color.
75
     *
76
     * @return BCGColor
77
     */
78
    public function getBackgroundColor() {
79
        return $this->backgroundColor;
80
    }
81
82
    /**
83
     * Sets the background color.
84
     *
85
     * @param BCGColor $backgroundColor
86
     */
87
    public function setBackgroundColor($backgroundColor) {
88
        $this->backgroundColor = $backgroundColor;
89
    }
90
91
    /**
92
     * Gets the foreground color.
93
     *
94
     * @return BCGColor
95
     */
96
    public function getForegroundColor() {
97
        return $this->foregroundColor;
98
    }
99
100
    /**
101
     * Sets the foreground color.
102
     *
103
     * @param BCGColor $foregroundColor
104
     */
105
    public function setForegroundColor($foregroundColor) {
106
        $this->foregroundColor = $foregroundColor;
107
    }
108
109
    /**
110
     * Returns the width and height that the text takes to be written.
111
     *
112
     * @return int[]
113
     */
114
    public function getDimension() {
115
        $w = imagefontwidth($this->font) * strlen($this->text);
116
        $h = imagefontheight($this->font);
117
118
        $rotationAngle = $this->getRotationAngle();
119
        if ($rotationAngle === 90 || $rotationAngle === 270) {
120
            return array($h, $w);
121
        } else {
122
            return array($w, $h);
123
        }
124
    }
125
126
    /**
127
     * Draws the text on the image at a specific position.
128
     * $x and $y represent the left bottom corner.
129
     *
130
     * @param resource $im
131
     * @param int $x
132
     * @param int $y
133
     */
134
    public function draw($im, $x, $y) {
135
        if ($this->getRotationAngle() !== 0) {
136
            if (!function_exists('imagerotate')) {
137
                throw new BCGDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
138
            }
139
140
            $w = imagefontwidth($this->font) * strlen($this->text);
141
            $h = imagefontheight($this->font);
142
            $gd = imagecreatetruecolor($w, $h);
143
            imagefilledrectangle($gd, 0, 0, $w - 1, $h - 1, $this->backgroundColor->allocate($gd));
0 ignored issues
show
It seems like $gd 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

143
            imagefilledrectangle($gd, 0, 0, $w - 1, $h - 1, $this->backgroundColor->allocate(/** @scrutinizer ignore-type */ $gd));
Loading history...
144
            imagestring($gd, $this->font, 0, 0, $this->text, $this->foregroundColor->allocate($gd));
145
            $gd = imagerotate($gd, $this->rotationAngle, 0);
146
            imagecopy($im, $gd, $x, $y, 0, 0, imagesx($gd), imagesy($gd));
147
        } else {
148
            imagestring($im, $this->font, $x, $y, $this->text, $this->foregroundColor->allocate($im));
149
        }
150
    }
151
}
152
?>
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...