1
|
|
|
<?php |
2
|
|
|
namespace HtImgModule\Imagine\Filter; |
3
|
|
|
|
4
|
|
|
use Imagine\Filter\FilterInterface; |
5
|
|
|
use Imagine\Image\ImageInterface; |
6
|
|
|
use Imagine\Image\Point; |
7
|
|
|
use HtImgModule\Exception; |
8
|
|
|
|
9
|
|
|
class Paste implements FilterInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Imagine\Image\ImageInterface |
13
|
|
|
*/ |
14
|
|
|
protected $pasteImage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string|integer |
18
|
|
|
*/ |
19
|
|
|
protected $x; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|integer |
23
|
|
|
*/ |
24
|
|
|
protected $y; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \Imagine\Image\ImageInterface $pasteImage Image to be pasted |
28
|
|
|
* @param string|int $x x-coordinage|position of left corner |
29
|
|
|
* @param string|int $y y-coordinage|position of left corner |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct(ImageInterface $pasteImage, $x, $y) |
32
|
|
|
{ |
33
|
6 |
|
$this->throwIfPointNotValid($x, 'x', ['left', 'right', 'center']); |
34
|
4 |
|
$this->throwIfPointNotValid($y, 'y', ['top', 'bottom', 'middle']); |
35
|
|
|
|
36
|
3 |
|
$this->pasteImage = $pasteImage; |
37
|
3 |
|
$this->x = $x; |
38
|
3 |
|
$this->y = $y; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
2 |
|
public function apply(ImageInterface $image) |
45
|
|
|
{ |
46
|
2 |
|
$x = is_string($this->x) |
47
|
2 |
|
? $this->stringXtoInteger($this->x, $this->pasteImage, $image) |
48
|
2 |
|
: $this->x |
49
|
|
|
; |
50
|
|
|
|
51
|
2 |
|
$y = is_string($this->y) |
52
|
2 |
|
? $this->stringYtoInteger($this->y, $this->pasteImage, $image) |
53
|
2 |
|
: $this->y |
54
|
|
|
; |
55
|
|
|
|
56
|
2 |
|
return $image->paste($this->pasteImage, new Point($x, $y)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $point |
61
|
|
|
* @param \Imagine\Image\ImageInterface $pasteImage |
62
|
|
|
* @param \Imagine\Image\ImageInterface $image |
63
|
|
|
* |
64
|
|
|
* @return integer |
65
|
|
|
*/ |
66
|
2 |
|
protected function stringXtoInteger($point, ImageInterface $pasteImage, ImageInterface $image) |
67
|
|
|
{ |
68
|
|
|
switch ($point) { |
69
|
2 |
|
case 'right': |
70
|
1 |
|
return (integer) $image->getSize()->getWidth() - $pasteImage->getSize()->getWidth(); |
71
|
1 |
|
case 'center': |
72
|
1 |
|
return (integer) round( ($image->getSize()->getWidth() / 2) - ($pasteImage->getSize()->getWidth() / 2) ); |
73
|
1 |
|
case 'left': |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $point |
79
|
|
|
* @param \Imagine\Image\ImageInterface $pasteImage |
80
|
|
|
* @param \Imagine\Image\ImageInterface $image |
81
|
|
|
* |
82
|
|
|
* @return integer |
83
|
|
|
*/ |
84
|
2 |
|
protected function stringYtoInteger($point, ImageInterface $pasteImage, ImageInterface $image) |
85
|
|
|
{ |
86
|
|
|
switch ($point) { |
87
|
2 |
|
case 'bottom': |
88
|
1 |
|
return (integer) $image->getSize()->getHeight() - $pasteImage->getSize()->getHeight(); |
89
|
2 |
|
case 'middle': |
90
|
1 |
|
return (integer) round( ($image->getSize()->getHeight() / 2) - ($pasteImage->getSize()->getHeight() / 2) ); |
91
|
1 |
|
case 'top': |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param integer|string $point |
97
|
|
|
* @param string $pointName |
98
|
|
|
* @param array $allowedStringValues |
99
|
|
|
* |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
6 |
|
protected function throwIfPointNotValid($point, $pointName, array $allowedStringValues) |
103
|
|
|
{ |
104
|
6 |
|
if (is_string($point) && in_array($point, $allowedStringValues)) { |
105
|
2 |
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
if (is_integer($point) && $point >= 0) { |
109
|
3 |
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
throw new Exception\InvalidArgumentException(sprintf( |
113
|
3 |
|
'Expected "%s" one of the [%s] or integer greater than zero', |
114
|
3 |
|
$pointName, |
115
|
3 |
|
implode('|', $allowedStringValues) |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|