Passed
Push — master ( 06c932...7812b7 )
by smiley
03:13
created

ImagetilerOptionsTrait::ImagetilerOptionsTrait()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 4
nc 4
nop 0
1
<?php
2
/**
3
 * Trait ImagetilerOptionsTrait
4
 *
5
 * @filesource   ImagetilerOptionsTrait.php
6
 * @created      20.06.2018
7
 * @package      chillerlan\Imagetiler
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Imagetiler;
14
15
use Imagick;
16
17
use function in_array, max, strtolower;
18
19
trait ImagetilerOptionsTrait{
20
21
	/**
22
	 * width/height of a single tile
23
	 *
24
	 * @var int
25
	 */
26
	protected $tile_size = 256;
27
28
	/**
29
	 * minimum zoom level
30
	 *
31
	 * @var int
32
	 */
33
34
	protected $zoom_min = 0;
35
36
	/**
37
	 * maximum zoom level
38
	 *
39
	 * @var int
40
	 */
41
	protected $zoom_max = 8;
42
43
	/**
44
	 * normalize zoom level
45
	 *
46
	 * this zoom level represents the zoom/size of the original image.
47
	 * zoom levels higher than this will be upscaled up to $zoom_max,
48
	 * lower will be downsampled to $zoom_min, which may take
49
	 * some time and resources depending on the size of the input image.
50
	 *
51
	 * Defaults to $zoom_max
52
	 *
53
	 * @var int
54
	 */
55
	protected $zoom_normalize = null;
56
57
	/**
58
	 * if set to true - the origin will be set to bottom left, +y upwards
59
	 *
60
	 * @see http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification#TileMap_Diagram
61
	 *
62
	 * otherwise the origin is on the top left, +y downwards (default)
63
	 * @see https://developers.google.com/maps/documentation/javascript/coordinates#tile-coordinates
64
	 *
65
	 * @var bool
66
	 */
67
	protected $tms = false;
68
69
	/**
70
	 * fill color can be transparent for png
71
	 *
72
	 * @var string
73
	 */
74
75
	protected $fill_color = '#000000';
76
77
	/**
78
	 * @see https://secure.php.net/manual/ini.core.php#ini.memory-limit
79
	 * @var string
80
	 */
81
	protected $memory_limit = '-1';
82
83
	/**
84
	 * %1$d - zoom
85
	 * %2$d - x
86
	 * %3$d - y
87
	 *
88
	 * @see https://secure.php.net/manual/function.sprintf.php
89
	 * @var string
90
	 */
91
	protected $store_structure = '%1$d/%2$d/%3$d';
92
93
	/**
94
	 * determines whether to use fast scaleImage (true) or slow resizeImage (false)
95
	 *
96
	 * @var bool
97
	 */
98
	protected $fast_resize_upsample = false;
99
100
	/**
101
	 * @see https://secure.php.net/manual/imagick.constants.php
102
	 * @see http://www.imagemagick.org/Usage/filter/nicolas/
103
	 * @var int
104
	 */
105
	protected $resize_filter_upsample = Imagick::FILTER_ROBIDOUXSHARP;
106
107
	/**
108
	 * @var float
109
	 */
110
	protected $resize_blur_upsample = 1.0;
111
112
	/**
113
	 * determines whether to use fast scaleImage (true) or slow resizeImage (false)
114
	 *
115
	 * @var bool
116
	 */
117
	protected $fast_resize_downsample = false;
118
119
	/**
120
	 * @see https://secure.php.net/manual/imagick.constants.php
121
	 * @var int
122
	 */
123
	protected $resize_filter_downsample = Imagick::FILTER_LANCZOSRADIUS;
124
125
	/**
126
	 * @var float
127
	 */
128
	protected $resize_blur_downsample = 1.0;
129
130
	/**
131
	 * image format used for storing the tiles: jpeg or png
132
	 *
133
	 * @see http://www.imagemagick.org/script/formats.php
134
	 *
135
	 * @var string
136
	 */
137
	protected $tile_format = 'png';
138
139
	/**
140
	 * tile image extension - autodetected from format if none given
141
	 *
142
	 * @var string
143
	 */
144
	protected $tile_ext = null;
145
146
	/**
147
	 * quality of the saved image in jpeg format
148
	 *
149
	 * @var int
150
	 */
151
	protected $quality_jpeg = 80;
152
153
	/**
154
	 * @var bool
155
	 */
156
	protected $overwrite_base_image = false;
157
158
	/**
159
	 * @var bool
160
	 */
161
	protected $overwrite_tile_image = false;
162
163
	/**
164
	 * @var bool
165
	 */
166
	protected $clean_up = true;
167
168
	/**
169
	 * @var bool
170
	 */
171
	protected $optimize_output = false;
172
173
	/**
174
	 * don't create temporary base images
175
	 *
176
	 * @var bool
177
	 */
178
	protected $no_temp_baseimages = false;
179
180
	/**
181
	 * @param int $zoom_min
182
	 *
183
	 * @return void
184
	 */
185
	protected function set_zoom_min(int $zoom_min):void{
186
		$this->zoom_min = max(0, $zoom_min);
187
	}
188
189
	/**
190
	 * @param int $zoom_max
191
	 *
192
	 * @return void
193
	 */
194
	protected function set_zoom_max(int $zoom_max):void{
195
		$this->zoom_max = max(0, $zoom_max);
196
	}
197
198
	/**
199
	 * @return string
200
	 */
201
	protected function get_tile_ext():string{
202
		return $this->tile_ext ?? $this->getExtension($this->tile_format);
203
	}
204
205
	/**
206
	 * return file extension depending on given format
207
	 *
208
	 * @param string $format
209
	 *
210
	 * @return string
211
	 * @throws \chillerlan\Imagetiler\ImagetilerException
212
	 */
213
	protected function getExtension(string $format):string{
214
		$format = strtolower($format);
215
216
		if(in_array($format, ['jpeg', 'jp2', 'jpc', 'jxr'], true)){
217
			return 'jpg';
218
		}
219
220
		if(in_array($format, ['png', 'png00', 'png8', 'png24', 'png32', 'png64'], true)){
221
			return 'png';
222
		}
223
224
		throw new ImagetilerException('invalid file format');
225
	}
226
227
}
228