Skip to content

Commit 73b3421

Browse files
committed
1 parent 976fa26 commit 73b3421

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": ">=7.2.0",
2020
"ext-imagick": "*",
2121
"chillerlan/php-traits": "^1.1",
22+
"ps/image-optimizer": "^1.2",
2223
"psr/log": "^1.0"
2324
},
2425
"require-dev": {

src/Imagetiler.php

+38-23
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
namespace chillerlan\Imagetiler;
1414

1515
use chillerlan\Traits\ContainerInterface;
16+
use ImageOptimizer\Optimizer;
17+
use ImageOptimizer\OptimizerFactory;
1618
use Imagick;
1719
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
1820

@@ -100,21 +102,38 @@ public function process(string $image_path, string $out_path):Imagetiler{
100102

101103
}
102104

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
103113
$this->prepareZoomBaseImages($image_path, $out_path);
104114

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);
107126
}
108127

109128
// clean up base images
110129
if($this->options->clean_up){
111130

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;
114133

115134
if(is_file($lvl_file)){
116135
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);
118137
}
119138
}
120139
}
@@ -178,23 +197,14 @@ protected function prepareZoomBaseImages(string $image_path, string $out_path):v
178197
/**
179198
* create tiles for each zoom level
180199
*
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
183204
*
184205
* @return void
185-
* @throws \chillerlan\Imagetiler\ImagetilerException
186206
*/
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{
198208
$w = $im->getimagewidth();
199209
$h = $im->getImageHeight();
200210

@@ -242,7 +252,7 @@ protected function createTilesForZoom(string $out_path, int $zoom):void{
242252
$ti->extentImage($ts, $ts, 0, $th);
243253
}
244254

245-
$this->saveImage($ti, $tile);
255+
$this->saveImage($ti, $tile, $optimizer);
246256
$this->clearImage($ti);
247257
}
248258

@@ -257,13 +267,14 @@ protected function createTilesForZoom(string $out_path, int $zoom):void{
257267
/**
258268
* save image in to destination
259269
*
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
262273
*
263274
* @return void
264275
* @throws \chillerlan\Imagetiler\ImagetilerException
265276
*/
266-
protected function saveImage(Imagick $image, string $dest):void{
277+
protected function saveImage(Imagick $image, string $dest, Optimizer $optimizer = null):void{
267278
$dir = dirname($dest);
268279

269280
if(!is_dir($dir)){
@@ -281,6 +292,10 @@ protected function saveImage(Imagick $image, string $dest):void{
281292
throw new ImagetilerException('cannot save image '.$dest);
282293
}
283294

295+
if($optimizer instanceof Optimizer){
296+
$optimizer->optimize($dest);
297+
}
298+
284299
}
285300

286301
/**

src/ImagetilerOptions.php

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* @property bool $overwrite_base_image
3434
* @property bool $overwrite_tile_image
3535
* @property bool $clean_up
36+
* @property bool $optimize_output
37+
* @property array $optimizer_settings
3638
*/
3739
class ImagetilerOptions extends ContainerAbstract{
3840
use ImagetilerOptionsTrait;

src/ImagetilerOptionsTrait.php

+9
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,13 @@ trait ImagetilerOptionsTrait{
147147
*/
148148
protected $clean_up = true;
149149

150+
/**
151+
* @var bool
152+
*/
153+
protected $optimize_output = false;
154+
155+
/**
156+
* @var array
157+
*/
158+
protected $optimizer_settings = [];
150159
}

0 commit comments

Comments
 (0)