1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Imagine\Filter\Loader; |
4
|
|
|
|
5
|
|
|
use Imagine\Image\Box; |
6
|
|
|
use Imagine\Image\Point; |
7
|
|
|
use Imagine\Image\ImageInterface; |
8
|
|
|
use Imagine\Image\ImagineInterface; |
9
|
|
|
|
10
|
|
|
class WatermarkFilterLoader implements LoaderInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ImagineInterface |
14
|
|
|
*/ |
15
|
|
|
protected $imagine; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $rootPath; |
21
|
|
|
|
22
|
|
|
public function __construct(ImagineInterface $imagine, $rootPath) |
23
|
|
|
{ |
24
|
|
|
$this->imagine = $imagine; |
25
|
|
|
$this->rootPath = $rootPath; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load() |
30
|
|
|
*/ |
31
|
|
|
public function load(ImageInterface $image, array $options = array()) |
32
|
|
|
{ |
33
|
|
|
$options += array( |
34
|
|
|
'size' => null, |
35
|
|
|
'position' => 'center', |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
if (substr($options['size'], -1) == '%') { |
39
|
|
|
$options['size'] = substr($options['size'], 0, -1) / 100; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$watermark = $this->imagine->open($this->rootPath.'/'.$options['image']); |
43
|
|
|
|
44
|
|
|
$size = $image->getSize(); |
45
|
|
|
$watermarkSize = $watermark->getSize(); |
46
|
|
|
|
47
|
|
|
// If 'null': Downscale if needed |
48
|
|
|
if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) { |
49
|
|
|
$options['size'] = 1.0; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($options['size']) { |
53
|
|
|
$factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight()); |
54
|
|
|
|
55
|
|
|
$watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor)); |
56
|
|
|
$watermarkSize = $watermark->getSize(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
switch ($options['position']) { |
60
|
|
|
case 'topleft': |
61
|
|
|
$x = 0; |
62
|
|
|
$y = 0; |
63
|
|
|
break; |
64
|
|
|
case 'top': |
65
|
|
|
$x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; |
66
|
|
|
$y = 0; |
67
|
|
|
break; |
68
|
|
|
case 'topright': |
69
|
|
|
$x = $size->getWidth() - $watermarkSize->getWidth(); |
70
|
|
|
$y = 0; |
71
|
|
|
break; |
72
|
|
|
case 'left': |
73
|
|
|
$x = 0; |
74
|
|
|
$y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; |
75
|
|
|
break; |
76
|
|
View Code Duplication |
case 'center': |
|
|
|
|
77
|
|
|
$x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; |
78
|
|
|
$y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; |
79
|
|
|
break; |
80
|
|
View Code Duplication |
case 'right': |
|
|
|
|
81
|
|
|
$x = $size->getWidth() - $watermarkSize->getWidth(); |
82
|
|
|
$y = ($size->getHeight() - $watermarkSize->getHeight()) / 2; |
83
|
|
|
break; |
84
|
|
|
case 'bottomleft': |
85
|
|
|
$x = 0; |
86
|
|
|
$y = $size->getHeight() - $watermarkSize->getHeight(); |
87
|
|
|
break; |
88
|
|
View Code Duplication |
case 'bottom': |
|
|
|
|
89
|
|
|
$x = ($size->getWidth() - $watermarkSize->getWidth()) / 2; |
90
|
|
|
$y = $size->getHeight() - $watermarkSize->getHeight(); |
91
|
|
|
break; |
92
|
|
View Code Duplication |
case 'bottomright': |
|
|
|
|
93
|
|
|
$x = $size->getWidth() - $watermarkSize->getWidth(); |
94
|
|
|
$y = $size->getHeight() - $watermarkSize->getHeight(); |
95
|
|
|
break; |
96
|
|
|
default: |
97
|
|
|
throw new \InvalidArgumentException("Unexpected position '{$options['position']}'"); |
98
|
|
|
break; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $image->paste($watermark, new Point($x, $y)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.