This commit is contained in:
philipp lang 2022-02-15 18:15:58 +01:00
parent 55928e7da0
commit 38ac146340
3 changed files with 18 additions and 8 deletions

View File

@ -2,6 +2,7 @@
namespace Aweos\Resizer\Classes;
use Aweos\Resizer\Compressors\Compressor;
use Aweos\Resizer\Exceptions\ResizerException;
use Aweos\Resizer\Lib\MediaPath;
use Aweos\Resizer\Models\Setting;
@ -17,6 +18,7 @@ class ImageResizer
private string $uploadDir;
private MediaPath $file;
private bool $update;
private Compressor $compressor;
public function __construct(FilesystemAdapter $disk, string $uploadDir)
{
@ -27,22 +29,23 @@ class ImageResizer
public function generate(MediaPath $file, bool $update): void
{
$this->file = $file;
$this->compressor = $this->file->compressor();
$this->update = $update;
if (!$file->exists()) {
throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.');
}
if ($this->file->compressor()->shouldGenerateVersions()) {
$this->file->compressor()->start();
if ($this->compressor->shouldGenerateVersions()) {
$this->compressor->start();
$this->generateVersions();
$this->file->compressor()->end();
$this->compressor->end();
}
}
private function dimensions(): Collection
{
[$width, $height] = $this->file->compressor()->originalSize();
[$width, $height] = $this->compressor->originalSize();
return collect(compact('width', 'height'));
}
@ -90,15 +93,13 @@ class ImageResizer
private function generateVersions(): void
{
$compressor = $this->file->compressor();
foreach ($this->possibleSizes() as $size) {
$compressor->resize($size, $this->update, function($media, $file) {
$this->compressor->resize($size, $this->update, function($media, $file) {
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) {
$this->file->compressor()->make($file);
$this->compressor->make($file);
}
});
}

View File

@ -3,6 +3,7 @@
namespace Aweos\Resizer\Compressors;
use Aweos\Resizer\Lib\MediaPath;
use Illuminate\Support\Collection;
use Storage;
abstract class Compressor
@ -13,8 +14,10 @@ abstract class Compressor
abstract function make(string $path): array;
abstract protected function getExtension(): string;
abstract public function getExtensionRegex(): string;
abstract public function shouldGenerateVersions(): bool;
abstract public function start(): void;
abstract public function end(): void;
abstract public function resize(Collection $size, bool $update, callable $callback): void;
public function __construct(MediaPath $media)
{

View File

@ -2,6 +2,8 @@
namespace Aweos\Resizer\Compressors;
use Illuminate\Support\Collection;
class DefaultCompressor extends Compressor {
public function getExtension(): string
@ -34,4 +36,8 @@ class DefaultCompressor extends Compressor {
//
}
public function resize(Collection $size, bool $update, callable $callback): void {
//
}
}