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;
|
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
class JpgCompressor extends Compressor {
|
|
|
|
|
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
|
|
|
$mimetype = mime_content_type($path);
|
|
|
|
|
|
|
|
system('imagemin '.escapeshellarg($path).' --plugin=jpegtran --plugin=mozjpeg --plugin.mozjpeg.quality=70 > '.escapeshellarg($output));
|
|
|
|
system("mv ".escapeshellarg($output)." ".escapeshellarg($path));
|
|
|
|
|
|
|
|
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-09-17 13:29:16 +02:00
|
|
|
public function resize(string $source, string $destination, Collection $size): void
|
|
|
|
{
|
|
|
|
$extension = pathinfo($source, PATHINFO_EXTENSION);
|
|
|
|
$temp = microtime().'.'.$extension;
|
|
|
|
|
|
|
|
$r = app(ImageManager::class)->make($source)
|
|
|
|
->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
|
|
|
|
->save(Storage::path($temp));
|
|
|
|
|
|
|
|
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
|
|
|
|
|
2021-09-17 14:22:19 +02:00
|
|
|
$versionFilename = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
|
2021-09-17 13:29:16 +02:00
|
|
|
|
2021-09-17 13:31:44 +02:00
|
|
|
$this->moveTo($temp, $versionFilename);
|
2021-09-17 13:29:16 +02:00
|
|
|
}
|
|
|
|
|
2021-09-09 19:18:41 +02:00
|
|
|
}
|