Skip to content

Commit

Permalink
:octocat: moved settings init
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Jan 30, 2019
1 parent 8569fce commit 17fb3da
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
43 changes: 5 additions & 38 deletions src/Imagetiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace chillerlan\Imagetiler;

use chillerlan\Traits\ContainerInterface;
use chillerlan\Settings\SettingsContainerInterface;
use ImageOptimizer\Optimizer;
use Imagick;
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
Expand All @@ -33,13 +33,13 @@ class Imagetiler implements LoggerAwareInterface{
/**
* Imagetiler constructor.
*
* @param \chillerlan\Traits\ContainerInterface|null $options
* @param \chillerlan\Settings\SettingsContainerInterface|null $options
* @param \ImageOptimizer\Optimizer $optimizer
* @param \Psr\Log\LoggerInterface|null $logger
*
* @throws \chillerlan\Imagetiler\ImagetilerException
*/
public function __construct(ContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null){
public function __construct(SettingsContainerInterface $options = null, Optimizer $optimizer = null, LoggerInterface $logger = null){

if(!extension_loaded('imagick')){
throw new ImagetilerException('Imagick extension is not available');
Expand All @@ -54,23 +54,12 @@ public function __construct(ContainerInterface $options = null, Optimizer $optim
}

/**
* @param \chillerlan\Traits\ContainerInterface $options
* @param \chillerlan\Settings\SettingsContainerInterface $options
*
* @return \chillerlan\Imagetiler\Imagetiler
* @throws \chillerlan\Imagetiler\ImagetilerException
*/
public function setOptions(ContainerInterface $options):Imagetiler{
$options->zoom_min = max(0, $options->zoom_min);
$options->zoom_max = max(1, $options->zoom_max);

if($options->zoom_normalize === null || $options->zoom_max < $options->zoom_normalize){
$options->zoom_normalize = $options->zoom_max;
}

if($options->tile_ext === null){
$options->tile_ext = $this->getExtension($options->tile_format);
}

public function setOptions(SettingsContainerInterface $options):Imagetiler{
$this->options = $options;

if(ini_set('memory_limit', $this->options->memory_limit) === false){
Expand All @@ -86,7 +75,6 @@ public function setOptions(ContainerInterface $options):Imagetiler{
putenv('MAGICK_TEMPORARY_PATH='.$this->options->imagick_tmp);
}


return $this;
}

Expand Down Expand Up @@ -350,25 +338,4 @@ protected function getSize(int $width, int $height, int $zoom):array{
return [$width, $height];
}

/**
* return file extension depend of given format
*
* @param string $format
*
* @return string
* @throws \chillerlan\Imagetiler\ImagetilerException
*/
protected function getExtension(string $format):string{

if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){
return 'jpg';
}

if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){
return 'png';
}

throw new ImagetilerException('invalid file format');
}

}
4 changes: 2 additions & 2 deletions src/ImagetilerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace chillerlan\Imagetiler;

use chillerlan\Traits\ContainerAbstract;
use chillerlan\Settings\SettingsContainerAbstract;

/**
* @property int $tile_size
Expand All @@ -35,6 +35,6 @@
* @property bool $clean_up
* @property bool $optimize_output
*/
class ImagetilerOptions extends ContainerAbstract{
class ImagetilerOptions extends SettingsContainerAbstract{
use ImagetilerOptionsTrait;
}
38 changes: 38 additions & 0 deletions src/ImagetilerOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,42 @@ trait ImagetilerOptionsTrait{
*/
protected $optimize_output = false;

/**
* "constructor"
*/
public function ImagetilerOptionsTrait(){
$this->zoom_min = max(0, $this->zoom_min);
$this->zoom_max = max(1, $this->zoom_max);

if($this->zoom_normalize === null || $this->zoom_max < $this->zoom_normalize){
$this->zoom_normalize = $this->zoom_max;
}

if($this->tile_ext === null){
$this->tile_ext = $this->getExtension($this->tile_format);
}

}

/**
* return file extension depend of given format
*
* @param string $format
*
* @return string
* @throws \chillerlan\Imagetiler\ImagetilerException
*/
protected function getExtension(string $format):string{

if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr',], true)){
return 'jpg';
}

if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64',], true)){
return 'png';
}

throw new ImagetilerException('invalid file format');
}

}

0 comments on commit 17fb3da

Please sign in to comment.