2021-09-06 02:31:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer\Classes;
|
|
|
|
|
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;
|
|
|
|
use Media\Classes\MediaLibrary;
|
|
|
|
use October\Rain\Resize\Resizer;
|
|
|
|
use Storage;
|
|
|
|
|
|
|
|
class ImageResizer
|
|
|
|
{
|
|
|
|
|
|
|
|
private FilesystemAdapter $disk;
|
|
|
|
private string $uploadDir;
|
|
|
|
private MediaLibrary $media;
|
2021-09-09 19:18:41 +02:00
|
|
|
private MediaPath $file;
|
2021-09-06 02:31:27 +02:00
|
|
|
|
|
|
|
public function __construct(FilesystemAdapter $disk, string $uploadDir, MediaLibrary $media)
|
|
|
|
{
|
|
|
|
$this->disk = $disk;
|
|
|
|
$this->uploadDir = $uploadDir;
|
|
|
|
$this->media = $media;
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
public function generate(MediaPath $file): void
|
2021-09-06 02:31:27 +02:00
|
|
|
{
|
2021-09-09 19:18:41 +02:00
|
|
|
$this->file = $file;
|
2021-09-06 02:31:27 +02:00
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
if ($this->file->compressor()->shouldGenerateVersions()) {
|
|
|
|
$this->generateVersions();
|
|
|
|
}
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function dimensions(): Collection
|
|
|
|
{
|
2021-09-17 14:22:19 +02:00
|
|
|
[$width, $height] = $this->file->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();
|
|
|
|
$ratios->push($this->dimensions());
|
|
|
|
$ratios = $ratios->merge($this->sizes());
|
|
|
|
|
|
|
|
foreach (collect(Setting::get('breakpoints'))->push($this->dimensions()->get('width'))->unique() as $size) {
|
|
|
|
foreach ($ratios as $ratio) {
|
2021-09-16 01:11:00 +02:00
|
|
|
$width = $size;
|
2021-09-06 02:31:27 +02:00
|
|
|
$height = $size * $ratio->get('height') / $ratio->get('width');
|
|
|
|
|
2021-09-17 14:22:19 +02:00
|
|
|
if ($height > $this->dimensions()->get('height')) {
|
|
|
|
$width = $width * $this->dimensions()->get('height') / $height;
|
|
|
|
$height = $this->dimensions()->get('height');
|
2021-09-16 01:11:00 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 14:22:19 +02:00
|
|
|
if (ceil($width) > $this->dimensions()->get('width') || ceil($height) > $this->dimensions()->get('height')) {
|
2021-09-16 01:11:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-06 02:31:27 +02:00
|
|
|
$return->push(collect([
|
2021-09-16 01:11:00 +02:00
|
|
|
'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) {
|
2021-09-17 13:29:16 +02:00
|
|
|
$this->file->compressor()->resize($this->file->root(), $this->file->versionsDirPath(), $size);
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
2021-09-09 19:18:41 +02:00
|
|
|
|
|
|
|
foreach ($this->file->versions() as $version) {
|
|
|
|
$this->file->compressor()->make($this->disk->path($version->get('path')));
|
|
|
|
}
|
2021-09-06 02:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|