EffectBlurCentered::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 22
c 2
b 1
f 1
nc 4
nop 1
dl 0
loc 38
rs 9.568
1
<?php
2
3
namespace Jackal\ImageMerge\Command\Effect;
4
5
use Exception;
6
use Jackal\ImageMerge\Builder\ImageBuilder;
7
use Jackal\ImageMerge\Command\AbstractCommand;
8
use Jackal\ImageMerge\Command\Options\DimensionCommandOption;
9
use Jackal\ImageMerge\Exception\InvalidColorException;
10
use Jackal\ImageMerge\Model\Color;
11
use Jackal\ImageMerge\Model\File\FileTempObject;
12
use Jackal\ImageMerge\Model\Image;
13
14
/**
15
 * Class EffectBlurCentered
16
 * @package Jackal\ImageMerge\Command\Effect
17
 */
18
class EffectBlurCentered extends AbstractCommand
19
{
20
    /**
21
     * EffectBlurCentered constructor.
22
     * @param DimensionCommandOption $options
23
     */
24
    public function __construct(DimensionCommandOption $options)
25
    {
26
        parent::__construct($options);
27
    }
28
29
    /**
30
     * @param Image $image
31
     * @return Image
32
     * @throws InvalidColorException
33
     */
34
    public function execute(Image $image)
35
    {
36
        /** @var DimensionCommandOption $options */
37
        $options = $this->options;
38
39
        $builder = new ImageBuilder($image);
40
41
        $originalWidth = $image->getWidth();
42
        $originalHeight = $image->getHeight();
43
44
        if ($originalHeight > $options->getDimention()->getHeight()) {
45
            $builder->thumbnail(null, $options->getDimention()->getHeight() - 4);
46
            $originalWidth = $image->getWidth();
47
            $originalHeight = $image->getHeight();
48
        }
49
50
        if ($originalWidth > $options->getDimention()->getWidth()) {
51
            $builder->thumbnail($options->getDimention()->getWidth() - 4, null);
52
            $originalWidth = $image->getWidth();
53
            $originalHeight = $image->getHeight();
54
        }
55
56
        $originalImg = $this->saveImage($image);
57
58
        $builder->resize($options->getDimention()->getWidth(), $options->getDimention()->getHeight());
59
        $builder->blur(40);
60
        $builder->brightness(-70);
61
62
        $x = round(($options->getDimention()->getWidth() - $originalWidth) / 2);
63
        $y = round(($options->getDimention()->getHeight() - $originalHeight) / 2);
64
65
        $borderColor = Color::WHITE;
66
67
        $builder->addSquare($x - 1, $y - 1, $x + $originalWidth, $y + $originalHeight, $borderColor);
68
69
        $builder->merge(Image::fromFile($originalImg), $x, $y);
0 ignored issues
show
Bug introduced by
$y of type double is incompatible with the type integer expected by parameter $y of Jackal\ImageMerge\Builder\ImageBuilder::merge(). ( Ignorable by Annotation )

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

69
        $builder->merge(Image::fromFile($originalImg), $x, /** @scrutinizer ignore-type */ $y);
Loading history...
Bug introduced by
$x of type double is incompatible with the type integer expected by parameter $x of Jackal\ImageMerge\Builder\ImageBuilder::merge(). ( Ignorable by Annotation )

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

69
        $builder->merge(Image::fromFile($originalImg), /** @scrutinizer ignore-type */ $x, $y);
Loading history...
70
71
        return $builder->getImage();
72
    }
73
74
    /**
75
     * @param Image $image
76
     * @return FileTempObject
77
     * @throws Exception
78
     */
79
    private function saveImage(Image $image)
80
    {
81
        return FileTempObject::fromString($image->toPNG()->getContent());
82
    }
83
}
84