oc-resizer-plugin/compressors/PngCompressor.php

56 lines
1.4 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-14 02:13:29 +02:00
public function make(string $path): array
{
$output = "/tmp/".str_slug(microtime());
$mimetype = mime_content_type($path);
system('imagemin '.escapeshellarg($path).' --plugin=pngquant > '.escapeshellarg($output));
system("mv ".escapeshellarg($output)." ".escapeshellarg($path));
return [
$path => [$path],
];
}
public function shouldGenerateVersions(): bool
{
return true;
}
2021-09-17 13:29:16 +02:00
public function resize(string $source, string $destination, Collection $size): void
{
$extension = pathinfo($source, PATHINFO_EXTENSION);
$temp = microtime().'.'.$extension;
$r = app(ImageManager::class)->make($source);
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 = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
if (Storage::exists($versionFilename)) {
Storage::delete($versionFilename);
}
Storage::move($temp, $versionFilename);
}
2021-09-14 02:13:29 +02:00
}