1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Imagine\Filter\Loader; |
13
|
|
|
|
14
|
|
|
use Imagine\Image\Box; |
15
|
|
|
use Imagine\Image\ImageInterface; |
16
|
|
|
use Imagine\Image\ImagineInterface; |
17
|
|
|
use Imagine\Image\Point; |
18
|
|
|
|
19
|
|
|
class WatermarkFilterLoader implements LoaderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ImagineInterface |
23
|
|
|
*/ |
24
|
|
|
protected $imagine; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $projectDir; |
30
|
|
|
|
31
|
|
|
public function __construct(ImagineInterface $imagine, $projectDir) |
32
|
|
|
{ |
33
|
|
|
$this->imagine = $imagine; |
34
|
|
|
$this->projectDir = $projectDir; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see \Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load() |
39
|
|
|
* |
40
|
|
|
* @param ImageInterface $image |
41
|
|
|
* @param array $options |
42
|
|
|
* |
43
|
|
|
* @return ImageInterface|static |
44
|
|
|
*/ |
45
|
|
|
public function load(ImageInterface $image, array $options = []) |
46
|
|
|
{ |
47
|
|
|
$options += [ |
48
|
|
|
'size' => null, |
49
|
|
|
'position' => 'center', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if ('%' === mb_substr($options['size'], -1)) { |
53
|
|
|
$options['size'] = mb_substr($options['size'], 0, -1) / 100; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$watermark = $this->imagine->open($this->projectDir.'/'.$options['image']); |
57
|
|
|
|
58
|
|
|
$size = $image->getSize(); |
59
|
|
|
$watermarkSize = $watermark->getSize(); |
60
|
|
|
|
61
|
|
|
// If 'null': Downscale if needed |
62
|
|
|
if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) { |
63
|
|
|
$options['size'] = 1.0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($options['size']) { |
67
|
|
|
$factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight()); |
68
|
|
|
|
69
|
|
|
$watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor)); |
70
|
|
|
$watermarkSize = $watermark->getSize(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ('multiple' === $options['position']) { |
74
|
|
|
// we loop over the coordinates of the image to apply the watermark as much as possible |
75
|
|
|
$pasteX = 0; |
76
|
|
|
while ($pasteX < $size->getWidth()) { |
77
|
|
|
$pasteY = 0; |
78
|
|
|
while ($pasteY < $size->getHeight()) { |
79
|
|
|
$image->paste($watermark, new Point($pasteX, $pasteY)); |
80
|
|
|
$pasteY += $watermarkSize->getHeight(); |
81
|
|
|
} |
82
|
|
|
$pasteX += $watermarkSize->getWidth(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $image; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
switch ($options['position']) { |
89
|
|
|
case 'topleft': |
90
|
|
|
$x = 0; |
91
|
|
|
$y = 0; |
92
|
|
|
break; |
93
|
|
|
case 'top': |
94
|
|
|
$x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; |
95
|
|
|
$y = 0; |
96
|
|
|
break; |
97
|
|
|
case 'topright': |
98
|
|
|
$x = $size->getWidth() - $watermarkSize->getWidth(); |
99
|
|
|
$y = 0; |
100
|
|
|
break; |
101
|
|
|
case 'left': |
102
|
|
|
$x = 0; |
103
|
|
|
$y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; |
104
|
|
|
break; |
105
|
|
View Code Duplication |
case 'center': |
|
|
|
|
106
|
|
|
$x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; |
107
|
|
|
$y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; |
108
|
|
|
break; |
109
|
|
View Code Duplication |
case 'right': |
|
|
|
|
110
|
|
|
$x = $size->getWidth() - $watermarkSize->getWidth(); |
111
|
|
|
$y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; |
112
|
|
|
break; |
113
|
|
|
case 'bottomleft': |
114
|
|
|
$x = 0; |
115
|
|
|
$y = $size->getHeight() - $watermarkSize->getHeight(); |
116
|
|
|
break; |
117
|
|
View Code Duplication |
case 'bottom': |
|
|
|
|
118
|
|
|
$x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; |
119
|
|
|
$y = $size->getHeight() - $watermarkSize->getHeight(); |
120
|
|
|
break; |
121
|
|
View Code Duplication |
case 'bottomright': |
|
|
|
|
122
|
|
|
$x = $size->getWidth() - $watermarkSize->getWidth(); |
123
|
|
|
$y = $size->getHeight() - $watermarkSize->getHeight(); |
124
|
|
|
break; |
125
|
|
|
default: |
126
|
|
|
throw new \InvalidArgumentException("Unexpected position '{$options['position']}'"); |
127
|
|
|
break; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $image->paste($watermark, new Point($x, $y)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.