Create PDF image files

This commit is contained in:
Philipp Lang 2021-09-17 15:09:17 +00:00
parent 5cb79c75a6
commit 2c3400715c
11 changed files with 82 additions and 49 deletions

View File

@ -14,6 +14,8 @@ abstract class Compressor
abstract protected function getExtension(); abstract protected function getExtension();
abstract public function getExtensionRegex(): string;
public function __construct(MediaPath $media) public function __construct(MediaPath $media)
{ {
$this->media = $media; $this->media = $media;

View File

@ -9,6 +9,11 @@ class DefaultCompressor extends Compressor {
return ''; return '';
} }
public function getExtensionRegex(): string
{
return '[a-zA-Z0-9]*';
}
public function make(string $path): array public function make(string $path): array
{ {
return []; return [];

View File

@ -15,6 +15,12 @@ class Factory
'application/pdf' => PdfCompressor::class, 'application/pdf' => PdfCompressor::class,
]; ];
public array $extensions = [
'jpg' => JpgCompressor::class,
'png' => PngCompressor::class,
'pdf' => PdfCompressor::class,
];
public function fromMedia(string $mediaPath): Compressor public function fromMedia(string $mediaPath): Compressor
{ {
return $this->resolve(new MediaPath($mediaPath)); return $this->resolve(new MediaPath($mediaPath));
@ -22,7 +28,9 @@ class Factory
public function resolve(MediaPath $path): Compressor public function resolve(MediaPath $path): Compressor
{ {
$compiler = $this->resolveType($path->type()); $compiler = is_null($path->type())
? $this->resolveExtension($path->extension())
: $this->resolveType($path->type());
if (is_null($compiler)) { if (is_null($compiler)) {
return new $this->default($path); return new $this->default($path);
@ -36,4 +44,9 @@ class Factory
return collect($this->types)->get($type); return collect($this->types)->get($type);
} }
private function resolveExtension(string $extension): ?string
{
return collect($this->extensions)->get($extension);
}
} }

View File

@ -8,6 +8,11 @@ use Storage;
class JpgCompressor extends Compressor { class JpgCompressor extends Compressor {
public function getExtensionRegex(): string
{
return 'jpg';
}
protected function getExtension(): string protected function getExtension(): string
{ {
return 'jpg'; return 'jpg';

View File

@ -6,7 +6,13 @@ use Illuminate\Support\Collection;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use Storage; use Storage;
class PdfCompressor extends Compressor { class PdfCompressor extends Compressor
{
public function getExtensionRegex(): string
{
return 'pdf\.jpg';
}
protected function getExtension(): string protected function getExtension(): string
{ {

View File

@ -13,6 +13,11 @@ class PngCompressor extends Compressor {
return 'png'; return 'png';
} }
public function getExtensionRegex(): string
{
return 'png';
}
public function make(string $path): array public function make(string $path): array
{ {
$output = "/tmp/".str_slug(microtime()); $output = "/tmp/".str_slug(microtime());

View File

@ -44,8 +44,12 @@ class MediaPath
return pathinfo($this->path, PATHINFO_FILENAME); return pathinfo($this->path, PATHINFO_FILENAME);
} }
public function type(): string public function type(): ?string
{ {
if (!file_exists($this->root())) {
return null;
}
return mime_content_type($this->root()); return mime_content_type($this->root());
} }
@ -86,7 +90,7 @@ class MediaPath
$return = collect([]); $return = collect([]);
foreach (Storage::files($this->versionsDirPath()) as $file) { foreach (Storage::files($this->versionsDirPath()) as $file) {
if (!preg_match_all('|('.preg_quote($this->filename(), '|').')(-[0-9]+x[0-9]+)?(\.[a-zA-Z]+)$|', $file, $matches)) { if (!preg_match_all('|('.preg_quote($this->filename(), '|').')(-[0-9]+x[0-9]+)?(\.'.$this->compressor()->getExtensionRegex().'+)$|', $file, $matches)) {
continue; continue;
} }

View File

@ -73,4 +73,16 @@ class DeleteTest extends TestCase
$this->assertDoesntHaveFile('pages/test-300x500.jpg'); $this->assertDoesntHaveFile('pages/test-300x500.jpg');
} }
public function testItDeletesVersionsOfAPdf()
{
Setting::set('folders', ['pages']);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-100x100.pdf.jpg', 'local');
Event::fire('media.file.delete', [null, '/pages/test.pdf', null]);
$this->assertDoesntHaveFile('pages/test-100x100.pdf.jpg');
}
} }

View File

@ -135,7 +135,7 @@ class ImageTagTest extends TestCase
Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]);
Setting::set('breakpoints', [640]); Setting::set('breakpoints', [640]);
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
$output = app(Twig::class)->parse('{{"pages/test.svg" | resize}}'); $output = app(Twig::class)->parse('{{"pages/test.svg" | resize}}');
$this->assertFalse(str_contains($output, 'srcset')); $this->assertFalse(str_contains($output, 'srcset'));

View File

@ -24,14 +24,12 @@ class MoveTest extends TestCase
Setting::set('folders', ['pages']); Setting::set('folders', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image(100, 100)->get()); $this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages/alt', 'test.jpg', 'local'); UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages/alt', 'test-200x300.jpg', 'local');
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages/alt', 'test-200x300.jpg', 'local');
Event::fire('media.file.move', [null, '/pages/alt/test.jpg', '/pages/neu/test.jpg']); Event::fire('media.file.move', [null, '/pages/alt/test.jpg', '/pages/neu/test.jpg']);
$this->assertFileCount(0, 'pages/alt'); $this->assertFileCount(0, 'pages/alt');
$this->assertHasFile('pages/neu/test.jpg');
$this->assertHasFile('pages/neu/test-200x300.jpg'); $this->assertHasFile('pages/neu/test-200x300.jpg');
$this->assertNull(Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->get('resizer.original.pages/alt/test.jpg')); $this->assertNull(Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->get('resizer.original.pages/alt/test.jpg'));
} }
@ -41,14 +39,12 @@ class MoveTest extends TestCase
Setting::set('folders', ['pages']); Setting::set('folders', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image(100, 100)->get()); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local');
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local'); UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local');
Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']); Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);
$this->assertFileCount(2, 'pages'); $this->assertFileCount(1, 'pages');
$this->assertHasFile('pages/testneu.jpg');
$this->assertHasFile('pages/testneu-200x300.jpg'); $this->assertHasFile('pages/testneu-200x300.jpg');
} }
@ -58,15 +54,14 @@ class MoveTest extends TestCase
Setting::set('folders', ['pages']); Setting::set('folders', ['pages']);
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image(100, 100)->get()); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local'); UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local');
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local'); UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'testother-200x300.jpg', 'local');
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'testother.jpg', 'local');
Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']); Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);
$this->assertFileCount(3, 'pages'); $this->assertFileCount(2, 'pages');
$this->assertHasFile('pages/testneu.jpg'); $this->assertHasFile('pages/testother-200x300.jpg');
$this->assertHasFile('pages/testneu-200x300.jpg'); $this->assertHasFile('pages/testneu-200x300.jpg');
$this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg')); $this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg'));
} }

View File

@ -16,6 +16,7 @@ class ResizerTest extends TestCase
{ {
parent::setUp(); parent::setUp();
Storage::fake('local'); Storage::fake('local');
$this->media = MediaLibrary::instance();
} }
public function testDontCopyOriginalFileWhenSettingsAreNotSet(): void public function testDontCopyOriginalFileWhenSettingsAreNotSet(): void
@ -25,8 +26,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.jpg', $file);
$media->put('/pages/test.jpg', $file);
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(0, ''); $this->assertFileCount(0, '');
@ -39,8 +39,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.jpg', $file->get());
$media->put('/pages/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(1, 'pages'); $this->assertFileCount(1, 'pages');
@ -55,8 +54,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.jpg', $file->get());
$media->put('/pages/test.jpg', $file->get());
Event::fire('media.file.upload', [null, 'pages/test.jpg', null]); Event::fire('media.file.upload', [null, 'pages/test.jpg', null]);
$this->assertFileCount(1, 'pages'); $this->assertFileCount(1, 'pages');
@ -70,8 +68,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$media = MediaLibrary::instance(); $this->media->put('/pages-neu/test.jpg', $file->get());
$media->put('/pages-neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages-neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages-neu/test.jpg', null]);
$this->assertFileCount(0, 'pages'); $this->assertFileCount(0, 'pages');
@ -85,8 +82,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', []); Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$media = MediaLibrary::instance(); $this->media->put('/pages/neu/test.jpg', $file->get());
$media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(1, 'pages/neu'); $this->assertFileCount(1, 'pages/neu');
@ -99,8 +95,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', ['250']); Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 500, 600); $file = UploadedFile::fake()->image('test.jpg', 500, 600);
$media = MediaLibrary::instance(); $this->media->put('/pages/neu/test.jpg', $file->get());
$media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(2, 'pages/neu'); $this->assertFileCount(2, 'pages/neu');
@ -115,8 +110,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', ['250']); Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 500, 100); $file = UploadedFile::fake()->image('test.jpg', 500, 100);
$media = MediaLibrary::instance(); $this->media->put('/pages/neu/test.jpg', $file->get());
$media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertHasFile('pages/neu/test-250x50.jpg'); $this->assertHasFile('pages/neu/test-250x50.jpg');
@ -129,8 +123,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', ['250']); Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 250, 1000); $file = UploadedFile::fake()->image('test.jpg', 250, 1000);
$media = MediaLibrary::instance(); $this->media->put('/pages/neu/test.jpg', $file->get());
$media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(1, 'pages/neu'); $this->assertFileCount(1, 'pages/neu');
@ -143,8 +136,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', ['250']); Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 249, 1000); $file = UploadedFile::fake()->image('test.jpg', 249, 1000);
$media = MediaLibrary::instance(); $this->media->put('/pages/neu/test.jpg', $file->get());
$media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(1, 'pages/neu'); $this->assertFileCount(1, 'pages/neu');
@ -157,8 +149,7 @@ class ResizerTest extends TestCase
Setting::set('breakpoints', ['100']); Setting::set('breakpoints', ['100']);
$file = UploadedFile::fake()->image('test.jpg', 500, 400); $file = UploadedFile::fake()->image('test.jpg', 500, 400);
$media = MediaLibrary::instance(); $this->media->put('/pages/neu/test.jpg', $file->get());
$media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(4, 'pages/neu'); $this->assertFileCount(4, 'pages/neu');
@ -174,8 +165,7 @@ class ResizerTest extends TestCase
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', ['100']); Setting::set('breakpoints', ['100']);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
$media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
Event::fire('media.file.upload', [null, '/pages/test.svg', null]); Event::fire('media.file.upload', [null, '/pages/test.svg', null]);
$this->assertFileCount(0, 'pages'); $this->assertFileCount(0, 'pages');
@ -187,8 +177,7 @@ class ResizerTest extends TestCase
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', ['100']); Setting::set('breakpoints', ['100']);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
$media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
Event::fire('media.file.upload', [null, '/pages/test.png', null]); Event::fire('media.file.upload', [null, '/pages/test.png', null]);
$this->assertHasFile('pages/test-100x100.png'); $this->assertHasFile('pages/test-100x100.png');
@ -202,8 +191,7 @@ class ResizerTest extends TestCase
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', ['100', '600']); Setting::set('breakpoints', ['100', '600']);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
$media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(2, 'pages'); $this->assertFileCount(2, 'pages');
@ -217,8 +205,7 @@ class ResizerTest extends TestCase
Setting::set('sizes', []); Setting::set('sizes', []);
Setting::set('breakpoints', ['100', '600']); Setting::set('breakpoints', ['100', '600']);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
$media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
Event::fire('media.file.upload', [null, '/pages/test.png', null]); Event::fire('media.file.upload', [null, '/pages/test.png', null]);
$this->assertHasFile('pages/test-100x200.png'); $this->assertHasFile('pages/test-100x200.png');
@ -231,8 +218,7 @@ class ResizerTest extends TestCase
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x1']]); Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x1']]);
Setting::set('breakpoints', [200]); Setting::set('breakpoints', [200]);
$media = MediaLibrary::instance(); $this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
$media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
Event::fire('media.file.upload', [null, '/pages/test.pdf', null]); Event::fire('media.file.upload', [null, '/pages/test.pdf', null]);
$this->assertHasFile('pages/test-1275x1650.pdf.jpg'); $this->assertHasFile('pages/test-1275x1650.pdf.jpg');