oc-resizer-plugin/compressors/JpgCompressor.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2021-09-09 19:18:41 +02:00
<?php
namespace Aweos\Resizer\Compressors;
2021-09-17 13:29:16 +02:00
use Illuminate\Support\Collection;
use Intervention\Image\ImageManager;
use Storage;
2022-09-10 19:58:54 +02:00
class JpgCompressor extends Compressor
{
2021-09-17 17:09:17 +02:00
public function getExtensionRegex(): string
{
return 'jpg';
}
2021-09-17 13:29:16 +02:00
protected function getExtension(): string
{
return 'jpg';
}
2021-09-09 19:18:41 +02:00
public function make(string $path): array
{
2021-09-17 13:29:16 +02:00
$output = $this->tmpPath();
2021-09-09 19:18:41 +02:00
system('imagemin '.escapeshellarg($path).' --plugin=jpegtran --plugin=mozjpeg --plugin.mozjpeg.quality=70 > '.escapeshellarg($output));
2022-09-10 19:58:54 +02:00
system('mv '.escapeshellarg($output).' '.escapeshellarg($path));
2021-09-09 19:18:41 +02:00
return [
$path => [$path],
];
}
2021-09-17 14:22:19 +02:00
public function originalSize(): array
{
return getimagesize($this->media->root());
}
2021-09-09 19:18:41 +02:00
public function shouldGenerateVersions(): bool
{
return true;
}
2021-10-31 23:58:16 +01:00
public function resize(Collection $size, bool $update, callable $callback): void
2021-09-17 13:29:16 +02:00
{
2021-10-31 22:03:34 +01:00
$extension = $this->media->extension();
2021-09-17 13:29:16 +02:00
$temp = microtime().'.'.$extension;
2021-10-31 22:03:34 +01:00
$r = app(ImageManager::class)->make($this->media->root())
2021-09-17 13:29:16 +02:00
->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
->save(Storage::path($temp));
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
2021-10-31 22:03:34 +01:00
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
2021-09-17 13:29:16 +02:00
2021-10-31 23:58:16 +01:00
if ($update && Storage::exists($versionFilename)) {
return;
}
2021-09-17 13:31:44 +02:00
$this->moveTo($temp, $versionFilename);
2021-10-31 23:58:16 +01:00
call_user_func($callback, $this->media, Storage::path($versionFilename));
2021-09-17 13:29:16 +02:00
}
2021-10-31 22:03:34 +01:00
public function start(): void
{
}
public function end(): void
{
}
2021-09-09 19:18:41 +02:00
}