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

BackgroundFilterLoader::load()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 12
nc 2
nop 2
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