Passed
Push — master ( 533749...fb4928 )
by compolom
01:48
created

GD   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 190
rs 10
c 0
b 0
f 0
wmc 18

15 Methods

Rating   Name   Duplication   Size   Complexity  
A tmp() 0 13 2
A flip() 0 5 1
A flop() 0 5 1
A grayscale() 0 5 1
A __construct() 0 3 1
A crop() 0 10 1
A setSizes() 0 4 1
A copyright() 0 19 2
A rotate() 0 10 1
A newImage() 0 9 1
A save() 0 3 1
A resize() 0 8 1
B prepareImage() 0 32 2
A __toString() 0 8 1
A watermark() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Compolomus\Compomage;
4
5
use Compolomus\Compomage\Interfaces\ImageInterface;
6
7
class GD extends AbstractImage implements ImageInterface
8
{
9
    /**
10
     * GD constructor.
11
     * @param string $image
12
     * @throws \Exception
13
     */
14
    public function __construct(string $image)
15
    {
16
        $this->init($image);
17
    }
18
19
    /**
20
     * @param string $source
21
     * @return ImageInterface
22
     * @throws \Exception
23
     */
24
    protected function tmp(string $source): ImageInterface
25
    {
26
        $image = imagecreatefromstring($source);
27
        if (!is_resource($image)) {
28
            throw new \Exception('Image create failed');
29
        }
30
        $this->setImage($image);
31
        $this->setSizes();
32
        // save transparent
33
        imagesavealpha($this->getImage(), true);
34
        imagealphablending($this->getImage(), false);
35
36
        return $this;
37
    }
38
39
    public function flip(): ImageInterface
40
    {
41
        imageflip($this->getImage(), IMG_FLIP_VERTICAL);
42
43
        return $this;
44
    }
45
46
    public function flop(): ImageInterface
47
    {
48
        imageflip($this->getImage(), IMG_FLIP_HORIZONTAL);
49
50
        return $this;
51
    }
52
53
    public function grayscale(): ImageInterface
54
    {
55
        imagefilter($this->getImage(), IMG_FILTER_GRAYSCALE);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param string $text
62
     * @param string $font
63
     * @param string $position
64
     * @return $this
65
     * @throws \Exception
66
     */
67
    public function copyright(string $text, string $font, string $position = 'SouthWest')
68
    {
69
        if (!array_key_exists(strtoupper($position), $this->positions)) {
70
            throw new \Exception('Wrong position');
71
        }
72
73
        imagecopymerge(
74
            $this->getImage(),
75
            $image = $this->prepareImage($text, $font),
76
            intval((($this->getWidth() - imagesx($image)) / 2) * $this->positions[strtoupper($position)]['x']) + $this->positions[strtoupper($position)]['padX'],
77
            intval((($this->getHeight() - imagesy($image)) / 2) * $this->positions[strtoupper($position)]['y']) + $this->positions[strtoupper($position)]['padY'],
78
            0,
79
            0,
80
            $this->getWidth(),
81
            $this->getHeight(),
82
            80
83
        );
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $text
90
     * @param string $font
91
     * @return resource
92
     * @throws \Exception
93
     */
94
    private function prepareImage(string $text, string $font)
95
    {
96
        $fontSize = 15;
97
        if (!$coordinates = imagettfbbox($fontSize, 0, $font, $text)) {
98
            throw new \Exception('Does not support font');
99
        }
100
        $minX = min([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]);
101
        $maxX = max([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]);
102
        $minY = min([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]);
103
        $maxY = max([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]);
104
105
        $textX = intval(abs($minX)) + 1;
106
        $textY = intval(abs($minY)) + 1;
107
108
        $image = $this->newImage($maxX - $minX + 2, $maxY - $minY + 2);
109
110
        $white = imagecolorallocate($image, 0, 0, 0);
111
        $blue = imagecolorallocate($image, 0, 128, 128);
112
        $red = imagecolorallocate($image, 255, 0, 0);
113
        $black = imagecolorallocate($image, 255, 255, 255);
114
        $gray = imagecolorallocate($image, 128, 128, 128);
115
116
        imagecolortransparent($image, $white);
117
        imagefilledrectangle($image, 0, 0, $this->getWidth(), 20, $white);
118
119
        imagettftext($image, $fontSize, 0, $textX, $textY, $white, $font, $text);
120
        imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $blue, $font, $text);
121
        imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $red, $font, $text);
122
        imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $black, $font, $text);
123
        imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $gray, $font, $text);
124
125
        return $image;
126
    }
127
128
    protected function setSizes(): void
129
    {
130
        $this->setWidth(imagesx($this->getImage()));
131
        $this->setHeight(imagesy($this->getImage()));
132
    }
133
134
    private function newImage(int $width, int $height)
135
    {
136
        $newimg = imagecreatetruecolor($width, $height);
137
        $transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
138
        imagefill($newimg, 0, 0, $transparent);
139
        imagealphablending($newimg, true);
140
        imagesavealpha($newimg, true);
141
142
        return $newimg;
143
    }
144
145
    public function resize(int $width, int $height): ImageInterface
146
    {
147
        $newimage = $this->newImage($width, $height);
148
        imagecopyresampled($newimage, $this->getImage(), 0, 0, 0, 0, $width, $height, $this->getWidth() , $this->getHeight());
149
        $this->setImage($newimage);
150
        $this->setSizes();
151
152
        return $this;
153
    }
154
155
    public function crop(int $width, int $height, int $x, int $y): ImageInterface
156
    {
157
        $width = $width - $x;
158
        $height = $height - $y;
159
        $newimage = $this->newImage($width, $height);
160
        imagecopyresampled($newimage, $this->getImage(), 0, 0, $x, $y, $width, $height, $width, $height);
161
        $this->setImage($newimage);
162
        $this->setSizes();
163
164
        return $this;
165
    }
166
167
    public function watermark(): ImageInterface
168
    {
169
        return $this;
170
    }
171
172
    public function rotate(int $angle = 90): ImageInterface
173
    {
174
        $transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
175
        $rotate = imagerotate($this->getImage(), $angle, $transparent);
176
        imagealphablending($rotate, true);
177
        imagesavealpha($rotate, true);
178
        $this->setImage($rotate);
179
        $this->setSizes();
180
181
        return $this;
182
    }
183
184
    public function __toString(): string
185
    {
186
        ob_start();
187
        imagepng($this->getImage(), null, 9, PNG_ALL_FILTERS);
188
        $temp = ob_get_contents();
189
        ob_clean();
190
191
        return trim($temp);
192
    }
193
194
    public function save(string $filename): bool
195
    {
196
        return true;
197
    }
198
}
199