Passed
Push — master ( 34ef5d...2f53de )
by Michael
04:04 queued 19s
created

GD::add_ttf_text()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 15
rs 10
cc 2
nc 2
nop 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight;
6
7
/*
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
17
/**
18
 * @copyright    XOOPS Project (https://xoops.org)
19
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @author       XOOPS Development Team
21
 * @author       Pascal Le Boustouller: original author ([email protected])
22
 * @author       Luc Bizet (www.frxoops.org)
23
 * @author       jlm69 (www.jlmzone.com)
24
 * @author       mamba (www.xoops.org)
25
 */
26
27
/**
28
 * Class GD
29
 */
30
class GD
31
{
32
    public $image;
33
    public $width;
34
    public $height;
35
36
    /**
37
     * @param $location
38
     */
39
    public function __construct($location)
40
    {
41
        $imageinfo = @getimagesize($location) || exit('Unknown picture');
0 ignored issues
show
Best Practice introduced by
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...
42
43
        $this->width  = $imageinfo['0'];
44
        $this->height = $imageinfo['1'];
45
46
        switch ($imageinfo['2']) {
47
            case '1':
48
                $this->image = imagecreatefromgif($location);
49
                break;
50
            case '2':
51
                $this->image = imagecreatefromjpeg($location);
52
                break;
53
            case '3':
54
                $this->image = imagecreatefrompng($location);
55
                break;
56
            default:
57
                exit('Unknown file format');
0 ignored issues
show
Best Practice introduced by
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...
58
        }
59
    }
60
61
    /**
62
     * @param $sizex
63
     * @param $sizey
64
     */
65
    public function resize($sizex, $sizey): void
66
    {
67
        $org = round($this->width / $this->height, 2);
68
        $new = round($sizex / $sizey, 2);
69
70
        if ($new > $org) {
71
            $sizex = round($this->width / ($this->height / $sizey), 0);
72
            //            $sizey = $sizey;
73
        } else {
74
            //            $sizex = $sizex;
75
            $sizey = round($this->height / ($this->width / $sizex), 0);
76
        }
77
78
        $resized = imagecreatetruecolor((int)$sizex, (int)$sizey);
79
        imagecopyresampled($resized, $this->image, 0, 0, 0, 0, (int)$sizex, (int)$sizey, $this->width, $this->height);
80
81
        $this->image  = $resized;
82
        $this->width  = $sizex;
83
        $this->height = $sizey;
84
    }
85
86
    /**
87
     * @param $color
88
     * @return array
89
     */
90
    public function make_color($color): array
91
    {
92
        $rgb = [];
93
        if (is_array($color) && '3' === count($color)) {
94
            $rgb['r'] = $color['0'];
95
            $rgb['g'] = $color['1'];
96
            $rgb['b'] = $color['2'];
97
        } elseif (preg_match('#^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$#i', $color, $results)) {
98
            $rgb['r'] = hexdec($results['1']);
99
            $rgb['g'] = hexdec($results['2']);
100
            $rgb['b'] = hexdec($results['3']);
101
        } else {
102
            exit('Unknown color');
0 ignored issues
show
Best Practice introduced by
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...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
103
        }
104
105
        foreach (
106
            [
107
                'r',
108
                'g',
109
                'b',
110
            ] as $value
111
        ) {
112
            if (!array_key_exists($value, $rgb) || $rgb[$value] < 0
113
                || $rgb[$value] > 255
114
                || !is_numeric($rgb[$value])) {
115
                exit('Wrong color');
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Best Practice introduced by
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...
116
            }
117
        }
118
119
        return $rgb;
120
    }
121
122
    /**
123
     * @param $width
124
     * @param $color
125
     */
126
    public function add_border($width, $color): void
127
    {
128
        $rgb      = $this->make_color($color);
129
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
130
131
        if ($width < 1) {
132
            exit('Wrong frame width');
0 ignored issues
show
Best Practice introduced by
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...
133
        }
134
135
        $sizex     = $this->width + (2 * $width);
136
        $sizey     = $this->height + (2 * $width);
137
        $new_image = imagecreatetruecolor($sizex, $sizey);
138
139
        imagefill($new_image, 0, 0, $allocate);
140
        imagecopyresampled(
141
            $new_image,
142
            $this->image,
143
            $width,
144
            $width,
145
            0,
146
            0,
147
            $this->width,
148
            $this->height,
149
            $this->width,
150
            $this->height
151
        );
152
        $this->image  = $new_image;
153
        $this->width  = $sizex;
154
        $this->height = $sizey;
155
    }
156
157
    /**
158
     * @param $text
159
     * @param $font
160
     * @param $color
161
     * @param $x
162
     * @param $y
163
     */
164
    public function add_text(
165
        $text,
166
        $font,
167
        $color,
168
        $x,
0 ignored issues
show
Unused Code introduced by
The parameter $x is not used and could be removed. ( Ignorable by Annotation )

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

168
        /** @scrutinizer ignore-unused */ $x,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
        $y
0 ignored issues
show
Unused Code introduced by
The parameter $y is not used and could be removed. ( Ignorable by Annotation )

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

169
        /** @scrutinizer ignore-unused */ $y

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    ): void {
171
        if ($font < 1 || $font > 5) {
172
            exit('Wrong font');
0 ignored issues
show
Best Practice introduced by
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...
173
        }
174
175
        $rgb         = $this->make_color($color);
176
        $allocate    = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
0 ignored issues
show
Unused Code introduced by
The assignment to $allocate is dead and can be removed.
Loading history...
177
        $text_width  = imagefontwidth($font) * mb_strlen($text);
0 ignored issues
show
Unused Code introduced by
The assignment to $text_width is dead and can be removed.
Loading history...
178
        $text_height = imagefontheight($font);
0 ignored issues
show
Unused Code introduced by
The assignment to $text_height is dead and can be removed.
Loading history...
179
        //Dokoncaj
180
    }
181
182
    /**
183
     * @param        $text
184
     * @param        $size
185
     * @param        $color
186
     * @param        $x
187
     * @param        $y
188
     * @param string $font
189
     */
190
    public function add_ttf_text(
191
        $text,
192
        $size,
193
        $color,
194
        $x,
195
        $y,
196
        $font = './tahoma.ttf'
197
    ): void {
198
        if (!is_file($font)) {
199
            exit('Unknown font');
0 ignored issues
show
Best Practice introduced by
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...
200
        }
201
202
        $rgb      = $this->make_color($color);
203
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
204
        imagettftext($this->image, $size, 0, $x, $y, $allocate, $font, $text);
205
    }
206
207
    /**
208
     * @param        $location
209
     * @param string $quality
210
     */
211
    public function save(
212
        $location,
213
        $quality = '80'
214
    ): void {
215
        imagejpeg($this->image, $location, $quality);
0 ignored issues
show
Bug introduced by
$quality of type string is incompatible with the type integer expected by parameter $quality of imagejpeg(). ( Ignorable by Annotation )

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

215
        imagejpeg($this->image, $location, /** @scrutinizer ignore-type */ $quality);
Loading history...
216
        imagedestroy($this->image);
217
    }
218
}
219