From 6daae8e9a38ddba2ab3cbb33720b805c550256f2 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 31 Oct 2021 22:03:34 +0100 Subject: [PATCH] Fixed: Resize PDF only once --- classes/ImageResizer.php | 4 +++- compressors/Compressor.php | 6 ++--- compressors/JpgCompressor.php | 18 ++++++++++---- compressors/PdfCompressor.php | 44 ++++++++++++++++++++++++----------- compressors/PngCompressor.php | 18 ++++++++++---- tests/ResizerTest.php | 4 ++-- 6 files changed, 66 insertions(+), 28 deletions(-) diff --git a/classes/ImageResizer.php b/classes/ImageResizer.php index 2719b37..c5d461f 100644 --- a/classes/ImageResizer.php +++ b/classes/ImageResizer.php @@ -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) { diff --git a/compressors/Compressor.php b/compressors/Compressor.php index bf36957..f2e2929 100644 --- a/compressors/Compressor.php +++ b/compressors/Compressor.php @@ -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'. diff --git a/compressors/JpgCompressor.php b/compressors/JpgCompressor.php index 18d6e7c..ee44fc9 100644 --- a/compressors/JpgCompressor.php +++ b/compressors/JpgCompressor.php @@ -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 + { + // + } + } diff --git a/compressors/PdfCompressor.php b/compressors/PdfCompressor.php index 41bc23a..8afe14d 100644 --- a/compressors/PdfCompressor.php +++ b/compressors/PdfCompressor.php @@ -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); } } diff --git a/compressors/PngCompressor.php b/compressors/PngCompressor.php index a185926..277acf3 100644 --- a/compressors/PngCompressor.php +++ b/compressors/PngCompressor.php @@ -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); } diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index e73b0a7..a42a26e 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -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'); }