Passed
Branch master (d4e5f4)
by compolom
03:14 queued 24s
created

GD::tmp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imageflip() 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

41
        imageflip(/** @scrutinizer ignore-type */ $this->getImage(), IMG_FLIP_VERTICAL);
Loading history...
42
43
        return $this;
44
    }
45
46
    public function flop(): ImageInterface
47
    {
48
        imageflip($this->getImage(), IMG_FLIP_HORIZONTAL);
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imageflip() 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

48
        imageflip(/** @scrutinizer ignore-type */ $this->getImage(), IMG_FLIP_HORIZONTAL);
Loading history...
49
50
        return $this;
51
    }
52
53
    public function grayscale(): ImageInterface
54
    {
55
        imagefilter($this->getImage(), IMG_FILTER_GRAYSCALE);
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imagefilter() 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

55
        imagefilter(/** @scrutinizer ignore-type */ $this->getImage(), IMG_FILTER_GRAYSCALE);
Loading history...
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
        $positions = [
70
            'NORTHWEST' => ['x' => 0, 'y' => 0, 'padX' => 10, 'padY' => 10],
71
            'NORTH'     => ['x' => 1, 'y' => 0, 'padX' => 0, 'padY' => 10],
72
            'NORTHEAST' => ['x' => 2, 'y' => 0, 'padX' => -10, 'padY' => 10],
73
            'WEST'      => ['x' => 0, 'y' => 1, 'padX' => 10, 'padY' => 0],
74
            'CENTER'    => ['x' => 1, 'y' => 1, 'padX' => 0, 'padY' => 0],
75
            'EAST'      => ['x' => 2, 'y' => 1, 'padX' => -10, 'padY' => 0],
76
            'SOUTHWEST' => ['x' => 0, 'y' => 2, 'padX' => 10, 'padY' => -10],
77
            'SOUTH'     => ['x' => 1, 'y' => 2, 'padX' => 0, 'padY' => -10],
78
            'SOUTHEAST' => ['x' => 2, 'y' => 2, 'padX' => -10, 'padY' => -10]
79
        ];
80
81
        if (!array_key_exists(strtoupper($position), $positions)) {
82
            throw new \Exception('Wrong position');
83
        }
84
85
        $image = $this->prepareImage($text, $font);
86
87
        imagecopymerge(
88
            $this->getImage(),
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $dst_im of imagecopymerge() 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

88
            /** @scrutinizer ignore-type */ $this->getImage(),
Loading history...
89
            $image,
90
            intval((($this->getWidth() - imagesx($image)) / 2) * $positions[strtoupper($position)]['x']) + $positions[strtoupper($position)]['padX'],
91
            intval((($this->getHeight() - imagesy($image)) / 2) * $positions[strtoupper($position)]['y']) + $positions[strtoupper($position)]['padY'],
92
            0,
93
            0,
94
            $this->getWidth(),
95
            $this->getHeight(),
96
            80
97
        );
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $text
104
     * @param string $font
105
     * @return resource
106
     * @throws \Exception
107
     */
108
    private function prepareImage(string $text, string $font)
109
    {
110
        $fontSize = 15;
111
        $coordinates = imagettfbbox($fontSize, 0, $font, $text);
112
        if (!is_array($coordinates)) {
0 ignored issues
show
introduced by
The condition is_array($coordinates) is always true.
Loading history...
113
            throw new \Exception('Does not support font');
114
        }
115
        $minX = min([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]);
116
        $maxX = max([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]);
117
        $minY = min([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]);
118
        $maxY = max([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]);
119
120
        $textX = abs($minX) + 1;
121
        $textY = abs($minY) + 1;
122
123
        $image = $this->newImage($maxX - $minX + 2, $maxY - $minY + 2);
124
125
        $white = imagecolorallocate($image, 0, 0, 0);
126
        $blue = imagecolorallocate($image, 0, 128, 128);
127
        $red = imagecolorallocate($image, 255, 0, 0);
128
        $black = imagecolorallocate($image, 255, 255, 255);
129
        $gray = imagecolorallocate($image, 128, 128, 128);
130
131
        imagecolortransparent($image, $white);
132
        imagefilledrectangle($image, 0, 0, $this->getWidth(), 20, $white);
133
134
        imagettftext($image, $fontSize, 0, $textX, $textY, $white, $font, $text);
0 ignored issues
show
Bug introduced by
$textX of type double is incompatible with the type integer expected by parameter $x 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

134
        imagettftext($image, $fontSize, 0, /** @scrutinizer ignore-type */ $textX, $textY, $white, $font, $text);
Loading history...
Bug introduced by
$textY of type double is incompatible with the type integer expected by parameter $y 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

134
        imagettftext($image, $fontSize, 0, $textX, /** @scrutinizer ignore-type */ $textY, $white, $font, $text);
Loading history...
135
        imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $blue, $font, $text);
136
        imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $red, $font, $text);
137
        imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $black, $font, $text);
138
        imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $gray, $font, $text);
139
140
        return $image;
141
    }
142
143
    protected function setSizes(): void
144
    {
145
        $this->setWidth(imagesx($this->getImage()));
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imagesx() 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

145
        $this->setWidth(imagesx(/** @scrutinizer ignore-type */ $this->getImage()));
Loading history...
146
        $this->setHeight(imagesy($this->getImage()));
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imagesy() 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

146
        $this->setHeight(imagesy(/** @scrutinizer ignore-type */ $this->getImage()));
Loading history...
147
    }
148
149
    private function newImage(int $width, int $height)
150
    {
151
        $newimg = imagecreatetruecolor($width, $height);
152
        $transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
153
        imagefill($newimg, 0, 0, $transparent);
154
        imagealphablending($newimg, true);
155
        imagesavealpha($newimg, true);
156
157
        return $newimg;
158
    }
159
160
    public function resize(int $width, int $height): ImageInterface
161
    {
162
        $newimage = $this->newImage($width, $height);
163
        imagecopyresampled($newimage, $this->getImage(), 0, 0, 0, 0, $width, $height, $this->getWidth() , $this->getHeight());
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $src_image of imagecopyresampled() 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

163
        imagecopyresampled($newimage, /** @scrutinizer ignore-type */ $this->getImage(), 0, 0, 0, 0, $width, $height, $this->getWidth() , $this->getHeight());
Loading history...
164
        $this->setImage($newimage);
165
        $this->setSizes();
166
167
        return $this;
168
    }
169
170
    public function crop(int $width, int $height, int $x, int $y): ImageInterface
171
    {
172
        $width = $width - $x;
173
        $height = $height - $y;
174
        $newimage = $this->newImage($width, $height);
175
        imagecopyresampled($newimage, $this->getImage(), 0, 0, $x, $y, $width, $height, $width, $height);
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $src_image of imagecopyresampled() 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

175
        imagecopyresampled($newimage, /** @scrutinizer ignore-type */ $this->getImage(), 0, 0, $x, $y, $width, $height, $width, $height);
Loading history...
176
        $this->setImage($newimage);
177
        $this->setSizes();
178
179
        return $this;
180
    }
181
182
    public function watermark(): ImageInterface
183
    {
184
        return $this;
185
    }
186
187
    public function rotate(int $angle = 90): ImageInterface
188
    {
189
        $transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
190
        $rotate = imagerotate($this->getImage(), $angle, $transparent);
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imagerotate() 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

190
        $rotate = imagerotate(/** @scrutinizer ignore-type */ $this->getImage(), $angle, $transparent);
Loading history...
191
        imagealphablending($rotate, true);
192
        imagesavealpha($rotate, true);
193
        $this->setImage($rotate);
194
        $this->setSizes();
195
196
        return $this;
197
    }
198
199
    public function __toString(): string
200
    {
201
        ob_start();
202
        imagepng($this->getImage(), null, 9, PNG_ALL_FILTERS);
0 ignored issues
show
Bug introduced by
It seems like $this->getImage() can also be of type Imagick; however, parameter $image of imagepng() 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

202
        imagepng(/** @scrutinizer ignore-type */ $this->getImage(), null, 9, PNG_ALL_FILTERS);
Loading history...
203
        $temp = ob_get_contents();
204
        ob_clean();
205
206
        return trim($temp);
207
    }
208
209
    public function save(string $filename): bool
210
    {
211
        return true;
212
    }
213
}
214