1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WebThumbnailer\Utils; |
6
|
|
|
|
7
|
|
|
use WebThumbnailer\Application\ConfigManager; |
8
|
|
|
use WebThumbnailer\Exception\BadRulesException; |
9
|
|
|
use WebThumbnailer\Exception\IOException; |
10
|
|
|
use WebThumbnailer\WebThumbnailer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Handles 'meta' size operation. |
14
|
|
|
* |
15
|
|
|
* Fixed sizes: |
16
|
|
|
* - SMALL=160px |
17
|
|
|
* - MEDIUM=320px |
18
|
|
|
* - LARGE=640px |
19
|
|
|
*/ |
20
|
|
|
class SizeUtils |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Convert a 'meta' size to pixel size. |
24
|
|
|
* |
25
|
|
|
* Default value if unknown: 160px. |
26
|
|
|
* |
27
|
|
|
* @param string $size Meta size to convert. |
28
|
|
|
* |
29
|
|
|
* @return int the size in pixels |
30
|
|
|
* |
31
|
|
|
* @throws BadRulesException |
32
|
|
|
* @throws IOException |
33
|
|
|
*/ |
34
|
|
|
public static function getMetaSize(string $size): int |
35
|
|
|
{ |
36
|
|
|
switch ($size) { |
37
|
|
|
case WebThumbnailer::SIZE_LARGE: |
38
|
|
|
return (int) ConfigManager::get('settings.size_large', 640); |
39
|
|
|
case WebThumbnailer::SIZE_MEDIUM: |
40
|
|
|
return (int) ConfigManager::get('settings.size_medium', 320); |
41
|
|
|
case WebThumbnailer::SIZE_SMALL: |
42
|
|
|
default: |
43
|
|
|
return (int) ConfigManager::get('settings.size_small', 160); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check if a string is a meta size. |
49
|
|
|
* |
50
|
|
|
* @param string $size the string to test. |
51
|
|
|
* |
52
|
|
|
* @return boolean true|false. |
53
|
|
|
*/ |
54
|
|
|
public static function isMetaSize(string $size): bool |
55
|
|
|
{ |
56
|
|
|
$metaSize = array ( |
57
|
|
|
WebThumbnailer::SIZE_SMALL, |
58
|
|
|
WebThumbnailer::SIZE_MEDIUM, |
59
|
|
|
WebThumbnailer::SIZE_LARGE |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return in_array($size, $metaSize, true); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|