ImageBuilder::contrast()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jackal\ImageMerge\Builder;
4
5
use Exception;
6
use Jackal\ImageMerge\Command\Asset\ImageAssetCommand;
7
use Jackal\ImageMerge\Command\Asset\SquareAssetCommand;
8
use Jackal\ImageMerge\Command\Asset\TextAssetCommand;
9
use Jackal\ImageMerge\Command\BlurCommand;
10
use Jackal\ImageMerge\Command\BorderCommand;
11
use Jackal\ImageMerge\Command\BrightnessCommand;
12
use Jackal\ImageMerge\Command\CommandInterface;
13
use Jackal\ImageMerge\Command\ContrastCommand;
14
use Jackal\ImageMerge\Command\CropCommand;
15
use Jackal\ImageMerge\Command\CropPolygonCommand;
16
use Jackal\ImageMerge\Command\FlipHorizontalCommand;
17
use Jackal\ImageMerge\Command\FlipVerticalCommand;
18
use Jackal\ImageMerge\Command\GrayScaleCommand;
19
use Jackal\ImageMerge\Command\Options\BorderCommandOption;
20
use Jackal\ImageMerge\Command\Options\CropCommandOption;
21
use Jackal\ImageMerge\Command\Options\DimensionCommandOption;
22
use Jackal\ImageMerge\Command\Options\DoubleCoordinateColorCommandOption;
23
use Jackal\ImageMerge\Command\Options\LevelCommandOption;
24
use Jackal\ImageMerge\Command\Options\MultiCoordinateCommandOption;
25
use Jackal\ImageMerge\Command\Options\SingleCoordinateCommandOption;
26
use Jackal\ImageMerge\Command\Options\SingleCoordinateFileObjectCommandOption;
27
use Jackal\ImageMerge\Command\Options\TextCommandOption;
28
use Jackal\ImageMerge\Command\PixelCommand;
29
use Jackal\ImageMerge\Command\ResizeCommand;
30
use Jackal\ImageMerge\Command\RotateCommand;
31
use Jackal\ImageMerge\Exception\InvalidColorException;
32
use Jackal\ImageMerge\Model\Color;
33
use Jackal\ImageMerge\ValueObject\Coordinate;
34
use Jackal\ImageMerge\Model\File\FileTempObject;
35
use Jackal\ImageMerge\Model\Image;
36
use Jackal\ImageMerge\Model\Text\Text;
37
use Jackal\ImageMerge\ValueObject\Dimention;
38
39
class ImageBuilder
40
{
41
    /**
42
     * @var Image
43
     */
44
    protected $image;
45
46
    /**
47
     * ImageBuilder constructor.
48
     * @param Image $image
49
     */
50
    public function __construct(Image $image)
51
    {
52
        $this->image = $image;
53
    }
54
55
    /**
56
     * @param CommandInterface $command
57
     * @return $this
58
     */
59
    public function addCommand(CommandInterface $command)
60
    {
61
        $this->image = $command->execute($this->image);
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param $level
68
     * @return ImageBuilder
69
     */
70
    public function blur($level)
71
    {
72
        return $this->addCommand(new BlurCommand(new LevelCommandOption($level)));
73
    }
74
75
    /**
76
     * @param null $width
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $width is correct as it would always require null to be passed?
Loading history...
77
     * @param null $height
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $height is correct as it would always require null to be passed?
Loading history...
78
     * @return ImageBuilder
79
     */
80
    public function resize($width = null, $height = null)
81
    {
82
        return $this->addCommand(new ResizeCommand(new DimensionCommandOption(new Dimention($width, $height))));
83
    }
84
85
    /**
86
     * @param $degree
87
     * @return ImageBuilder
88
     */
89
    public function rotate($degree)
90
    {
91
        return $this->addCommand(new RotateCommand(new LevelCommandOption($degree)));
92
    }
93
94
    /**
95
     * @return ImageBuilder
96
     */
97
    public function flipVertical()
98
    {
99
        return $this->addCommand(new FlipVerticalCommand());
100
    }
101
102
    /**
103
     * @return ImageBuilder
104
     */
105
    public function flipHorizontal()
106
    {
107
        return $this->addCommand(new FlipHorizontalCommand());
108
    }
109
110
    /**
111
     * @param Text $text
112
     * @param $x1
113
     * @param $y1
114
     * @return ImageBuilder
115
     */
116
    public function addText(Text $text, $x1, $y1)
117
    {
118
        return $this->addCommand(new TextAssetCommand(new TextCommandOption($text, new Coordinate($x1, $y1))));
119
    }
120
121
    /**
122
     * @param $x1
123
     * @param $y1
124
     * @param $x2
125
     * @param $y2
126
     * @param string $colorHex
127
     * @return ImageBuilder
128
     * @throws InvalidColorException
129
     */
130
    public function addSquare($x1, $y1, $x2, $y2, $colorHex = COLOR::BLACK)
131
    {
132
        return $this->addCommand(new SquareAssetCommand(
133
            new DoubleCoordinateColorCommandOption(
134
                new Coordinate($x1, $y1),
135
                new Coordinate($x2, $y2),
136
                new Color($colorHex))
137
        ));
138
    }
139
140
    /**
141
     * @param Image $image
142
     * @param int $x
143
     * @param int $y
144
     * @return ImageBuilder
145
     * @throws Exception
146
     */
147
    public function merge(Image $image, $x = 0, $y = 0)
148
    {
149
        $fileObject = FileTempObject::fromString($image->toPNG()->getContent());
150
151
        return $this->addCommand(
152
            new ImageAssetCommand(
153
                new SingleCoordinateFileObjectCommandOption($fileObject, new Coordinate($x, $y))
154
            )
155
        );
156
    }
157
158
    /**
159
     * @param $level
160
     * @return ImageBuilder
161
     */
162
    public function pixelate($level)
163
    {
164
        return $this->addCommand(new PixelCommand(new LevelCommandOption($level)));
165
    }
166
167
    /**
168
     * @param $stroke
169
     * @param string $colorHex
170
     * @return ImageBuilder
171
     * @throws InvalidColorException
172
     */
173
    public function border($stroke, $colorHex = Color::WHITE)
174
    {
175
        return $this->addCommand(new BorderCommand(new BorderCommandOption($stroke, new Color($colorHex))));
176
    }
177
178
    /**
179
     * @param $newWidth
180
     * @param $newHeight
181
     * @return ImageBuilder
182
     */
183
    public function cropCenter($newWidth, $newHeight)
184
    {
185
        $width = $this->image->getWidth();
186
        $height = $this->image->getHeight();
187
188
        $x = round(($width - $newWidth) / 2);
189
        $y = round(($height - $newHeight) / 2);
190
191
        return $this->crop($x, $y, $newWidth, $newHeight);
192
    }
193
194
    /**
195
     * @param $level
196
     * @return ImageBuilder
197
     */
198
    public function brightness($level)
199
    {
200
        return $this->addCommand(new BrightnessCommand(new LevelCommandOption($level)));
201
    }
202
203
    /**
204
     * @param $x
205
     * @param $y
206
     * @param $width
207
     * @param $height
208
     * @return ImageBuilder
209
     */
210
    public function crop($x, $y, $width, $height)
211
    {
212
        return $this->addCommand(
213
            new CropCommand(
214
                new CropCommandOption(
215
                    new Coordinate($x, $y),
216
                    new Dimention($width, $height)
217
                )
218
            )
219
        );
220
    }
221
222
    /**
223
     * @param $x1
224
     * @param $y1
225
     * @param $x2
226
     * @param $y2
227
     * @param $x3
228
     * @param $y3
229
     * @return ImageBuilder
230
     */
231
    public function cropPolygon($x1, $y1, $x2, $y2, $x3, $y3)
232
    {
233
        $points = func_get_args();
234
        $coords = [];
235
236
        foreach ($points as $k => $point) {
237
            if ($k == 0 or ($k % 2) == 0) {
238
                if (isset($points[$k + 1])) {
239
                    $x = $point;
240
                    $y = $points[$k + 1];
241
                    $coords[] = new SingleCoordinateCommandOption(new Coordinate($x, $y));
242
                }
243
            }
244
        }
245
246
        return $this->addCommand(new CropPolygonCommand(
247
            new MultiCoordinateCommandOption($coords)
248
        ));
249
    }
250
251
    /**
252
     * @param null $width
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $width is correct as it would always require null to be passed?
Loading history...
253
     * @param null $height
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $height is correct as it would always require null to be passed?
Loading history...
254
     * @return $this
255
     */
256
    public function thumbnail($width = null, $height = null)
257
    {
258
        /** @var DimensionCommandOption $options */
259
        $options = new DimensionCommandOption(new Dimention($width, $height));
260
261
        if (!$options->getDimention()->getWidth()) {
262
            $options->add('width', round($this->image->getAspectRatio() * $options->getDimention()->getHeight()));
263
        }
264
265
        if (!$options->getDimention()->getHeight()) {
266
            $options->add('height', round($options->getDimention()->getWidth() / $this->image->getAspectRatio()));
267
        }
268
269
        $thumbAspect = $options->getDimention()->getWidth() / $options->getDimention()->getHeight();
270
271
        if ($this->image->getAspectRatio() >= $thumbAspect) {
272
            // If image is wider than thumbnail (in aspect ratio sense)
273
            $newHeight = $options->getDimention()->getHeight();
274
            $newWidth = round($this->image->getWidth() / ($this->image->getHeight() / $options->getDimention()->getHeight()));
275
        } else {
276
            // If the thumbnail is wider than the image
277
            $newHeight = round($this->image->getHeight() / ($this->image->getWidth() / $options->getDimention()->getWidth()));
278
            $newWidth = $options->getDimention()->getWidth();
279
        }
280
281
        $this->resize($newWidth, $newHeight);
282
        $this->cropCenter($options->getDimention()->getWidth(), $options->getDimention()->getHeight());
283
284
        return $this;
285
    }
286
287
    /**
288
     * @return ImageBuilder
289
     */
290
    public function grayScale()
291
    {
292
        return $this->addCommand(new GrayScaleCommand());
293
    }
294
295
    /**
296
     * @param $level
297
     * @return ImageBuilder
298
     */
299
    public function contrast($level)
300
    {
301
        return $this->addCommand(new ContrastCommand(new LevelCommandOption($level)));
302
    }
303
304
    /**
305
     * @return Image
306
     */
307
    public function getImage()
308
    {
309
        return $this->image;
310
    }
311
}
312