63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Aweos\Resizer\Compressors;
|
||
|
|
||
|
use Illuminate\Support\Collection;
|
||
|
use Intervention\Image\ImageManager;
|
||
|
use Storage;
|
||
|
|
||
|
class PdfCompressor 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],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
private function extractImage(string $pdf): string
|
||
|
{
|
||
|
$file = $this->tmpPath().'.jpg';
|
||
|
|
||
|
exec('convert -density 150 '.escapeshellarg($pdf.'[0]').' -quality 90 '.escapeshellarg($file), $output, $r);
|
||
|
|
||
|
return $file;
|
||
|
}
|
||
|
|
||
|
public function originalSize(): array
|
||
|
{
|
||
|
return getimagesize($this->extractImage($this->media->root()));
|
||
|
}
|
||
|
|
||
|
public function shouldGenerateVersions(): bool
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public function resize(string $source, string $destination, Collection $size): void
|
||
|
{
|
||
|
$temp = $this->extractImage($source);
|
||
|
|
||
|
$r = app(ImageManager::class)->make($temp)
|
||
|
->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
|
||
|
->save($temp);
|
||
|
|
||
|
list($destWidth, $destHeight) = getimagesize($temp);
|
||
|
$versionFilename = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
|
||
|
|
||
|
Storage::put($versionFilename, file_get_contents($temp));
|
||
|
}
|
||
|
|
||
|
}
|