oc-resizer-plugin/compressors/JpgCompressor.php

56 lines
1.5 KiB
PHP

<?php
namespace Aweos\Resizer\Compressors;
use Illuminate\Support\Collection;
use Intervention\Image\ImageManager;
use Storage;
class JpgCompressor extends Compressor {
protected function getExtension(): string
{
return 'jpg';
}
public function make(string $path): array
{
$output = $this->tmpPath();
$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],
];
}
public function originalSize(): array
{
return getimagesize($this->media->root());
}
public function shouldGenerateVersions(): bool
{
return true;
}
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));
$versionFilename = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
$this->moveTo($temp, $versionFilename);
}
}