Create PDF image files
This commit is contained in:
parent
5cb79c75a6
commit
2c3400715c
|
@ -14,6 +14,8 @@ abstract class Compressor
|
|||
|
||||
abstract protected function getExtension();
|
||||
|
||||
abstract public function getExtensionRegex(): string;
|
||||
|
||||
public function __construct(MediaPath $media)
|
||||
{
|
||||
$this->media = $media;
|
||||
|
|
|
@ -9,6 +9,11 @@ class DefaultCompressor extends Compressor {
|
|||
return '';
|
||||
}
|
||||
|
||||
public function getExtensionRegex(): string
|
||||
{
|
||||
return '[a-zA-Z0-9]*';
|
||||
}
|
||||
|
||||
public function make(string $path): array
|
||||
{
|
||||
return [];
|
||||
|
|
|
@ -15,6 +15,12 @@ class Factory
|
|||
'application/pdf' => PdfCompressor::class,
|
||||
];
|
||||
|
||||
public array $extensions = [
|
||||
'jpg' => JpgCompressor::class,
|
||||
'png' => PngCompressor::class,
|
||||
'pdf' => PdfCompressor::class,
|
||||
];
|
||||
|
||||
public function fromMedia(string $mediaPath): Compressor
|
||||
{
|
||||
return $this->resolve(new MediaPath($mediaPath));
|
||||
|
@ -22,7 +28,9 @@ class Factory
|
|||
|
||||
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)) {
|
||||
return new $this->default($path);
|
||||
|
@ -36,4 +44,9 @@ class Factory
|
|||
return collect($this->types)->get($type);
|
||||
}
|
||||
|
||||
private function resolveExtension(string $extension): ?string
|
||||
{
|
||||
return collect($this->extensions)->get($extension);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,11 @@ use Storage;
|
|||
|
||||
class JpgCompressor extends Compressor {
|
||||
|
||||
public function getExtensionRegex(): string
|
||||
{
|
||||
return 'jpg';
|
||||
}
|
||||
|
||||
protected function getExtension(): string
|
||||
{
|
||||
return 'jpg';
|
||||
|
|
|
@ -6,7 +6,13 @@ use Illuminate\Support\Collection;
|
|||
use Intervention\Image\ImageManager;
|
||||
use Storage;
|
||||
|
||||
class PdfCompressor extends Compressor {
|
||||
class PdfCompressor extends Compressor
|
||||
{
|
||||
|
||||
public function getExtensionRegex(): string
|
||||
{
|
||||
return 'pdf\.jpg';
|
||||
}
|
||||
|
||||
protected function getExtension(): string
|
||||
{
|
||||
|
|
|
@ -13,6 +13,11 @@ class PngCompressor extends Compressor {
|
|||
return 'png';
|
||||
}
|
||||
|
||||
public function getExtensionRegex(): string
|
||||
{
|
||||
return 'png';
|
||||
}
|
||||
|
||||
public function make(string $path): array
|
||||
{
|
||||
$output = "/tmp/".str_slug(microtime());
|
||||
|
|
|
@ -44,8 +44,12 @@ class MediaPath
|
|||
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());
|
||||
}
|
||||
|
||||
|
@ -86,7 +90,7 @@ class MediaPath
|
|||
$return = collect([]);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -73,4 +73,16 @@ class DeleteTest extends TestCase
|
|||
$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');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,14 +24,12 @@ class MoveTest extends TestCase
|
|||
Setting::set('folders', ['pages']);
|
||||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', []);
|
||||
$this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image(100, 100)->get());
|
||||
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages/alt', 'test.jpg', 'local');
|
||||
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages/alt', 'test-200x300.jpg', 'local');
|
||||
$this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
|
||||
UploadedFile::fake()->image('test.jpg', 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']);
|
||||
|
||||
$this->assertFileCount(0, 'pages/alt');
|
||||
$this->assertHasFile('pages/neu/test.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'));
|
||||
}
|
||||
|
@ -41,14 +39,12 @@ class MoveTest extends TestCase
|
|||
Setting::set('folders', ['pages']);
|
||||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', []);
|
||||
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image(100, 100)->get());
|
||||
UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local');
|
||||
$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-200x300.jpg', 'local');
|
||||
|
||||
Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);
|
||||
|
||||
$this->assertFileCount(2, 'pages');
|
||||
$this->assertHasFile('pages/testneu.jpg');
|
||||
$this->assertFileCount(1, 'pages');
|
||||
$this->assertHasFile('pages/testneu-200x300.jpg');
|
||||
}
|
||||
|
||||
|
@ -58,15 +54,14 @@ class MoveTest extends TestCase
|
|||
Setting::set('folders', ['pages']);
|
||||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', []);
|
||||
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image(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', 'testother.jpg', 'local');
|
||||
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());
|
||||
UploadedFile::fake()->image('test.jpg', 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');
|
||||
|
||||
Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']);
|
||||
|
||||
$this->assertFileCount(3, 'pages');
|
||||
$this->assertHasFile('pages/testneu.jpg');
|
||||
$this->assertFileCount(2, 'pages');
|
||||
$this->assertHasFile('pages/testother-200x300.jpg');
|
||||
$this->assertHasFile('pages/testneu-200x300.jpg');
|
||||
$this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg'));
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ class ResizerTest extends TestCase
|
|||
{
|
||||
parent::setUp();
|
||||
Storage::fake('local');
|
||||
$this->media = MediaLibrary::instance();
|
||||
}
|
||||
|
||||
public function testDontCopyOriginalFileWhenSettingsAreNotSet(): void
|
||||
|
@ -25,8 +26,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', []);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.jpg', $file);
|
||||
$this->media->put('/pages/test.jpg', $file);
|
||||
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(0, '');
|
||||
|
@ -39,8 +39,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', []);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.jpg', $file->get());
|
||||
$this->media->put('/pages/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(1, 'pages');
|
||||
|
@ -55,8 +54,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', []);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.jpg', $file->get());
|
||||
$this->media->put('/pages/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, 'pages/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(1, 'pages');
|
||||
|
@ -70,8 +68,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', []);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages-neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages-neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages-neu/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(0, 'pages');
|
||||
|
@ -85,8 +82,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', []);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages/neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(1, 'pages/neu');
|
||||
|
@ -99,8 +95,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', ['250']);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages/neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(2, 'pages/neu');
|
||||
|
@ -115,8 +110,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', ['250']);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 100);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages/neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
||||
|
||||
$this->assertHasFile('pages/neu/test-250x50.jpg');
|
||||
|
@ -129,8 +123,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', ['250']);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 250, 1000);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages/neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(1, 'pages/neu');
|
||||
|
@ -143,8 +136,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', ['250']);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 249, 1000);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages/neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(1, 'pages/neu');
|
||||
|
@ -157,8 +149,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', ['100']);
|
||||
|
||||
$file = UploadedFile::fake()->image('test.jpg', 500, 400);
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/neu/test.jpg', $file->get());
|
||||
$this->media->put('/pages/neu/test.jpg', $file->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(4, 'pages/neu');
|
||||
|
@ -174,8 +165,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', ['100']);
|
||||
|
||||
$media = MediaLibrary::instance();
|
||||
$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'));
|
||||
Event::fire('media.file.upload', [null, '/pages/test.svg', null]);
|
||||
|
||||
$this->assertFileCount(0, 'pages');
|
||||
|
@ -187,8 +177,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', ['100']);
|
||||
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
|
||||
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
|
||||
|
||||
$this->assertHasFile('pages/test-100x100.png');
|
||||
|
@ -202,8 +191,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', ['100', '600']);
|
||||
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
|
||||
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
||||
|
||||
$this->assertFileCount(2, 'pages');
|
||||
|
@ -217,8 +205,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('sizes', []);
|
||||
Setting::set('breakpoints', ['100', '600']);
|
||||
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
|
||||
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
|
||||
|
||||
$this->assertHasFile('pages/test-100x200.png');
|
||||
|
@ -231,8 +218,7 @@ class ResizerTest extends TestCase
|
|||
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x1']]);
|
||||
Setting::set('breakpoints', [200]);
|
||||
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
|
||||
$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');
|
||||
|
|
Loading…
Reference in New Issue