oc-resizer-plugin/compressors/PngCompressor.php

73 lines
1.8 KiB
PHP
Raw Normal View History

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
{
$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
{
//
}
2021-10-31 23:58:16 +01:00
public function resize(Collection $size, bool $update, callable $callback): void
2021-10-31 22:03:34 +01:00
{
$extension = $this->media->extension();
2021-09-17 13:29:16 +02:00
$temp = microtime().'.'.$extension;
2021-12-21 01:01:44 +01:00
$r = app(ImageManager::class)->make($this->media->root())->fit($size->get('width'), $size->get('height'));
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-10-31 23:58:16 +01:00
call_user_func($callback, $this->media, Storage::path($versionFilename));
2021-09-17 13:29:16 +02:00
}
2021-09-14 02:13:29 +02:00
}