ColorBlockPng   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 56
ccs 22
cts 23
cp 0.9565
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A image() 0 21 2
A hexColorAllocate() 0 8 1
A makeImage() 0 3 1
A mimeType() 0 3 1
1
<?php
2
3
namespace Limsum\ColorBlock;
4
5
use Illuminate\Support\Facades\Response;
6
7
class ColorBlockPng extends AbstractColorBlockImage
8
{
9 1
    public function image(): \Illuminate\Http\Response
10
    {
11 1
        if (!extension_loaded('gd')) {
12
            throw new \Exception('FG extension not loaded.');
13
        }
14
15 1
        $image = imagecreatetruecolor((int) $this->with, (int) $this->height);
16
17 1
        $color = $this->hexColorAllocate($image, $this->color);
18
19 1
        imagefilledrectangle($image, 0, 0, (int) $this->with, (int) $this->height, $color);
20
21 1
        ob_start();
22 1
        $this->makeImage($image);
23 1
        $imageData = ob_get_contents();
24 1
        ob_end_clean();
25
26 1
        imagedestroy($image);
27
28 1
        return Response::make($imageData, 200, [
29 1
            'Content-Type' => $this->mimeType(),
30
        ]);
31
    }
32
33
    /**
34
     * @param $image
35
     * @param $hex
36
     *
37
     * @return bool|int
38
     */
39 1
    protected function hexColorAllocate($image, $hex): bool|int
40
    {
41 1
        $hex = ltrim($hex, '#');
42 1
        $r   = (int) hexdec(substr($hex, 0, 2));
43 1
        $g   = (int) hexdec(substr($hex, 2, 2));
44 1
        $b   = (int) hexdec(substr($hex, 4, 2));
45
46 1
        return imagecolorallocate($image, $r, $g, $b);
47
    }
48
49
    /**
50
     * @param $image
51
     */
52 1
    protected function makeImage($image)
53
    {
54 1
        imagepng($image);
55
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    protected function mimeType():string
61
    {
62 1
        return 'image/png';
63
    }
64
}
65