Passed
Push — develop ( ddb92e...5ee4e8 )
by BENARD
04:19
created

Picture::getHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\File;
4
5
use Exception;
6
use GdImage;
7
8
class Picture implements PictureInterface
9
{
10
    protected $image = null;
11
12
    protected array $fonts = [];
13
    protected array $colors = [];
14
15
    protected ?string $activeColor = null;
16
    protected ?string $activeFont = null;
17
18
19
    /**
20
     * Picture constructor.
21
     * @param      $width
22
     * @param      $height
23
     */
24
    public function __construct($width, $height)
25
    {
26
        $width = (int) $width;
27
        $height = (int) $height;
28
29
        $this->image = imagecreatetruecolor($width, $height);
30
    }
31
32
    /**
33
     * @return $this
34
     */
35
    public static function create(GdImage $image): Picture
36
    {
37
        $oSelf = new self(imagesx($image), imagesy($image));
38
        $oSrc = new self(imagesx($image), imagesy($image));
39
        $oSrc->setImage($image);
40
        $oSelf->copyResized($oSrc, 0, 0);
41
        unset($oSrc);
42
        return $oSelf;
43
    }
44
45
    /**
46
     *
47
     */
48
    public function __destruct()
49
    {
50
        imagedestroy($this->image);
51
    }
52
53
    /**
54
     * @param $image
55
     * @return $this
56
     */
57
    public function setImage($image): Picture
58
    {
59
        $this->image = $image;
60
        return $this;
61
    }
62
63
    /**
64
     * @return int
65
     */
66
    public function getWidth():int
67
    {
68
        return imagesx($this->image);
69
    }
70
71
    /**
72
     * @return int
73
     */
74
    public function getHeight(): int
75
    {
76
        return imagesy($this->image);
77
    }
78
79
    /**
80
     * @return false|resource|null
81
     */
82
    public function getImage()
83
    {
84
        return $this->image;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->image also could return the type GdImage which is incompatible with the documented return type false|null|resource.
Loading history...
85
    }
86
87
    /**
88
     * @param $name
89
     * @return mixed
90
     * @throws Exception
91
     */
92
    public function getColor($name): mixed
93
    {
94
        if (!isset($this->colors[$name])) {
95
            throw new Exception('Unknown color ' . $name . '.');
96
        }
97
        $this->activeColor = $this->colors[$name];
98
        return $this->colors[$name];
99
    }
100
101
    /**
102
     * @param $name
103
     * @return mixed
104
     * @throws Exception
105
     */
106
    public function getFont($name): mixed
107
    {
108
        if (!isset($this->fonts[$name])) {
109
            throw new Exception('Unknown font ' . $name . '.');
110
        }
111
        $this->activeFont = $this->fonts[$name];
112
        return $this->fonts[$name];
113
    }
114
115
    /**
116
     * @param $name
117
     * @param $filePath
118
     * @return Picture
119
     * @throws Exception
120
     */
121
    public function addFont($name, $filePath): Picture
122
    {
123
        $fontPath = realpath($filePath);
124
        if ($fontPath === false) {
125
            throw new Exception('Unable to load font file. The file does not exists.');
126
        }
127
        $this->fonts[$name] = $fontPath;
128
        $this->activeFont = $fontPath;
129
        return $this;
130
    }
131
132
    /**
133
     * @param      $name
134
     * @param      $red
135
     * @param      $green
136
     * @param      $blue
137
     * @param      $alpha
138
     * @return Picture
139
     */
140
    public function addColor($name, $red, $green, $blue, $alpha = null): Picture
141
    {
142
        $red %= 256;
143
        $green %= 256;
144
        $blue %= 256;
145
        if ($alpha !== null) {
146
            $alpha %= 128;
147
            $color = imagecolorallocatealpha($this->image, $red, $green, $blue, $alpha);
148
        } else {
149
            $color = imagecolorallocate($this->image, $red, $green, $blue);
150
        }
151
        $this->colors[$name] = $color;
152
        $this->activeColor = $color;
153
        return $this;
154
    }
155
156
    /**
157
     * @param Picture $pic
158
     * @param                   $dstX
159
     * @param                   $dstY
160
     * @param int               $srcX
161
     * @param int               $srcY
162
     * @param null              $dstW
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dstW is correct as it would always require null to be passed?
Loading history...
163
     * @param null              $dstH
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dstH is correct as it would always require null to be passed?
Loading history...
164
     * @param null              $srcW
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $srcW is correct as it would always require null to be passed?
Loading history...
165
     * @param null              $srcH
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $srcH is correct as it would always require null to be passed?
Loading history...
166
     * @return Picture
167
     */
168
    public function copyResized(
169
        Picture $pic,
170
        $dstX,
171
        $dstY,
172
        $srcX = 0,
173
        $srcY = 0,
174
        $dstW = null,
175
        $dstH = null,
176
        $srcW = null,
177
        $srcH = null
178
    ): Picture {
179
        $dstW = is_null($dstW) ? $pic->getWidth() : $dstW;
0 ignored issues
show
introduced by
The condition is_null($dstW) is always true.
Loading history...
180
        $dstH = is_null($dstH) ? $pic->getHeight() : $dstH;
0 ignored issues
show
introduced by
The condition is_null($dstH) is always true.
Loading history...
181
        $srcW = is_null($srcW) ? $pic->getWidth() : $srcW;
0 ignored issues
show
introduced by
The condition is_null($srcW) is always true.
Loading history...
182
        $srcH = is_null($srcH) ? $pic->getHeight() : $srcH;
0 ignored issues
show
introduced by
The condition is_null($srcH) is always true.
Loading history...
183
184
        imagecopyresized($this->image, $pic->getImage(), $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
185
        return $this;
186
    }
187
188
    /**
189
     * @param      $xA
190
     * @param      $yA
191
     * @param      $xB
192
     * @param      $yB
193
     * @param null $color
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $color is correct as it would always require null to be passed?
Loading history...
194
     * @return Picture
195
     * @throws Exception
196
     */
197
    public function addRectangle($xA, $yA, $xB, $yB, $color = null): Picture
198
    {
199
        if ($color === null) {
0 ignored issues
show
introduced by
The condition $color === null is always true.
Loading history...
200
            $color = $this->activeColor;
201
            if ($color === null) {
202
                throw new Exception('No active color defined.');
203
            }
204
        }
205
        imagefilledrectangle($this->image, $xA, $yA, $xB, $yB, $color);
0 ignored issues
show
Bug introduced by
$color of type string is incompatible with the type integer expected by parameter $color of imagefilledrectangle(). ( Ignorable by Annotation )

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

205
        imagefilledrectangle($this->image, $xA, $yA, $xB, $yB, /** @scrutinizer ignore-type */ $color);
Loading history...
206
        return $this;
207
    }
208
209
    /**
210
     * @param      $message
211
     * @param      $size
212
     * @param      $x
213
     * @param      $y
214
     * @param int  $angle
215
     * @param null $color
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $color is correct as it would always require null to be passed?
Loading history...
216
     * @param null $font
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $font is correct as it would always require null to be passed?
Loading history...
217
     * @return Picture
218
     * @throws Exception
219
     */
220
    public function write($message, $size, $x, $y, int $angle = 0, $color = null, $font = null): Picture
221
    {
222
        if ($color === null) {
0 ignored issues
show
introduced by
The condition $color === null is always true.
Loading history...
223
            $color = $this->activeColor;
224
            if ($color === null) {
225
                throw new Exception('No active color defined.');
226
            }
227
        }
228
        if ($font === null) {
0 ignored issues
show
introduced by
The condition $font === null is always true.
Loading history...
229
            $font = $this->activeFont;
230
            if ($font === null) {
231
                throw new Exception('No active font defined.');
232
            }
233
        }
234
        imagettftext($this->image, $size, $angle, $x, $y, $color, $font, $message);
0 ignored issues
show
Bug introduced by
$color of type string is incompatible with the type integer expected by parameter $color of imagettftext(). ( Ignorable by Annotation )

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

234
        imagettftext($this->image, $size, $angle, $x, $y, /** @scrutinizer ignore-type */ $color, $font, $message);
Loading history...
235
        return $this;
236
    }
237
238
239
    /**
240
     * @param        $type
241
     * @param string $filename
242
     * @throws Exception
243
     */
244
    public function downloadPicture($type, string $filename): void
245
    {
246
        header('Content-Disposition: "attachement"; filename="' . $filename . '"');
247
        $this->showPicture($type);
248
    }
249
250
    /**
251
     * @param $type
252
     * @throws Exception
253
     */
254
    public function showPicture($type)
255
    {
256
        $method = self::getGererateMethod($type);
257
        $contentType = self::getMimeType($type);
258
259
        header('Content-Type: ' . $contentType);
260
        $method($this->image);
261
        exit;
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...
262
    }
263
264
    /**
265
     * @param $filename
266
     * @return $this
267
     * @throws Exception
268
     */
269
    public function savePicture($filename): static
270
    {
271
        $sExtension = pathinfo($filename, PATHINFO_EXTENSION);
272
        $method = $this->getGererateMethod($sExtension);
273
274
        $method($this->image, $filename);
275
        return $this;
276
    }
277
278
    /**
279
     * @param $type
280
     * @return string
281
     * @throws Exception
282
     */
283
    protected static function getGererateMethod($type): string
284
    {
285
        if (!array_key_exists($type, self::EXTENSIONS)) {
286
            throw new Exception('Unknown picture type.');
287
        }
288
        return self::EXTENSIONS[$type]['generate'];
289
    }
290
291
    /**
292
     * @param $type
293
     * @return string
294
     * @throws Exception
295
     */
296
    public static function getCreateMethod($type): string
297
    {
298
        if (!array_key_exists($type, self::EXTENSIONS)) {
299
            throw new Exception('Unknown picture type.');
300
        }
301
        return self::EXTENSIONS[$type]['create'];
302
    }
303
304
    /**
305
     * @param $extension
306
     * @return string
307
     */
308
    public static function getMimeType($extension): string
309
    {
310
        if (!isset(self::MIME_TYPES[$extension])) {
311
            return 'application/octet-stream';
312
        }
313
        return self::MIME_TYPES[$extension];
314
    }
315
}
316