Completed
Push — master ( 0a7932...aee286 )
by Maksim
13s
created

BackgroundFilterLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 5
c 3
b 2
f 1
lcom 1
cbo 6
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 20 4
1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Filter\Loader;
4
5
use Imagine\Image\Box;
6
use Imagine\Image\ImageInterface;
7
use Imagine\Image\ImagineInterface;
8
use Imagine\Image\Point;
9
10
class BackgroundFilterLoader implements LoaderInterface
11
{
12
    /**
13
     * @var ImagineInterface
14
     */
15
    protected $imagine;
16
17
    public function __construct(ImagineInterface $imagine)
18
    {
19
        $this->imagine = $imagine;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function load(ImageInterface $image, array $options = array())
26
    {
27
        $background = $image->palette()->color(
28
            isset($options['color']) ? $options['color'] : '#fff',
29
            isset($options['transparency']) ? $options['transparency'] : null
30
        );
31
        $topLeft = new Point(0, 0);
32
        $size = $image->getSize();
33
34
        if (isset($options['size'])) {
35
            list($width, $height) = $options['size'];
36
37
            $size = new Box($width, $height);
38
            $topLeft = new Point(($width - $image->getSize()->getWidth()) / 2, ($height - $image->getSize()->getHeight()) / 2);
39
        }
40
41
        $canvas = $this->imagine->create($size, $background);
42
43
        return $canvas->paste($image, $topLeft);
44
    }
45
}
46