oc-resizer-plugin/classes/ImageResizer.php

105 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2021-09-06 02:31:27 +02:00
<?php
namespace Aweos\Resizer\Classes;
2022-02-15 18:15:58 +01:00
use Aweos\Resizer\Compressors\Compressor;
use Aweos\Resizer\Exceptions\ResizerException;
2021-09-09 19:18:41 +02:00
use Aweos\Resizer\Lib\MediaPath;
2021-09-06 02:31:27 +02:00
use Aweos\Resizer\Models\Setting;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Collection;
class ImageResizer
{
private FilesystemAdapter $disk;
private string $uploadDir;
2021-09-09 19:18:41 +02:00
private MediaPath $file;
2021-10-31 23:58:16 +01:00
private bool $update;
2022-02-15 18:15:58 +01:00
private Compressor $compressor;
2021-09-06 02:31:27 +02:00
2021-12-17 01:27:57 +01:00
public function __construct(FilesystemAdapter $disk, string $uploadDir)
2021-09-06 02:31:27 +02:00
{
$this->disk = $disk;
$this->uploadDir = $uploadDir;
}
2021-10-31 23:58:16 +01:00
public function generate(MediaPath $file, bool $update): void
2021-09-06 02:31:27 +02:00
{
2021-09-09 19:18:41 +02:00
$this->file = $file;
2022-02-15 18:15:58 +01:00
$this->compressor = $this->file->compressor();
2021-10-31 23:58:16 +01:00
$this->update = $update;
2021-09-06 02:31:27 +02:00
if (!$file->exists()) {
throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.');
}
2022-02-15 18:15:58 +01:00
if ($this->compressor->shouldGenerateVersions()) {
$this->compressor->start();
2021-09-09 19:18:41 +02:00
$this->generateVersions();
2022-02-15 18:15:58 +01:00
$this->compressor->end();
2021-09-09 19:18:41 +02:00
}
2021-09-06 02:31:27 +02:00
}
private function dimensions(): Collection
{
2022-02-15 18:15:58 +01:00
[$width, $height] = $this->compressor->originalSize();
2021-09-06 02:31:27 +02:00
return collect(compact('width', 'height'));
}
private function sizes(): Collection
{
$sizes = Setting::get('sizes');
return collect(array_column($sizes, 'aspect_ratio'))->map(
fn ($ratio) => collect(array_combine(['width', 'height'], explode('x', $ratio))),
);
}
private function possibleSizes(): Collection
{
$return = collect([]);
$ratios = collect();
2022-02-15 17:46:29 +01:00
$dimensions = $this->dimensions();
$ratios->push($dimensions);
2021-09-06 02:31:27 +02:00
$ratios = $ratios->merge($this->sizes());
2022-02-15 17:46:29 +01:00
foreach (collect(Setting::get('breakpoints'))->push($dimensions->get('width'))->unique() as $size) {
2021-09-06 02:31:27 +02:00
foreach ($ratios as $ratio) {
$width = $size;
2021-09-06 02:31:27 +02:00
$height = $size * $ratio->get('height') / $ratio->get('width');
2022-02-15 17:46:29 +01:00
if ($height > $dimensions->get('height')) {
$width = $width * $dimensions->get('height') / $height;
$height = $dimensions->get('height');
}
2022-02-15 17:46:29 +01:00
if (ceil($width) > $dimensions->get('width') || ceil($height) > $dimensions->get('height')) {
continue;
}
2021-09-06 02:31:27 +02:00
$return->push(collect([
'width' => round($width),
2021-09-06 02:31:27 +02:00
'height' => round($height),
]));
}
}
return $return;
}
private function generateVersions(): void
{
foreach ($this->possibleSizes() as $size) {
2022-09-10 19:58:24 +02:00
$this->compressor->resize($size, $this->update, function ($media, $file) {
2021-10-31 23:58:16 +01:00
if (!file_exists($file)) {
throw new ResizerException('File versions cannot be generated. Version file "'.$file.'" of "'.$this->file->root().'" doesnt exist.');
}
if (file_exists($file) || !$this->update) {
2022-02-15 18:15:58 +01:00
$this->compressor->make($file);
2021-10-31 23:58:16 +01:00
}
});
2021-09-09 19:18:41 +02:00
}
2021-09-06 02:31:27 +02:00
}
}