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 BackgroundFilterLoader implements LoaderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ImagineInterface |
23
|
|
|
*/ |
24
|
|
|
protected $imagine; |
25
|
|
|
|
26
|
|
|
public function __construct(ImagineInterface $imagine) |
27
|
|
|
{ |
28
|
|
|
$this->imagine = $imagine; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function load(ImageInterface $image, array $options = []) |
35
|
|
|
{ |
36
|
|
|
$background = $image->palette()->color( |
37
|
|
|
isset($options['color']) ? $options['color'] : '#fff', |
38
|
|
|
isset($options['transparency']) ? $options['transparency'] : null |
39
|
|
|
); |
40
|
|
|
$topLeft = new Point(0, 0); |
41
|
|
|
$size = $image->getSize(); |
42
|
|
|
|
43
|
|
|
if (isset($options['size'])) { |
44
|
|
|
$width = isset($options['size'][0]) ? $options['size'][0] : null; |
45
|
|
|
$height = isset($options['size'][1]) ? $options['size'][1] : null; |
46
|
|
|
|
47
|
|
|
$position = isset($options['position']) ? $options['position'] : 'center'; |
48
|
|
|
switch ($position) { |
49
|
|
|
case 'topleft': |
50
|
|
|
$x = 0; |
51
|
|
|
$y = 0; |
52
|
|
|
break; |
53
|
|
|
case 'top': |
54
|
|
|
$x = ($width - $image->getSize()->getWidth()) / 2; |
55
|
|
|
$y = 0; |
56
|
|
|
break; |
57
|
|
|
case 'topright': |
58
|
|
|
$x = $width - $image->getSize()->getWidth(); |
59
|
|
|
$y = 0; |
60
|
|
|
break; |
61
|
|
|
case 'left': |
62
|
|
|
$x = 0; |
63
|
|
|
$y = ($height - $image->getSize()->getHeight()) / 2; |
64
|
|
|
break; |
65
|
|
|
case 'centerright': |
66
|
|
|
$x = $width - $image->getSize()->getWidth(); |
67
|
|
|
$y = ($height - $image->getSize()->getHeight()) / 2; |
68
|
|
|
break; |
69
|
|
View Code Duplication |
case 'center': |
|
|
|
|
70
|
|
|
$x = ($width - $image->getSize()->getWidth()) / 2; |
71
|
|
|
$y = ($height - $image->getSize()->getHeight()) / 2; |
72
|
|
|
break; |
73
|
|
|
case 'centerleft': |
74
|
|
|
$x = 0; |
75
|
|
|
$y = ($height - $image->getSize()->getHeight()) / 2; |
76
|
|
|
break; |
77
|
|
|
case 'right': |
78
|
|
|
$x = $width - $image->getSize()->getWidth(); |
79
|
|
|
$y = ($height - $image->getSize()->getHeight()) / 2; |
80
|
|
|
break; |
81
|
|
|
case 'bottomleft': |
82
|
|
|
$x = 0; |
83
|
|
|
$y = $height - $image->getSize()->getHeight(); |
84
|
|
|
break; |
85
|
|
|
case 'bottom': |
86
|
|
|
$x = ($width - $image->getSize()->getWidth()) / 2; |
87
|
|
|
$y = $height - $image->getSize()->getHeight(); |
88
|
|
|
break; |
89
|
|
|
case 'bottomright': |
90
|
|
|
$x = $width - $image->getSize()->getWidth(); |
91
|
|
|
$y = $height - $image->getSize()->getHeight(); |
92
|
|
|
break; |
93
|
|
|
default: |
94
|
|
|
throw new \InvalidArgumentException("Unexpected position '{$position}'"); |
95
|
|
|
break; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$size = new Box($width, $height); |
99
|
|
|
$topLeft = new Point($x, $y); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$canvas = $this->imagine->create($size, $background); |
103
|
|
|
|
104
|
|
|
return $canvas->paste($image, $topLeft); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.