2021-09-17 14:22:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aweos\Resizer\Compressors;
|
|
|
|
|
2021-10-31 12:59:34 +01:00
|
|
|
use Aweos\Resizer\Exceptions\ResizerException;
|
2021-09-17 14:22:19 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Intervention\Image\ImageManager;
|
|
|
|
use Storage;
|
|
|
|
|
2021-09-17 17:09:17 +02:00
|
|
|
class PdfCompressor extends Compressor
|
|
|
|
{
|
|
|
|
|
|
|
|
public function getExtensionRegex(): string
|
|
|
|
{
|
|
|
|
return 'pdf\.jpg';
|
|
|
|
}
|
2021-09-17 14:22:19 +02:00
|
|
|
|
|
|
|
protected function getExtension(): string
|
|
|
|
{
|
2021-09-17 14:28:41 +02:00
|
|
|
return 'pdf.jpg';
|
2021-09-17 14:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-10-31 22:03:34 +01:00
|
|
|
private function imagePath(): string
|
2021-09-17 14:22:19 +02:00
|
|
|
{
|
2021-10-31 22:03:34 +01:00
|
|
|
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();
|
2021-09-17 14:22:19 +02:00
|
|
|
|
2021-10-31 22:03:34 +01:00
|
|
|
if (!file_exists($file)) {
|
|
|
|
exec('convert -density 150 '.escapeshellarg($this->media->root().'[0]').' -quality 90 '.escapeshellarg($file), $output, $r);
|
|
|
|
}
|
2021-09-17 14:22:19 +02:00
|
|
|
|
2021-10-31 12:59:34 +01:00
|
|
|
if (!file_exists($file)) {
|
2021-10-31 22:03:34 +01:00
|
|
|
throw new ResizerException('File cannot be generated from PDF file. Root file is "'.$this->media->root().'"');
|
2021-10-31 12:59:34 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 14:22:19 +02:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function originalSize(): array
|
|
|
|
{
|
2021-09-20 11:11:04 +02:00
|
|
|
$filename = $this->extractImage($this->media->root());
|
|
|
|
$size = getimagesize($filename);
|
|
|
|
|
|
|
|
return $size;
|
2021-09-17 14:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldGenerateVersions(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-31 22:03:34 +01:00
|
|
|
public function resize(Collection $size): void
|
2021-09-17 14:22:19 +02:00
|
|
|
{
|
2021-10-31 22:03:34 +01:00
|
|
|
$temp = $this->extractImage();
|
|
|
|
$tempBefore = PATHINFO($temp, PATHINFO_FILENAME).'compiled.'.pathinfo($temp, PATHINFO_EXTENSION);
|
2021-09-17 14:22:19 +02:00
|
|
|
|
|
|
|
$r = app(ImageManager::class)->make($temp)
|
2021-10-31 22:03:34 +01:00
|
|
|
->fit($size->get('width'), $size->get('height'))
|
|
|
|
->save($tempBefore);
|
2021-09-17 14:22:19 +02:00
|
|
|
|
2021-10-31 22:03:34 +01:00
|
|
|
list($destWidth, $destHeight) = getimagesize($tempBefore);
|
|
|
|
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
|
2021-10-31 13:04:26 +01:00
|
|
|
|
2021-10-31 22:03:34 +01:00
|
|
|
Storage::put($versionFilename, file_get_contents($tempBefore));
|
|
|
|
unlink($tempBefore);
|
2021-09-17 14:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|