Fixed: Resize PDF only once

This commit is contained in:
philipp lang 2021-10-31 22:03:34 +01:00
parent 6b0ab3182c
commit 6daae8e9a3
6 changed files with 66 additions and 28 deletions

View File

@ -35,7 +35,9 @@ class ImageResizer
}
if ($this->file->compressor()->shouldGenerateVersions()) {
$this->file->compressor()->start();
$this->generateVersions();
$this->file->compressor()->end();
}
}
@ -89,7 +91,7 @@ class ImageResizer
private function generateVersions(): void
{
foreach ($this->possibleSizes() as $size) {
$this->file->compressor()->resize($this->file->root(), $this->file->versionsDirPath(), $size);
$this->file->compressor()->resize($size);
}
foreach ($this->file->versions() as $version) {

View File

@ -8,7 +8,7 @@ use Storage;
abstract class Compressor
{
protected $media;
protected MediaPath $media;
abstract function make(string $path): array;
@ -26,9 +26,9 @@ abstract class Compressor
return "/tmp/".str_slug(microtime());
}
protected function versionFilename(string $source, $width, $height): string
protected function versionFilename($width, $height): string
{
return pathinfo($source, PATHINFO_FILENAME).
return $this->media->filename().
'-'.
$width.
'x'.

View File

@ -41,20 +41,30 @@ class JpgCompressor extends Compressor {
return true;
}
public function resize(string $source, string $destination, Collection $size): void
public function resize(Collection $size): void
{
$extension = pathinfo($source, PATHINFO_EXTENSION);
$extension = $this->media->extension();
$temp = microtime().'.'.$extension;
$r = app(ImageManager::class)->make($source)
$r = app(ImageManager::class)->make($this->media->root())
->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
->save(Storage::path($temp));
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
$versionFilename = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
$this->moveTo($temp, $versionFilename);
}
public function start(): void
{
//
}
public function end(): void
{
//
}
}

View File

@ -33,14 +33,31 @@ class PdfCompressor extends Compressor
];
}
private function extractImage(string $pdf): string
private function imagePath(): string
{
$file = $this->tmpPath().'.'.$this->getExtension();
return '/tmp/'.str_slug($this->media->root()).'.jpg';
}
exec('convert -density 150 '.escapeshellarg($pdf.'[0]').' -quality 90 '.escapeshellarg($file), $output, $r);
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)) {
throw new ResizerException('File cannot be generated from PDF file. Root file is "'.$pdf.'"');
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;
@ -50,7 +67,6 @@ class PdfCompressor extends Compressor
{
$filename = $this->extractImage($this->media->root());
$size = getimagesize($filename);
unlink($filename);
return $size;
}
@ -60,20 +76,20 @@ class PdfCompressor extends Compressor
return true;
}
public function resize(string $source, string $destination, Collection $size): void
public function resize(Collection $size): void
{
$temp = $this->extractImage($source);
$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'), fn ($constraint) => $constraint->upsize())
->save($temp);
->fit($size->get('width'), $size->get('height'))
->save($tempBefore);
list($destWidth, $destHeight) = getimagesize($temp);
$versionFilename = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
list($destWidth, $destHeight) = getimagesize($tempBefore);
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
Storage::put($versionFilename, file_get_contents($temp));
unlink($temp);
Storage::put($versionFilename, file_get_contents($tempBefore));
unlink($tempBefore);
}
}

View File

@ -41,19 +41,29 @@ class PngCompressor extends Compressor {
return true;
}
public function resize(string $source, string $destination, Collection $size): void
public function start(): void
{
$extension = pathinfo($source, PATHINFO_EXTENSION);
//
}
public function end(): void
{
//
}
public function resize(Collection $size): void
{
$extension = $this->media->extension();
$temp = microtime().'.'.$extension;
$r = app(ImageManager::class)->make($source);
$r = app(ImageManager::class)->make($this->media->root());
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);
$versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight);
$this->moveTo($temp, $versionFilename);
}

View File

@ -234,10 +234,10 @@ class ResizerTest extends TestCase
$this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
Event::fire('media.file.upload', [null, '/pages/test.pdf', null]);
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
$this->assertHasFile('pages/test-1275x1275.pdf.jpg');
$this->assertHasFile('pages/test-200x259.pdf.jpg');
$this->assertHasFile('pages/test-200x200.pdf.jpg');
$this->assertHasFile('pages/test-1275x1275.pdf.jpg');
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
$this->assertFileCount(4, 'pages');
}