oc-resizer-plugin/compressors/PdfCompressor.php

94 lines
2.5 KiB
PHP
Raw Normal View History

2021-09-17 14:22:19 +02:00
<?php
namespace Aweos\Resizer\Compressors;
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
{
2022-02-15 18:34:31 +01:00
private string $originalImage;
2021-09-17 17:09:17 +02:00
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));
2022-09-10 19:58:54 +02:00
system('mv '.escapeshellarg($output).' '.escapeshellarg($path));
2021-09-17 14:22:19 +02:00
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());
2022-02-15 18:34:31 +01:00
$this->originalImage = $this->extractImage();
2021-10-31 22:03:34 +01:00
}
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
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-09-17 14:22:19 +02:00
return $file;
}
public function originalSize(): array
{
2022-02-15 18:34:31 +01:00
return getimagesize($this->originalImage);
2021-09-17 14:22:19 +02:00
}
public function shouldGenerateVersions(): bool
{
return true;
}
2021-10-31 23:58:16 +01:00
public function resize(Collection $size, bool $update, callable $callback): void
2021-09-17 14:22:19 +02:00
{
2022-09-10 19:58:54 +02:00
$tempBefore = pathinfo($this->originalImage, PATHINFO_FILENAME).'compiled.'.pathinfo($this->originalImage, PATHINFO_EXTENSION);
2021-09-17 14:22:19 +02:00
2022-02-15 18:34:31 +01:00
$r = app(ImageManager::class)->make($this->originalImage)
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 22:03:34 +01:00
Storage::put($versionFilename, file_get_contents($tempBefore));
unlink($tempBefore);
2021-10-31 23:58:16 +01:00
call_user_func($callback, $this->media, Storage::path($versionFilename));
2021-09-17 14:22:19 +02:00
}
}