oc-resizer-plugin/compressors/JpgCompressor.php

76 lines
1.8 KiB
PHP

<?php
namespace Aweos\Resizer\Compressors;
use Aweos\Resizer\Exceptions\ResizerException;
use Illuminate\Support\Collection;
use Intervention\Image\ImageManager;
use Storage;
class JpgCompressor extends Compressor {
public function getExtensionRegex(): string
{
return 'jpg';
}
protected function getExtension(): string
{
return 'jpg';
}
public function make(string $path): array
{
$output = $this->tmpPath();
system('imagemin '.escapeshellarg($path).' --plugin=jpegtran --plugin=mozjpeg --plugin.mozjpeg.quality=70 > '.escapeshellarg($output));
system("mv ".escapeshellarg($output)." ".escapeshellarg($path));
return [
$path => [$path],
];
}
public function originalSize(): array
{
return getimagesize($this->media->root());
}
public function shouldGenerateVersions(): bool
{
return true;
}
public function resize(Collection $size, bool $update, callable $callback): void
{
$extension = $this->media->extension();
$temp = microtime().'.'.$extension;
$r = app(ImageManager::class)->make($this->media->root())
->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
->save(Storage::path($temp));
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
if ($update && Storage::exists($versionFilename)) {
return;
}
$this->moveTo($temp, $versionFilename);
call_user_func($callback, $this->media, Storage::path($versionFilename));
}
public function start(): void
{
//
}
public function end(): void
{
//
}
}