97 lines
2.5 KiB
PHP
97 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Aweos\Resizer\Compressors;
|
|
|
|
use Aweos\Resizer\Exceptions\ResizerException;
|
|
use Illuminate\Support\Collection;
|
|
use Intervention\Image\ImageManager;
|
|
use Storage;
|
|
|
|
class PdfCompressor extends Compressor
|
|
{
|
|
|
|
public function getExtensionRegex(): string
|
|
{
|
|
return 'pdf\.jpg';
|
|
}
|
|
|
|
protected function getExtension(): string
|
|
{
|
|
return 'pdf.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 imagePath(): string
|
|
{
|
|
return '/tmp/'.str_slug($this->media->root()).'.jpg';
|
|
}
|
|
|
|
public function start(): void
|
|
{
|
|
@unlink($this->imagePath());
|
|
}
|
|
|
|
public function end(): void
|
|
{
|
|
@unlink($this->imagePath());
|
|
}
|
|
|
|
private function extractImage(): string
|
|
{
|
|
$file = $this->imagePath();
|
|
|
|
if (!file_exists($file)) {
|
|
exec('convert -density 150 '.escapeshellarg($this->media->root().'[0]').' -quality 90 '.escapeshellarg($file), $output, $r);
|
|
}
|
|
|
|
if (!file_exists($file)) {
|
|
throw new ResizerException('File cannot be generated from PDF file. Root file is "'.$this->media->root().'"');
|
|
}
|
|
|
|
return $file;
|
|
}
|
|
|
|
public function originalSize(): array
|
|
{
|
|
$filename = $this->extractImage($this->media->root());
|
|
$size = getimagesize($filename);
|
|
|
|
return $size;
|
|
}
|
|
|
|
public function shouldGenerateVersions(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function resize(Collection $size, bool $update, callable $callback): void
|
|
{
|
|
$temp = $this->extractImage();
|
|
$tempBefore = PATHINFO($temp, PATHINFO_FILENAME).'compiled.'.pathinfo($temp, PATHINFO_EXTENSION);
|
|
|
|
$r = app(ImageManager::class)->make($temp)
|
|
->fit($size->get('width'), $size->get('height'))
|
|
->save($tempBefore);
|
|
|
|
list($destWidth, $destHeight) = getimagesize($tempBefore);
|
|
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
|
|
|
Storage::put($versionFilename, file_get_contents($tempBefore));
|
|
unlink($tempBefore);
|
|
call_user_func($callback, $this->media, Storage::path($versionFilename));
|
|
}
|
|
|
|
}
|