/
Resize.php
182 lines (167 loc) · 5.94 KB
/
Resize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Munee: Optimising Your Assets
*
* @copyright Cody Lundquist 2013
* @license http://opensource.org/licenses/mit-license.php
*/
namespace Munee\Asset\Filter\Image;
use Munee\Asset\Filter;
use Munee\ErrorException;
use Imagine\Image\ImageInterface;
use Imagine\Image\Box;
use Imagine\Image\Color;
use Imagine\Image\Point;
/**
* Resize Filter to resize/crop/fill/stretch images
*
* @author Cody Lundquist
*/
class Resize extends Filter
{
/**
* List of allowed params for this particular filter
*
* @var array
*/
protected $allowedParams = array(
'resize' => array(
'arguments' => array(
'width' => array(
'alias' => 'w',
'regex' => '\d+',
'cast' => 'integer'
),
'height' => array(
'alias' => 'h',
'regex' => '\d+',
'cast' => 'integer'
),
'quality' => array(
'alias' => array('q', 'qlty'),
'regex' => '\d{1,2}(?!\d)|100',
'default' => 75,
'cast' => 'integer'
),
'exact' => array(
'alias' => 'e',
'regex' => 'true|false|t|f|yes|no|y|n',
'default' => 'false',
'cast' => 'boolean'
),
'stretch' => array(
'alias' => 's',
'regex' => 'true|false|t|f|yes|no|y|n',
'default' => 'false',
'cast' => 'boolean'
),
'fill' => array(
'alias' => 'f',
'regex' => 'true|false|t|f|yes|no|y|n',
'default' => 'false',
'cast' => 'boolean'
),
'fillColour' => array(
'alias' => array(
'fc',
'fillColor',
'fillcolor',
'fill_color',
'fill-color',
'fillcolour',
'fill_colour',
'fill-colour'
),
'regex' => '[A-Fa-f0-9]{3}$|^[A-Fa-f0-9]{6}',
'default' => 'ffffff',
'cast' => 'string'
)
)
)
);
/**
* Use Imagine to resize an image and return it's new path
*
* @param string $originalImage
* @param array $arguments
* @param array $imageOptions
*
* @return void
*
* @throws ErrorException
*/
public function doFilter($originalImage, $arguments, $imageOptions)
{
// Need at least a height or a width
if (empty($arguments['height']) && empty($arguments['width'])) {
throw new ErrorException('You must set at least the height (h) or the width (w)');
}
switch (strtolower($imageOptions['imageProcessor'])) {
case 'gd':
$Imagine = new \Imagine\Gd\Imagine();
break;
case 'imagick':
$Imagine = new \Imagine\Imagick\Imagine();
break;
case 'gmagick':
$Imagine = new \Imagine\Gmagick\Imagine();
break;
default:
throw new ErrorException('Unsupported imageProcessor config value: ' . $imageOptions['imageProcessor']);
}
$image = $Imagine->open($originalImage);
$size = $image->getSize();
$originalWidth = $size->getWidth();
$originalHeight = $size->getHeight();
$width = $originalWidth;
$height = $originalHeight;
if (! empty($arguments['height'])) {
if ($originalHeight > $arguments['height'] || $arguments['stretch']) {
$height = $arguments['height'];
}
}
if (! empty($arguments['width'])) {
if ($originalWidth > $arguments['width'] || $arguments['stretch']) {
$width = $arguments['width'];
}
}
/**
* Prevent from someone from creating huge images
*/
if ($width > $imageOptions['maxAllowedResizeWidth']) {
$width = $imageOptions['maxAllowedResizeWidth'];
}
if ($height > $imageOptions['maxAllowedResizeHeight']) {
$height = $imageOptions['maxAllowedResizeHeight'];
}
$mode = $arguments['exact'] ?
ImageInterface::THUMBNAIL_OUTBOUND :
ImageInterface::THUMBNAIL_INSET;
$newSize = new Box($width, $height);
$newImage = $image->thumbnail($newSize, $mode);
if ($arguments['fill']) {
$adjustedSize = $newImage->getSize();
$canvasWidth = isset($arguments['width']) ? $arguments['width'] : $adjustedSize->getWidth();
$canvasHeight = isset($arguments['height']) ? $arguments['height'] : $adjustedSize->getHeight();
/**
* Prevent from someone from creating huge images
*/
if ($canvasWidth > $imageOptions['maxAllowedResizeWidth']) {
$canvasWidth = $imageOptions['maxAllowedResizeWidth'];
}
if ($canvasHeight > $imageOptions['maxAllowedResizeHeight']) {
$canvasHeight = $imageOptions['maxAllowedResizeHeight'];
}
$canvas = $Imagine->create(
new Box($canvasWidth, $canvasHeight),
new Color($arguments['fillColour'])
);
// Put image in the middle of the canvas
$newImage = $canvas->paste($newImage, new Point(
(int) (($canvasWidth - $adjustedSize->getWidth()) / 2),
(int) (($canvasHeight - $adjustedSize->getHeight()) / 2)
));
}
$newImage->save($originalImage, array('quality' => $arguments['quality']));
}
}