Completed
Pull Request — master (#1163)
by Grégoire
02:51
created

SquareResizer::getAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Resizer;
13
14
use Gaufrette\File;
15
use Imagine\Image\Box;
16
use Imagine\Image\ImagineInterface;
17
use Imagine\Image\Point;
18
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
19
use Sonata\MediaBundle\Model\MediaInterface;
20
21
/**
22
 * This reziser crop the image when the width and height are specified.
23
 * Every time you specify the W and H, the script generate a square with the
24
 * smaller size. For example, if width is 100 and height 80; the generated image
25
 * will be 80x80.
26
 *
27
 * @author Edwin Ibarra <[email protected]>
28
 */
29
class SquareResizer implements ResizerInterface
30
{
31
    /**
32
     * @var ImagineInterface
33
     */
34
    protected $adapter;
35
36
    /**
37
     * @var string
38
     */
39
    protected $mode;
40
41
    /**
42
     * @param ImagineInterface         $adapter
43
     * @param string                   $mode
44
     * @param MetadataBuilderInterface $metadata
45
     */
46
    public function __construct(ImagineInterface $adapter, $mode, MetadataBuilderInterface $metadata)
47
    {
48
        $this->adapter = $adapter;
49
        $this->mode = $mode;
50
        $this->metadata = $metadata;
0 ignored issues
show
Bug introduced by
The property metadata does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getAdapter()
57
    {
58
        return $this->adapter;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function setAdapter(ImagineInterface $adapter)
65
    {
66
        $this->adapter = $adapter;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function resize(MediaInterface $media, File $in, File $out, $format, array $settings)
73
    {
74
        if (!isset($settings['width'])) {
75
            throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderName()));
76
        }
77
78
        $image = $this->adapter->load($in->getContent());
79
        $size = $media->getBox();
80
81
        if (null != $settings['height']) {
82
            if ($size->getHeight() > $size->getWidth()) {
83
                $higher = $size->getHeight();
84
                $lower = $size->getWidth();
85
            } else {
86
                $higher = $size->getWidth();
87
                $lower = $size->getHeight();
88
            }
89
90
            $crop = $higher - $lower;
91
92
            if ($crop > 0) {
93
                $point = $higher == $size->getHeight() ? new Point(0, 0) : new Point($crop / 2, 0);
94
                $image->crop($point, new Box($lower, $lower));
95
                $size = $image->getSize();
96
            }
97
        }
98
99
        $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
100
101
        if ($settings['height'] < $size->getHeight() && $settings['width'] < $size->getWidth()) {
102
            $content = $image
103
                ->thumbnail(new Box($settings['width'], $settings['height']), $this->mode)
104
                ->get($format, array('quality' => $settings['quality']));
105
        } else {
106
            $content = $image->get($format, array('quality' => $settings['quality']));
107
        }
108
109
        $out->setContent($content, $this->metadata->get($media, $out->getName()));
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getBox(MediaInterface $media, array $settings)
116
    {
117
        $size = $media->getBox();
118
119
        if (null != $settings['height']) {
120
            if ($size->getHeight() > $size->getWidth()) {
121
                $higher = $size->getHeight();
122
                $lower = $size->getWidth();
123
            } else {
124
                $higher = $size->getWidth();
125
                $lower = $size->getHeight();
126
            }
127
128
            if ($higher - $lower > 0) {
129
                return new Box($lower, $lower);
130
            }
131
        }
132
133
        $settings['height'] = (int) ($settings['width'] * $size->getHeight() / $size->getWidth());
134
135
        if ($settings['height'] < $size->getHeight() && $settings['width'] < $size->getWidth()) {
136
            return new Box($settings['width'], $settings['height']);
137
        }
138
139
        return $size;
140
    }
141
}
142