13
13
namespace chillerlan \Imagetiler ;
14
14
15
15
use chillerlan \Traits \ContainerInterface ;
16
+ use ImageOptimizer \Optimizer ;
17
+ use ImageOptimizer \OptimizerFactory ;
16
18
use Imagick ;
17
19
use Psr \Log \{LoggerAwareInterface , LoggerAwareTrait , LoggerInterface , NullLogger };
18
20
@@ -100,21 +102,38 @@ public function process(string $image_path, string $out_path):Imagetiler{
100
102
101
103
}
102
104
105
+ // load the optional image optimizer
106
+ $ optimizer = null ;
107
+
108
+ if ($ this ->options ->optimize_output ){
109
+ $ optimizer = (new OptimizerFactory ($ this ->options ->optimizer_settings , $ this ->logger ))->get ();
110
+ }
111
+
112
+ // prepare the zoom base images
103
113
$ this ->prepareZoomBaseImages ($ image_path , $ out_path );
104
114
105
- for ($ i = $ this ->options ->zoom_min ; $ i <= $ this ->options ->zoom_max ; $ i ++){
106
- $ this ->createTilesForZoom ($ out_path , $ i );
115
+ // create the tiles
116
+ for ($ zoom = $ this ->options ->zoom_min ; $ zoom <= $ this ->options ->zoom_max ; $ zoom ++){
117
+
118
+ $ base_image = $ out_path .'/ ' .$ zoom .'. ' .$ this ->options ->tile_ext ;
119
+
120
+ //load image
121
+ if (!is_file ($ base_image ) || !is_readable ($ base_image )){
122
+ throw new ImagetilerException ('cannot read base image ' .$ base_image .' for zoom ' .$ zoom );
123
+ }
124
+
125
+ $ this ->createTilesForZoom (new Imagick ($ base_image ), $ zoom , $ out_path , $ optimizer );
107
126
}
108
127
109
128
// clean up base images
110
129
if ($ this ->options ->clean_up ){
111
130
112
- for ($ i = $ this ->options ->zoom_min ; $ i <= $ this ->options ->zoom_max ; $ i ++){
113
- $ lvl_file = $ out_path .'/ ' .$ i .'. ' .$ this ->options ->tile_ext ;
131
+ for ($ zoom = $ this ->options ->zoom_min ; $ zoom <= $ this ->options ->zoom_max ; $ zoom ++){
132
+ $ lvl_file = $ out_path .'/ ' .$ zoom .'. ' .$ this ->options ->tile_ext ;
114
133
115
134
if (is_file ($ lvl_file )){
116
135
if (unlink ($ lvl_file )){
117
- $ this ->logger ->info ('deleted base image for zoom level ' .$ i .': ' .$ lvl_file );
136
+ $ this ->logger ->info ('deleted base image for zoom level ' .$ zoom .': ' .$ lvl_file );
118
137
}
119
138
}
120
139
}
@@ -178,23 +197,14 @@ protected function prepareZoomBaseImages(string $image_path, string $out_path):v
178
197
/**
179
198
* create tiles for each zoom level
180
199
*
181
- * @param string $out_path
182
- * @param int $zoom
200
+ * @param \Imagick $im
201
+ * @param int $zoom
202
+ * @param string $out_path
203
+ * @param \ImageOptimizer\Optimizer|null $optimizer
183
204
*
184
205
* @return void
185
- * @throws \chillerlan\Imagetiler\ImagetilerException
186
206
*/
187
- protected function createTilesForZoom (string $ out_path , int $ zoom ):void {
188
- $ base_image = $ out_path .'/ ' .$ zoom .'. ' .$ this ->options ->tile_ext ;
189
-
190
- //load image
191
- if (!is_file ($ base_image ) || !is_readable ($ base_image )){
192
- throw new ImagetilerException ('cannot read base image ' .$ base_image .' for zoom ' .$ zoom );
193
- }
194
-
195
- $ im = new Imagick ($ base_image );
196
-
197
- //get image size
207
+ protected function createTilesForZoom (Imagick $ im , int $ zoom , string $ out_path , Optimizer $ optimizer = null ):void {
198
208
$ w = $ im ->getimagewidth ();
199
209
$ h = $ im ->getImageHeight ();
200
210
@@ -242,7 +252,7 @@ protected function createTilesForZoom(string $out_path, int $zoom):void{
242
252
$ ti ->extentImage ($ ts , $ ts , 0 , $ th );
243
253
}
244
254
245
- $ this ->saveImage ($ ti , $ tile );
255
+ $ this ->saveImage ($ ti , $ tile, $ optimizer );
246
256
$ this ->clearImage ($ ti );
247
257
}
248
258
@@ -257,13 +267,14 @@ protected function createTilesForZoom(string $out_path, int $zoom):void{
257
267
/**
258
268
* save image in to destination
259
269
*
260
- * @param Imagick $image
261
- * @param string $dest full path with file name
270
+ * @param Imagick $image
271
+ * @param string $dest full path with file name
272
+ * @param \ImageOptimizer\Optimizer $optimizer
262
273
*
263
274
* @return void
264
275
* @throws \chillerlan\Imagetiler\ImagetilerException
265
276
*/
266
- protected function saveImage (Imagick $ image , string $ dest ):void {
277
+ protected function saveImage (Imagick $ image , string $ dest, Optimizer $ optimizer = null ):void {
267
278
$ dir = dirname ($ dest );
268
279
269
280
if (!is_dir ($ dir )){
@@ -281,6 +292,10 @@ protected function saveImage(Imagick $image, string $dest):void{
281
292
throw new ImagetilerException ('cannot save image ' .$ dest );
282
293
}
283
294
295
+ if ($ optimizer instanceof Optimizer){
296
+ $ optimizer ->optimize ($ dest );
297
+ }
298
+
284
299
}
285
300
286
301
/**
0 commit comments