Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Jun 21, 2018
1 parent 976fa26 commit 73b3421
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": ">=7.2.0",
"ext-imagick": "*",
"chillerlan/php-traits": "^1.1",
"ps/image-optimizer": "^1.2",
"psr/log": "^1.0"
},
"require-dev": {
Expand Down
61 changes: 38 additions & 23 deletions src/Imagetiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace chillerlan\Imagetiler;

use chillerlan\Traits\ContainerInterface;
use ImageOptimizer\Optimizer;
use ImageOptimizer\OptimizerFactory;
use Imagick;
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};

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

}

// load the optional image optimizer
$optimizer = null;

if($this->options->optimize_output){
$optimizer = (new OptimizerFactory($this->options->optimizer_settings, $this->logger))->get();
}

// prepare the zoom base images
$this->prepareZoomBaseImages($image_path, $out_path);

for($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++){
$this->createTilesForZoom($out_path, $i);
// create the tiles
for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){

$base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext;

//load image
if(!is_file($base_image) || !is_readable($base_image)){
throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom);
}

$this->createTilesForZoom(new Imagick($base_image), $zoom, $out_path, $optimizer);
}

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

for($i = $this->options->zoom_min; $i <= $this->options->zoom_max; $i++){
$lvl_file = $out_path.'/'.$i.'.'.$this->options->tile_ext;
for($zoom = $this->options->zoom_min; $zoom <= $this->options->zoom_max; $zoom++){
$lvl_file = $out_path.'/'.$zoom.'.'.$this->options->tile_ext;

if(is_file($lvl_file)){
if(unlink($lvl_file)){
$this->logger->info('deleted base image for zoom level '.$i.': '.$lvl_file);
$this->logger->info('deleted base image for zoom level '.$zoom.': '.$lvl_file);
}
}
}
Expand Down Expand Up @@ -178,23 +197,14 @@ protected function prepareZoomBaseImages(string $image_path, string $out_path):v
/**
* create tiles for each zoom level
*
* @param string $out_path
* @param int $zoom
* @param \Imagick $im
* @param int $zoom
* @param string $out_path
* @param \ImageOptimizer\Optimizer|null $optimizer
*
* @return void
* @throws \chillerlan\Imagetiler\ImagetilerException
*/
protected function createTilesForZoom(string $out_path, int $zoom):void{
$base_image = $out_path.'/'.$zoom.'.'.$this->options->tile_ext;

//load image
if(!is_file($base_image) || !is_readable($base_image)){
throw new ImagetilerException('cannot read base image '.$base_image.' for zoom '.$zoom);
}

$im = new Imagick($base_image);

//get image size
protected function createTilesForZoom(Imagick $im, int $zoom, string $out_path, Optimizer $optimizer = null):void{
$w = $im->getimagewidth();
$h = $im->getImageHeight();

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

$this->saveImage($ti, $tile);
$this->saveImage($ti, $tile, $optimizer);
$this->clearImage($ti);
}

Expand All @@ -257,13 +267,14 @@ protected function createTilesForZoom(string $out_path, int $zoom):void{
/**
* save image in to destination
*
* @param Imagick $image
* @param string $dest full path with file name
* @param Imagick $image
* @param string $dest full path with file name
* @param \ImageOptimizer\Optimizer $optimizer
*
* @return void
* @throws \chillerlan\Imagetiler\ImagetilerException
*/
protected function saveImage(Imagick $image, string $dest):void{
protected function saveImage(Imagick $image, string $dest, Optimizer $optimizer = null):void{
$dir = dirname($dest);

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

if($optimizer instanceof Optimizer){
$optimizer->optimize($dest);
}

}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/ImagetilerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* @property bool $overwrite_base_image
* @property bool $overwrite_tile_image
* @property bool $clean_up
* @property bool $optimize_output
* @property array $optimizer_settings
*/
class ImagetilerOptions extends ContainerAbstract{
use ImagetilerOptionsTrait;
Expand Down
9 changes: 9 additions & 0 deletions src/ImagetilerOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ trait ImagetilerOptionsTrait{
*/
protected $clean_up = true;

/**
* @var bool
*/
protected $optimize_output = false;

/**
* @var array
*/
protected $optimizer_settings = [];
}

0 comments on commit 73b3421

Please sign in to comment.