|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ImageOptimizer; |
|
6
|
|
|
|
|
7
|
|
|
use Imagine\Image\{Box, ImageInterface}; |
|
8
|
|
|
use ImageOptimizer\Exception\ImageNotFoundException; |
|
9
|
|
|
use ImageOptimizer\Exception\DirectoryNotFoundException; |
|
10
|
|
|
|
|
11
|
|
|
class ImageOptimizer |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ImagineDriver $imagine |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $driver; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string $appPath |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $appPath; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(ImagineDriver $driver, string $resizedFolder, string $appPath) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->driver = $driver; |
|
26
|
|
|
$this->resizedFolder = $resizedFolder; |
|
|
|
|
|
|
27
|
|
|
$this->appPath = $appPath; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Optimizes image |
|
32
|
|
|
* |
|
33
|
|
|
* @access public |
|
34
|
|
|
* @param string $path |
|
35
|
|
|
* @param int $height |
|
36
|
|
|
* @param int $width |
|
37
|
|
|
* @return ImageOptimized |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getOptimizedImage(string $path, int $height, int $width): ImageOptimized |
|
40
|
|
|
{ |
|
41
|
|
|
$relativePath = parse_url($path, PHP_URL_PATH); |
|
42
|
|
|
|
|
43
|
|
|
$pathinfo = pathinfo($relativePath); |
|
44
|
|
|
|
|
45
|
|
|
$absolutePath = $this->appPath . $path; |
|
46
|
|
|
|
|
47
|
|
|
if (! $this->fileExists($absolutePath)) { |
|
48
|
|
|
throw new ImageNotFoundException($absolutePath); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$resizedUrl = $pathinfo['dirname'] . $pathinfo['filename'] . '_' . $height . 'x' . $width . '.' . $pathinfo['extension']; |
|
52
|
|
|
|
|
53
|
|
|
$resizedFile = $this->getBasePath() . $resizedUrl; |
|
54
|
|
|
|
|
55
|
|
|
$optimizedImage = new ImageOptimized([ |
|
56
|
|
|
'original_url' => $path, |
|
57
|
|
|
'resized_url' => $this->resizedFolder . $resizedUrl, |
|
58
|
|
|
'resized_webp_url' => $this->resizedFolder . $resizedUrl . '.webp', |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
if (! $this->fileExists($resizedFile)) { |
|
62
|
|
|
$this->ensureDirectoryExists($resizedFile); |
|
63
|
|
|
$this->driver->process($absolutePath, $width, $height, $resizedFile); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $optimizedImage; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $path |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function ensureDirectoryExists(string $path): void |
|
74
|
|
|
{ |
|
75
|
|
|
$directory = dirname($path); |
|
76
|
|
|
|
|
77
|
|
|
if (! $this->fileExists($directory)) { |
|
78
|
|
|
if (! $this->mkdir($directory)) { |
|
79
|
|
|
throw new DirectoryNotFoundException(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* file_exists wrapper |
|
86
|
|
|
* |
|
87
|
|
|
* @access protected |
|
88
|
|
|
* @param string $path |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function fileExists(string $path): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return file_exists($path); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* mkdir wraper |
|
98
|
|
|
* |
|
99
|
|
|
* @access protected |
|
100
|
|
|
* @param string $directory |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function mkdir(string $directory): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return mkdir($directory, 0755, true); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getBasePath(): string |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->appPath . $this->resizedFolder; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|