Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
ImageInformation | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
load | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Image; |
6 | |
7 | use Exception; |
8 | use PeServer\Core\Errors\ErrorHandler; |
9 | use PeServer\Core\Image\ImageType; |
10 | use PeServer\Core\Image\Size; |
11 | use PeServer\Core\Throws\Throws; |
12 | use PeServer\Core\Throws\ImageException; |
13 | |
14 | /** |
15 | * 画像情報。 |
16 | */ |
17 | readonly class ImageInformation |
18 | { |
19 | /** |
20 | * 生成 |
21 | * |
22 | * @param Size $size |
23 | * @param string $mime |
24 | * @param ImageType $type |
25 | */ |
26 | private function __construct( |
27 | public Size $size, |
28 | public string $mime, |
29 | public ImageType $type |
30 | ) { |
31 | } |
32 | |
33 | /** |
34 | * ファイルからイメージサイズを取得。 |
35 | * |
36 | * `getimagesize` ラッパー。 |
37 | * |
38 | * @param string $filePath 対象画像ファイルパス。 |
39 | * @return ImageInformation |
40 | * @throws ImageException |
41 | * @see https://www.php.net/manual/function.getimagesize.php |
42 | */ |
43 | public static function load(string $filePath): ImageInformation |
44 | { |
45 | $result = ErrorHandler::trap(fn () => getimagesize($filePath)); |
46 | if ($result->isFailureOrFalse()) { |
47 | throw new ImageException($filePath); |
48 | } |
49 | |
50 | $result = $result->value; |
51 | |
52 | Throws::throwIf(1 <= $result[0]); |
53 | Throws::throwIf(1 <= $result[1]); |
54 | Throws::throwIf(-1 <= $result[2] && $result[2] <= 18 && $result[2] !== 0); |
55 | |
56 | return new ImageInformation( |
57 | new Size( |
58 | $result[0], |
59 | $result[1] |
60 | ), |
61 | $result['mime'], |
62 | ImageType::from($result[2]) |
63 | ); |
64 | } |
65 | } |