|
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(); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$watermark = $this->getWatermark($imagine); |
|
86
|
|
|
$watermarkSize = $watermark->getSize(); |
|
|
|
|
|
|
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
|
|
|
} |