72 lines
1.6 KiB
PHP
72 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Aweos\Resizer\Compressors;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Intervention\Image\ImageManager;
|
|
use Storage;
|
|
|
|
class PngCompressor extends Compressor {
|
|
|
|
protected function getExtension(): string
|
|
{
|
|
return 'png';
|
|
}
|
|
|
|
public function getExtensionRegex(): string
|
|
{
|
|
return 'png';
|
|
}
|
|
|
|
public function make(string $path): array
|
|
{
|
|
$output = $this->tmpPath();
|
|
$mimetype = mime_content_type($path);
|
|
|
|
system('imagemin '.escapeshellarg($path).' --plugin=pngquant > '.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 start(): void
|
|
{
|
|
//
|
|
}
|
|
|
|
public function end(): void
|
|
{
|
|
//
|
|
}
|
|
|
|
public function resize(Collection $size): void
|
|
{
|
|
$extension = $this->media->extension();
|
|
$temp = microtime().'.'.$extension;
|
|
|
|
$r = app(ImageManager::class)->make($this->media->root());
|
|
app(ImageManager::class)
|
|
->canvas($size->get('width'), $size->get('height'))->insert($r, 'center')
|
|
->save(Storage::path($temp));
|
|
|
|
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
|
|
|
|
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
|
|
|
$this->moveTo($temp, $versionFilename);
|
|
}
|
|
|
|
}
|