|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Convert\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Get/parse shorthandsize strings from php.ini as bytes. |
|
7
|
|
|
* |
|
8
|
|
|
* Parse strings like "1k" into bytes (1024). |
|
9
|
|
|
* |
|
10
|
|
|
* @package WebPConvert |
|
11
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
12
|
|
|
* @since Class available since Release 2.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class PhpIniSizes |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Parse a shordhandsize string as the ones returned by ini_get() |
|
19
|
|
|
* |
|
20
|
|
|
* Parse a shorthandsize string having the syntax allowed in php.ini and returned by ini_get(). |
|
21
|
|
|
* Ie "1K" => 1024. |
|
22
|
|
|
* Strings without units are also accepted. |
|
23
|
|
|
* The shorthandbytes syntax is described here: https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $shortHandSize A size string of the type returned by ini_get() |
|
26
|
|
|
* @return float|false The parsed size (beware: it is float, do not check high numbers for equality), |
|
27
|
|
|
* or false if parse error |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public static function parseShortHandSize($shortHandSize) |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
1 |
|
$result = preg_match("#^\\s*(\\d+(?:\\.\\d+)?)([bkmgtpezy]?)\\s*$#i", $shortHandSize, $matches); |
|
33
|
1 |
|
if ($result !== 1) { |
|
34
|
1 |
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// Truncate, because that is what php does. |
|
38
|
1 |
|
$digitsValue = floor($matches[1]); |
|
39
|
|
|
|
|
40
|
1 |
|
if ((count($matches) >= 3) && ($matches[2] != '')) { |
|
41
|
1 |
|
$unit = $matches[2]; |
|
42
|
|
|
|
|
43
|
|
|
// Find the position of the unit in the ordered string which is the power |
|
44
|
|
|
// of magnitude to multiply a kilobyte by. |
|
45
|
1 |
|
$position = stripos('bkmgtpezy', $unit); |
|
46
|
|
|
|
|
47
|
1 |
|
return floatval($digitsValue * pow(1024, $position)); |
|
48
|
|
|
} else { |
|
49
|
1 |
|
return $digitsValue; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/* |
|
54
|
|
|
* Get the size of an php.ini option. |
|
55
|
|
|
* |
|
56
|
|
|
* Calls ini_get() and parses the size to a number. |
|
57
|
|
|
* If the configuration option is null, does not exist, or cannot be parsed as a shorthandsize, false is returned |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $varname The configuration option name. |
|
60
|
|
|
* @return float|false The parsed size or false if the configuration option does not exist |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getIniBytes($iniVarName) |
|
63
|
|
|
{ |
|
64
|
|
|
$iniVarValue = ini_get($iniVarName); |
|
65
|
|
|
if (($iniVarValue == '') || $iniVarValue === false) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
return self::parseShortHandSize($iniVarValue); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|