-
Notifications
You must be signed in to change notification settings - Fork 0
/
JpegOptimImage.module
225 lines (181 loc) · 6.58 KB
/
JpegOptimImage.module
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* JpegOptimImage: Optimize resized jpegs.
*
* By Jonathan Dart
* http://metricmarketing.ca/blog/author/jonathan-dart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class JpegOptimImage extends WireData implements Module, ConfigurableModule
{
static $defaults = array(
'joQuality' => 75,
'joThreshold' => 40,
'joProgressive' => true,
'joStrip' => true,
'joBinary' => 'jpegoptim'
);
protected
$functionCallOptions = array();
public static function getModuleInfo()
{
return array(
'title' => 'JpegOptimImage',
'version' => 10,
'summary' => 'JpegOptimImage: Optimize resized jpegs.',
'href' => 'https://github.com/jdart/JpegOptimImage',
'singular' => true,
'autoload' => true,
);
}
public function init()
{
$this->addHookAfter('ImageSizer::resize', $this, 'imageSizerResizeAfter');
$this->addHook('Pageimage::optimize', $this, 'pageImageOptimize');
}
public function imageSizerResizeAfter($event)
{
$imageSizer = $event->object;
$this->functionCallOptions = array();
if ($imageSizer->getImageType() !== IMAGETYPE_JPEG)
return;
$this->fileOptimize($imageSizer->getFilename());
}
public function fileOptimize($path, $newPath=null)
{
if ( ! is_null($newPath)) // Make a copy for jpegoptim to overwrite
copy($path, $newPath);
$cmd = array(
escapeshellarg($this->valueOrDefault('joBinary')),
'-q',
);
if ($this->valueOrDefault('joStrip'))
$cmd[] = '--strip-exif --strip-com --strip-icc';
if ( ! $this->valueOrDefault('joLossless')) {
$cmd[] = sprintf('-m%d', $this->valueOrDefault('joQuality'));
$cmd[] = sprintf('-T%d', $this->valueOrDefault('joThreshold'));
}
if ($this->valueOrDefault('joProgressive'))
$cmd[] = '--all-progressive';
$cmd[] = escapeshellarg(is_null($newPath) ? $path : $newPath);
exec(implode(' ', $cmd));
}
/**
* There are two scenarios this function covers:
*
* Resizing an original image:
* $page->image->optimize()->url;
*
* Resizing a manipulated image:
* $page->image->someManipulations()->optimize()->url;
*/
public function pageImageOptimize(HookEvent $event)
{
$image = $event->object; // Pageimage
$path = $image->filename;
// options passed in to optimize()
$this->functionCallOptions = $event->arguments && is_array($event->arguments[0])
? $event->arguments[0]
: array();
// original images should not be overwritten by an optimized version
if ($this_is_the_original = is_null($image->original)) {
if ( ! ($newPath = $this->optimizedFilename($path)))
return;
if ( ! file_exists($newPath))
$this->fileOptimize($path, $newPath);
$return_image = clone $image;
$return_image->setOriginal($image);
$return_image->setFilename($newPath);
} else { // a variation (manipulated)
$original_mtime = filemtime($image->original->filename);
// only run optimize if the original file is newer
if (filemtime($path) > $original_mtime) {
$this->fileOptimize($path);
touch($path, $original_mtime);
}
$return_image = $image;
}
$event->return = $return_image;
}
public function optimizedFilename($path)
{
$pathInfo = pathinfo($path);
if ( ! in_array(strtolower($pathInfo['extension']), array('jpg', 'jpeg')))
return false;
$newPath = sprintf('%s%s%s.0x0.%s',
$pathInfo['dirname'],
DIRECTORY_SEPARATOR,
$pathInfo['filename'],
$pathInfo['extension']
);
return $newPath;
}
public static function getModuleConfigInputfields(array $data)
{
$fields = new InputfieldWrapper();
$modules = Wire::getFuel('modules');
$field = $modules->get("InputfieldText");
$field->name = "joQuality";
$field->label = __("Maximum image quality factor");
$field->value = static::dataValueOrDefault('joQuality', $data);
$field->description = __("Sets the maximum image quality factor. This option will reduce quality of those source files that were saved using higher quality setting. While files that already have lower quality setting will be compressed using the lossless optimization method. Valid values for quality parameter are: 0 - 100");
$fields->add($field);
$field = $modules->get("InputfieldText");
$field->name = "joThreshold";
$field->label = __("Minimum Compression Threshold");
$field->value = static::dataValueOrDefault('joThreshold', $data);
$field->description = __("Keep the file unchanged if the compression gain is lower than the threshold (%). Valid values for thresholds are: 0 - 100");
$fields->add($field);
$field = $modules->get("InputfieldText");
$field->name = "joBinary";
$field->label = __("JpegOptim Binary path");
$field->value = static::dataValueOrDefault('joBinary', $data);
$field->description = __("Path to the jpeg optim location");
$fields->add($field);
$field = $modules->get("InputfieldCheckbox");
$field->name = "joProgressive";
$field->label = __("Create progressive images");
$field->checked = static::dataValueOrDefault('joProgressive', $data);
$field->description = __("Force all output files to be progressive.");
$fields->add($field);
$field = $modules->get("InputfieldCheckbox");
$field->name = "joStrip";
$field->label = __("Strip all markers from output file");
$field->checked = static::dataValueOrDefault('joStrip', $data);
$field->description = __("Output JPEGs will still likely contain one or two markers (JFIF and Adobe APP14) depending on colorspace used in the image, as these markers are generated by the libjpeg encoder automatically.");
$fields->add($field);
return $fields;
}
public static function dataValueOrDefault($key, $data=null)
{
if ( ! isset($data[$key]))
if (isset(static::$defaults[$key]))
return static::$defaults[$key];
else
return null;
return $data[$key];
}
public function valueOrDefault($key)
{
$value = null;
if (isset($this->functionCallOptions[$key]))
$value = $this->functionCallOptions[$key];
if (is_null($value))
$value = $this->get($key);
if (is_null($value) && isset(static::$defaults[$key]))
$value = static::$defaults[$key];
return $value;
}
}