1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GNAHotelSolutions\ImageCacher; |
4
|
|
|
|
5
|
|
|
class Image |
6
|
|
|
{ |
7
|
|
|
/** @var string */ |
8
|
|
|
protected $rootPath; |
9
|
|
|
|
10
|
|
|
/** @var string */ |
11
|
|
|
protected $name; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $path; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
protected $width; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
protected $height; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $outputFormat = null; |
27
|
|
|
|
28
|
|
|
public function __construct(string $image, string $rootPath = '') |
29
|
|
|
{ |
30
|
|
|
$this->rootPath = rtrim($rootPath, '/'); |
31
|
|
|
|
32
|
|
|
[$this->name, $this->path] = $this->splitNameAndPath($image); |
33
|
|
|
|
34
|
|
|
$this->verify(); |
35
|
|
|
|
36
|
|
|
$this->extractImageInformation(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function verify(): void |
40
|
|
|
{ |
41
|
|
|
if (! file_exists($this->getOriginalFullPath())) { |
42
|
|
|
throw new \Exception("file [{$this->getOriginalFullPath()}] not found."); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function splitNameAndPath(string $image): array |
47
|
|
|
{ |
48
|
|
|
$pieces = explode('/', $image); |
49
|
|
|
|
50
|
|
|
return [array_pop($pieces), implode('/', $pieces)]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function extractImageInformation(): self |
54
|
|
|
{ |
55
|
|
|
$information = getimagesize($this->getOriginalFullPath()); |
56
|
|
|
|
57
|
|
|
$this->width = $information[0]; |
58
|
|
|
$this->height = $information[1]; |
59
|
|
|
$this->type = explode('/', $information['mime'])[1]; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getOriginalName(): string |
65
|
|
|
{ |
66
|
|
|
if ($this->getPath() === '') { |
67
|
|
|
return $this->getName(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return "{$this->getPath()}/{$this->getName()}"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getOriginalFullPath(): string |
74
|
|
|
{ |
75
|
|
|
if ($this->rootPath === '') { |
76
|
|
|
return $this->getOriginalName(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return "{$this->rootPath}/{$this->getOriginalName()}"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getPath(): string |
83
|
|
|
{ |
84
|
|
|
return $this->path; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getName(): string |
88
|
|
|
{ |
89
|
|
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getType(): string |
93
|
|
|
{ |
94
|
|
|
return $this->type; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getWidth(): int |
98
|
|
|
{ |
99
|
|
|
return $this->width; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getHeight(): int |
103
|
|
|
{ |
104
|
|
|
return $this->height; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getAspectRatio(): float |
108
|
|
|
{ |
109
|
|
|
return round($this->width / $this->height, 2); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getOutputFormat(): string |
113
|
|
|
{ |
114
|
|
|
return $this->outputFormat ?? $this->getType(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setOutputFormat(string $outputFormat): self |
118
|
|
|
{ |
119
|
|
|
$this->outputFormat = $outputFormat; |
120
|
|
|
|
121
|
|
|
$name = explode('.', $this->name); |
122
|
|
|
|
123
|
|
|
array_pop($name); |
124
|
|
|
|
125
|
|
|
$this->name = implode('.', $name) . ".{$this->outputFormat}"; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function isSmallerThan(?int $width, ?int $height): bool |
131
|
|
|
{ |
132
|
|
|
return $this->width <= $width && $this->height <= $height; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function content(): string |
136
|
|
|
{ |
137
|
|
|
return file_get_contents($this->getOriginalFullPath()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|