Completed
Push — master ( 9873f2...bcf436 )
by Kanstantsin
02:30
created

Watermark::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace tkanstantsin\fileupload\formatter\adapter;
4
5
use Imagine\Image\ImageInterface;
6
use Imagine\Imagick\Imagine;
7
use tkanstantsin\fileupload\config\InvalidConfigException;
8
use tkanstantsin\fileupload\model\BaseObject;
9
use tkanstantsin\fileupload\model\IFile;
10
11
/**
12
 * Class Watermark
13
 */
14
class Watermark extends BaseObject implements IFormatAdapter
15
{
16
    public const POSITION_CENTER_CENTER = 'center_center';
17
    public const POSITION_CENTER_LEFT = 'center_left';
18
    public const POSITION_CENTER_RIGHT = 'center_right';
19
    public const POSITION_TOP_CENTER = 'top_center';
20
    public const POSITION_TOP_LEFT = 'top_left';
21
    public const POSITION_TOP_RIGHT = 'top_right';
22
    public const POSITION_BOTTOM_CENTER = 'bottom_center';
23
    public const POSITION_BOTTOM_LEFT = 'bottom_left';
24
    public const POSITION_BOTTOM_RIGHT = 'bottom_right';
25
26
    public const SIZE_COVER = 'cover';
27
    public const SIZE_CONTAIN = 'contain';
28
    public const SIZE_STRETCH = 'stretch';
29
30
    /**
31
     * Absolute path to image file
32
     * @var string|null
33
     */
34
    public $markFilepath;
35
    /**
36
     * Water mark image binary
37
     * @var string|resource|null
38
     */
39
    public $markContent;
40
41
    /**
42
     * Opacity for watermark image
43
     * @var int
44
     */
45
    public $opacity = 100;
46
    /**
47
     * @var string
48
     */
49
    public $position = self::POSITION_CENTER_CENTER;
50
    /**
51
     * @var string
52
     */
53
    public $size = self::SIZE_CONTAIN;
54
55
    /**
56
     * @inheritdoc
57
     * @throws \tkanstantsin\fileupload\config\InvalidConfigException
58
     */
59
    public function init(): void
60
    {
61
        parent::init();
62
63
        if ($this->markFilepath === null && $this->markContent === null) {
64
            throw new InvalidConfigException('Either watermarkPath or watermarkImage must be defined.');
65
        }
66
    }
67
68
    /**
69
     * Applies filters or something to content and return it
70
     *
71
     * @param IFile $file
72
     * @param       $content
73
     *
74
     * @return mixed
75
     * @throws \Imagine\Exception\RuntimeException
76
     * @throws \ImageOptimizer\Exception\Exception
77
     */
78
    public function exec(IFile $file, $content)
79
    {
80
        $imagine = new Imagine();
81
82
        $image = $imagine->read($content);
83
        $imageSize = $image->getSize();
0 ignored issues
show
Unused Code introduced by
The assignment to $imageSize is dead and can be removed.
Loading history...
84
85
        $watermark = $this->getWatermark($imagine);
86
        $watermarkSize = $watermark->getSize();
0 ignored issues
show
Unused Code introduced by
The assignment to $watermarkSize is dead and can be removed.
Loading history...
87
88
        // TODO: implement watermark adapter.
89
90
        return $content;
91
    }
92
93
    private function getWatermark(Imagine $imagine): ImageInterface
94
    {
95
        $resource = null;
96
        if ($this->markFilepath !== null) {
97
            $resource = fopen($this->markFilepath, 'rb');
98
        } elseif (\is_string($this->markContent)) {
99
            $resource = 'a'; // TODO: create resource.
100
        } elseif (\is_resource($this->markContent)) {
101
            $resource = $this->markContent;
102
        }
103
104
        if ($resource === null) {
105
            throw new \UnexpectedValueException('Water mark image invalid format');
106
        }
107
108
        return $imagine->read($resource);
109
    }
110
}