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