Performance fixes
This commit is contained in:
parent
38ac146340
commit
e6d9d0d941
|
@ -15,36 +15,77 @@ use Storage;
|
||||||
class TagGenerator {
|
class TagGenerator {
|
||||||
|
|
||||||
public array $breakpoints;
|
public array $breakpoints;
|
||||||
|
private $compressor;
|
||||||
public $path;
|
public $path;
|
||||||
private CompressorFactory $compressorFactory;
|
|
||||||
private $defaultOptions = [
|
private $defaultOptions = [
|
||||||
'lazy' => false,
|
'lazy' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(CompressorFactory $compressorFactory)
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->compressorFactory = $compressorFactory;
|
|
||||||
$this->breakpoints = Setting::get('breakpoints');
|
$this->breakpoints = Setting::get('breakpoints');
|
||||||
ksort($this->breakpoints);
|
ksort($this->breakpoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function size(string $name): array
|
public function generate(MediaPath $path, ?string $ratio = 'original', $sizes = null, array $options = []): string
|
||||||
{
|
{
|
||||||
$size = collect(Setting::get('sizes'))->filter(fn ($size) => $size['name'] === $name)
|
$options = array_merge($this->defaultOptions, $options);
|
||||||
->first();
|
$this->path = $path;
|
||||||
|
$this->compressor = $this->path->compressor();
|
||||||
|
$this->compressor->start();
|
||||||
|
|
||||||
if (is_null($size)) {
|
try {
|
||||||
throw new ResizerException("Size with name {$name} not found.");
|
if (!$this->compressor->shouldGenerateVersions()) {
|
||||||
|
return $this->fallback();
|
||||||
|
}
|
||||||
|
} catch (CompressorNotFoundException $e) {
|
||||||
|
return $this->fallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
return explode('x', $size['aspect_ratio']);
|
$files = $this->possibleFiles($ratio);
|
||||||
|
|
||||||
|
if ($files === null) {
|
||||||
|
return $this->fallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
$sizes = $this->parseSizes($files, $sizes);
|
||||||
|
|
||||||
|
$srcset = $files->map(function($file) {
|
||||||
|
return "{$file->get('url')} {$file->get('width')}w";
|
||||||
|
});
|
||||||
|
|
||||||
|
$html = $this->htmlAttributes(collect([
|
||||||
|
'width' => $files->last()->get('width'),
|
||||||
|
'height' => $files->last()->get('height'),
|
||||||
|
'sizes' => $sizes->implode(', '),
|
||||||
|
$options['lazy'] ? 'data-srcset' : 'srcset' => $srcset->implode(', '),
|
||||||
|
$options['lazy'] ? 'data-src' : 'src' => $files->last()->get('url'),
|
||||||
|
'class' => data_get($options, 'class'),
|
||||||
|
])->filter(fn ($value) => (bool) $value));
|
||||||
|
|
||||||
|
$this->compressor->end();
|
||||||
|
|
||||||
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function possibleFiles(string $ratio): ?Collection
|
public function singleFile(MediaPath $path, ?string $ratio = 'original'): string
|
||||||
|
{
|
||||||
|
$this->path = $path;
|
||||||
|
$this->compressor = $this->path->compressor();
|
||||||
|
$this->compressor->start();
|
||||||
|
$versions = $this->possibleFiles($ratio);
|
||||||
|
throw_unless($versions && count($versions), Exception::class, "No files for {$path->root()} found");
|
||||||
|
$possibleFiles = url($this->possibleFiles($ratio)->last()->get('url'));
|
||||||
|
$this->compressor->end();
|
||||||
|
|
||||||
|
return $possibleFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function possibleFiles(string $ratio): ?Collection
|
||||||
{
|
{
|
||||||
$filename = $this->path->filename();
|
$filename = $this->path->filename();
|
||||||
$basePath = $this->path->versionsDirPath();
|
$basePath = $this->path->versionsDirPath();
|
||||||
[$originalWidth, $originalHeight] = $this->path->compressor()->originalSize();
|
[$originalWidth, $originalHeight] = $this->compressor->originalSize();
|
||||||
$aspectRatio = $ratio === 'original'
|
$aspectRatio = $ratio === 'original'
|
||||||
? $originalWidth / $originalHeight
|
? $originalWidth / $originalHeight
|
||||||
: $this->size($ratio)[0] / $this->size($ratio)[1];
|
: $this->size($ratio)[0] / $this->size($ratio)[1];
|
||||||
|
@ -69,52 +110,6 @@ class TagGenerator {
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate(MediaPath $path, ?string $ratio = 'original', $sizes = null, array $options = []): string
|
|
||||||
{
|
|
||||||
$options = array_merge($this->defaultOptions, $options);
|
|
||||||
$this->path = $path;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!$path->compressor()->shouldGenerateVersions()) {
|
|
||||||
return $this->fallback();
|
|
||||||
}
|
|
||||||
} catch (CompressorNotFoundException $e) {
|
|
||||||
return $this->fallback();
|
|
||||||
}
|
|
||||||
|
|
||||||
$files = $this->possibleFiles($ratio);
|
|
||||||
|
|
||||||
if ($files === null) {
|
|
||||||
return $this->fallback();
|
|
||||||
}
|
|
||||||
|
|
||||||
$sizes = $this->parseSizes($files, $sizes);
|
|
||||||
|
|
||||||
$srcset = $files->map(function($file) {
|
|
||||||
return "{$file->get('url')} {$file->get('width')}w";
|
|
||||||
});
|
|
||||||
|
|
||||||
return $this->htmlAttributes(collect([
|
|
||||||
'width' => $files->last()->get('width'),
|
|
||||||
'height' => $files->last()->get('height'),
|
|
||||||
'sizes' => $sizes->implode(', '),
|
|
||||||
$options['lazy'] ? 'data-srcset' : 'srcset' => $srcset->implode(', '),
|
|
||||||
$options['lazy'] ? 'data-src' : 'src' => $files->last()->get('url'),
|
|
||||||
'class' => data_get($options, 'class'),
|
|
||||||
])->filter(fn ($value) => (bool) $value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function singleFile(MediaPath $path, ?string $ratio = 'original'): string
|
|
||||||
{
|
|
||||||
$this->path = $path;
|
|
||||||
|
|
||||||
$versions = $this->possibleFiles($ratio);
|
|
||||||
|
|
||||||
throw_unless($versions && count($versions), Exception::class, "No files for {$path->root()} found");
|
|
||||||
|
|
||||||
return url($this->possibleFiles($ratio)->last()->get('url'));
|
|
||||||
}
|
|
||||||
|
|
||||||
private function htmlAttributes($attr) {
|
private function htmlAttributes($attr) {
|
||||||
return $attr->map(function($value, $key) {
|
return $attr->map(function($value, $key) {
|
||||||
return "{$key}=\"{$value}\"";
|
return "{$key}=\"{$value}\"";
|
||||||
|
@ -146,4 +141,17 @@ class TagGenerator {
|
||||||
{
|
{
|
||||||
return 'src="'.$this->path->publicUrl().'"';
|
return 'src="'.$this->path->publicUrl().'"';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function size(string $name): array
|
||||||
|
{
|
||||||
|
$size = collect(Setting::get('sizes'))->filter(fn ($size) => $size['name'] === $name)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (is_null($size)) {
|
||||||
|
throw new ResizerException("Size with name {$name} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return explode('x', $size['aspect_ratio']);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ use Storage;
|
||||||
class PdfCompressor extends Compressor
|
class PdfCompressor extends Compressor
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private string $originalImage;
|
||||||
|
|
||||||
public function getExtensionRegex(): string
|
public function getExtensionRegex(): string
|
||||||
{
|
{
|
||||||
return 'pdf\.jpg';
|
return 'pdf\.jpg';
|
||||||
|
@ -41,6 +43,7 @@ class PdfCompressor extends Compressor
|
||||||
public function start(): void
|
public function start(): void
|
||||||
{
|
{
|
||||||
@unlink($this->imagePath());
|
@unlink($this->imagePath());
|
||||||
|
$this->originalImage = $this->extractImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function end(): void
|
public function end(): void
|
||||||
|
@ -65,10 +68,7 @@ class PdfCompressor extends Compressor
|
||||||
|
|
||||||
public function originalSize(): array
|
public function originalSize(): array
|
||||||
{
|
{
|
||||||
$filename = $this->extractImage($this->media->root());
|
return getimagesize($this->originalImage);
|
||||||
$size = getimagesize($filename);
|
|
||||||
|
|
||||||
return $size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shouldGenerateVersions(): bool
|
public function shouldGenerateVersions(): bool
|
||||||
|
@ -78,10 +78,9 @@ class PdfCompressor extends Compressor
|
||||||
|
|
||||||
public function resize(Collection $size, bool $update, callable $callback): void
|
public function resize(Collection $size, bool $update, callable $callback): void
|
||||||
{
|
{
|
||||||
$temp = $this->extractImage();
|
$tempBefore = PATHINFO($this->originalImage, PATHINFO_FILENAME).'compiled.'.pathinfo($this->originalImage, PATHINFO_EXTENSION);
|
||||||
$tempBefore = PATHINFO($temp, PATHINFO_FILENAME).'compiled.'.pathinfo($temp, PATHINFO_EXTENSION);
|
|
||||||
|
|
||||||
$r = app(ImageManager::class)->make($temp)
|
$r = app(ImageManager::class)->make($this->originalImage)
|
||||||
->fit($size->get('width'), $size->get('height'))
|
->fit($size->get('width'), $size->get('height'))
|
||||||
->save($tempBefore);
|
->save($tempBefore);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue