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\Filter\Basic\Crop; |
15
|
|
|
use Imagine\Filter\Basic\Resize; |
16
|
|
|
use Imagine\Image\Box; |
17
|
|
|
use Imagine\Image\ImageInterface; |
18
|
|
|
use Imagine\Image\Point; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Fixed size filter. |
23
|
|
|
* |
24
|
|
|
* @author Robbe Clerckx <https://github.com/robbeman> |
25
|
|
|
*/ |
26
|
|
|
class FixedFilterLoader implements LoaderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param ImageInterface $image |
30
|
|
|
* @param array $options |
31
|
|
|
* |
32
|
|
|
* @return ImageInterface |
33
|
|
|
*/ |
34
|
|
|
public function load(ImageInterface $image, array $options = array()) |
35
|
|
|
{ |
36
|
|
|
$optionsResolver = new OptionsResolver(); |
37
|
|
|
$optionsResolver->setRequired(array('width', 'height')); |
38
|
|
|
$options = $optionsResolver->resolve($options); |
39
|
|
|
|
40
|
|
|
// get the original image size and create a crop box |
41
|
|
|
$size = $image->getSize(); |
42
|
|
|
$box = new Box($options['width'], $options['height']); |
43
|
|
|
|
44
|
|
|
// determine scale |
45
|
|
|
if ($size->getWidth() / $size->getHeight() > $box->getWidth() / $box->getHeight()) { |
46
|
|
|
$size = $size->heighten($box->getHeight()); |
47
|
|
|
} else { |
48
|
|
|
$size = $size->widen($box->getWidth()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// define filters |
52
|
|
|
$resize = new Resize($size); |
53
|
|
|
$origin = new Point( |
54
|
|
|
floor(($size->getWidth() - $box->getWidth()) / 2), |
55
|
|
|
floor(($size->getHeight() - $box->getHeight()) / 2) |
56
|
|
|
); |
57
|
|
|
$crop = new Crop($origin, $box); |
58
|
|
|
|
59
|
|
|
// apply filters to image |
60
|
|
|
$image = $resize->apply($image); |
61
|
|
|
$image = $crop->apply($image); |
62
|
|
|
|
63
|
|
|
return $image; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|