2021-09-14 02:13:29 +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-14 02:13:29 +02:00
|
|
|
class PngCompressor extends Compressor {
|
|
|
|
|
2021-09-17 13:29:16 +02:00
|
|
|
protected function getExtension(): string
|
|
|
|
{
|
|
|
|
return 'png';
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
public function getExtensionRegex(): string
|
|
|
|
{
|
|
|
|
return 'png';
|
|
|
|
}
|
|
|
|
|
2021-09-14 02:13:29 +02:00
|
|
|
public function make(string $path): array
|
|
|
|
{
|
2021-10-31 15:04:00 +01:00
|
|
|
$output = $this->tmpPath();
|
2021-09-14 02:13:29 +02:00
|
|
|
$mimetype = mime_content_type($path);
|
|
|
|
|
|
|
|
system('imagemin '.escapeshellarg($path).' --plugin=pngquant > '.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-14 02:13:29 +02:00
|
|
|
public function shouldGenerateVersions(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-31 22:03:34 +01:00
|
|
|
public function start(): void
|
2021-09-17 13:29:16 +02:00
|
|
|
{
|
2021-10-31 22:03:34 +01:00
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
public function end(): void
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resize(Collection $size): void
|
|
|
|
{
|
|
|
|
$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
|
|
|
app(ImageManager::class)
|
|
|
|
->canvas($size->get('width'), $size->get('height'))->insert($r, 'center')
|
|
|
|
->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-09-17 13:31:44 +02:00
|
|
|
$this->moveTo($temp, $versionFilename);
|
2021-09-17 13:29:16 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 02:13:29 +02:00
|
|
|
}
|