kisphp / strategy-files-uploader
0.1.0
2020-03-02 15:12 UTC
Requires
- php: >=7.3
- ext-fileinfo: *
- kisphp/thumbnails: ^1.3
- kisphp/tools: ^2.1
- symfony/http-foundation: ^3.4|^4.4|^5.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ~5.0
This package is auto-updated.
Last update: 2024-11-05 09:22:13 UTC
README
Upload files and generate thumbnails using Strategy pattern
Installation
composer require kisphp/strategy-files-uploader
Usage
<?php
use Kisphp\FileManager\Strategy\CopyUploadedFile;
use Kisphp\FileManager\Strategy\GenerateThumbnail;
use Kisphp\FileManager\StrategyManager;
use Kisphp\FileManager\UploadedSourceFile;
use Symfony\Component\HttpFoundation\Request;
require_once __DIR__ . '/path/to/vendor/autoload.php';
$req = Request::createFromGlobals();
$sourceFilePath = __DIR__ . '/path/to/uploads/';
$targetFilePath = __DIR__ . '/path/to/public/thumbs/';
$sourceImage = new UploadedSourceFile($req->files->get('file'));
$sm = new StrategyManager();
$sm
->chain(new CopyUploadedFile($sourceFilePath))
->chain(new GenerateThumbnail($targetFilePath, 300, 250))
->chain(new GenerateThumbnail($targetFilePath, 64, 64))
->chain(new GenerateThumbnail($targetFilePath, 800, 0))
;
$sm->executeChain($sourceImage);
GenerateThumbnail
will return the source file object. This way, you will not make a new thumbnail from the previous thumbnailCopyUploadedFile
,UniqIdName
andOriginalName
will return aSourceFile
object form the copied file