|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Silverback\ApiComponentBundle\Exception\FileMissingException; |
|
6
|
|
|
use Silverback\ApiComponentBundle\Exception\FileNotImageException; |
|
7
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
|
8
|
|
|
|
|
9
|
|
|
class ImageMetadata |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @Groups({"component", "content", "route"}) |
|
13
|
|
|
* @var int |
|
14
|
|
|
*/ |
|
15
|
|
|
private $width; |
|
16
|
|
|
/** |
|
17
|
|
|
* @Groups({"component", "content", "route"}) |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $height; |
|
21
|
|
|
/** |
|
22
|
|
|
* @Groups({"component", "content", "route"}) |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $filePath; |
|
26
|
|
|
/** |
|
27
|
|
|
* @Groups({"component", "content", "route"}) |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $publicPath; |
|
31
|
|
|
/** |
|
32
|
|
|
* @Groups({"component", "content", "route"}) |
|
33
|
|
|
* @var string|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private $imagineKey; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
string $filePath, |
|
39
|
|
|
string $publicPath, |
|
40
|
|
|
?string $imagineKey = null |
|
41
|
|
|
) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->filePath = $filePath; |
|
44
|
|
|
$this->publicPath = $publicPath; |
|
45
|
|
|
if (!file_exists($filePath)) { |
|
46
|
|
|
throw new FileMissingException(sprintf('The file %s does not exist while constructing %s', $filePath, self::class)); |
|
47
|
|
|
} elseif (false === \exif_imagetype($filePath)) { |
|
48
|
|
|
throw new FileNotImageException(sprintf('The file %s is not an image while constructing %s', $filePath, self::class)); |
|
49
|
|
|
} else { |
|
50
|
|
|
[$this->width, $this->height] = getimagesize($filePath); |
|
51
|
|
|
} |
|
52
|
|
|
$this->imagineKey= $imagineKey; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getWidth(): int |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->width; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return int |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getHeight(): int |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->height; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFilePath(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->filePath; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getPublicPath(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->publicPath; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return null|string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getImagineKey(): ?string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->imagineKey; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|