kisphp/strategy-files-uploader

0.1.0 2020-03-02 15:12 UTC

This package is auto-updated.

Last update: 2024-08-29 05:38:57 UTC


README

pipeline status coverage report

使用策略模式上传文件并生成缩略图

安装

composer require kisphp/strategy-files-uploader

使用方法

<?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 将返回源文件对象。这样,您将不会从之前的缩略图创建新的缩略图
  • CopyUploadedFileUniqIdNameOriginalName 将返回从复制文件中获取的 SourceFile 对象