fsasvari/laravel-uploadify

这是一个Laravel图像上传库,支持重命名、显示自定义路由的缩略图等功能。

v3.0.0 2024-07-25 15:07 UTC

This package is auto-updated.

Last update: 2024-08-25 15:21:02 UTC


README

Uploadify是一个Laravel 5.5+的库,可以自动重命名上传的图像,通过自定义路由显示缩略图等。所有这些都可以通过Eloquent模型实现。

Build For Laravel Latest Stable Version Latest Unstable Version Total Downloads License

安装

步骤1:安装包

要开始使用Laravel Uploadify,执行Composer命令将包添加到composer.json项目中

composer require fsasvari/laravel-uploadify

或者直接在composer.json中添加以下行

"fsasvari/laravel-uploadify": "2.*"

然后运行composer update

composer update

步骤2:服务提供者和外观

安装Laravel Uploadify库后,在config/app.php配置文件中注册Uploadify\Providers\UploadifyServiceProvider

'providers' => [
    // Application Service Providers...
    // ...

    // Other Service Providers...
    Uploadify\Providers\UploadifyServiceProvider::class,
    // ...
],

可选地,您可以为Uploadify外观添加别名

'aliases' => [
    'Uploadify' => Uploadify\Facades\UploadifyManager::class,
];

步骤3:配置

我们需要将配置文件复制到我们的项目中。

php artisan vendor:publish --tag=uploadify

步骤4:符号链接

如果您尚未在项目中创建符号链接,我们需要在publicstorage目录之间创建链接。我们可以使用内置的Laravel函数storage:link,它将在public/storagestorage/app/storage目录之间创建链接。

php artisan storage:link

或者使用Windows功能创建自定义存储链接

mklink /d "c:\path-to-project\project-name\public\custom-directory-name" "c:\path-to-project\project-name\storage\app\custom-directory-name" // custom-directory-name could be "storage", "upload"...

或者使用Unix功能创建自定义存储链接

ln -s /path-to-project/project-name/storage/app/custom-directory-name /path-to-project/project-name/public/custom-directory-name // custom-directory-name could be "storage", "upload"...

步骤5:模型

您需要将UploadifyTrait特质包含在您的Eloquent模型中。

文件

如果您需要在Eloquent模型中显示简单的文件(pdf、doc、zip...),您需要定义一个公共属性$uploadifyFiles,其中数据库字段名作为键,path作为必需的数组值。此外,disk值是可选的,它将从配置中的默认磁盘值获取。

<?php

namespace App;

use Uploadify\Traits\UploadifyTrait;

class Car extends Eloquent
{
    use UploadifyTrait;

    /**
     * List of uploadify files
     *
     * @var array
     */
    protected $uploadifyFiles = [
        'upload_information' => ['path' => 'documents/information'],
        'upload_specification' => ['path' => 'documents/specification', 'disk' => 's3'],
    ];
}

图像

如果您需要在Eloquent模型中显示图像(jpg、png、gif...),您需要定义一个公共属性$uploadifyImages,其中数据库字段名作为键,path作为必需的数组值。此外,disk值是可选的,它将从配置中的默认磁盘值获取。

<?php

namespace App;

use Uploadify\Traits\UploadifyTrait;

class User extends Eloquent
{
    use UploadifyTrait;

    /**
     * List of uploadify images
     *
     * @var array
     */
    public $uploadifyImages = [
        'upload_cover' => ['path' => 'images/cover'],
        'upload_avatar' => ['path' => 'images/avatar', 'disk' => 's3'],
    ];
}

文件和图像组合

您还可以将文件和图像组合到一个Eloquent模型中

<?php

namespace App;

use Uploadify\Traits\UploadifyTrait;

class Car extends Eloquent
{
    use UploadifyTrait;

    /**
     * List of uploadify files
     *
     * @var array
     */
    public $uploadifyFiles = [
        'upload_information' => ['path' => 'documents/information'],
        'upload_specification' => ['path' => 'documents/specification'],
    ];

    /**
     * List of uploadify images
     *
     * @var array
     */
    public $uploadifyImages = [
        'upload_cover' => ['path' => 'images/cover'],
    ];
}

步骤6:路由器

如果您想显示处理后的图像,您需要在routes/web.php文件中包含Uploadify控制器。

Route::get('{path}/{options}/{name}.{extension}', '\Uploadify\Http\Controllers\ImageController@show')
    ->where('path', '[a-z-/]+')
    ->where('options', '[a-z0-9-_,]+')
    ->where('name', '.+?')
    ->where('extension', 'jpe?g|gif|png|JPE?G|GIF|PNG');

用法

文件

// To use this package, first we need an instance of our model
$car = Car::first();

// get full file name with extension
$cat->upload_specification; // car-specification.pdf
$cat->upload_specification->name(); // car-specification.pdf

// get file basename
$cat->upload_specification->basename(); // car-specification

// get file extension
$cat->upload_specification->extension(); // pdf

// get file size in bytes
$cat->upload_specification->filesize(); // 1500000

// get full url path to file
$car->upload_specification->url(); // storage/documents/specification/car-specification.pdf or
                                   // http://www.website.com/storage/documents/specification/car-specification.pdf
                                   // if "url" value provided in disk url in "config/filesystems.php"

图像

// To use this package, first we need an instance of our model
$user = User::first();

// get full image name with extension
$cat->upload_avatar; // user-avatar.jpg
$cat->upload_avatar->name(); // user-avatar.jpg

// get image basename
$cat->upload_avatar->basename(); // user-avatar

// get file extension
$cat->upload_avatar->extension(); // jpg

// get image size in bytes
$cat->upload_avatar->filesize(); // 150000

// get full url path to image
// example: http://www.website.com/storage/images/avatar/user-avatar.jpg
$car->upload_avatar->url();

// get full url path to image thumb
// example: http://www.website.com/storage/images/avatar/w_200,h_200/user-avatar.jpg
$car->upload_avatar->url(200, 200);

// get full url path to image thumb with some special effects and options
// example: http://www.website.com/storage/images/avatar/w_200,h_200,opacity_50/user-avatar.jpg
$car->upload_avatar->url(200, 200, ['opacity' => 50]);

// get full url path to image with custom options
// example: http://www.website.com/storage/images/avatar/w_500,blur_50,brightness_-50,effect_grayscale,crop_resize/user-avatar.jpg
$car->upload_avatar->url(['width' => 500, 'blur' => 50, 'brightness' => -50, 'effect' => 'grayscale', 'crop' => 'resize']);

选项列表

您可以在调用url()方法时使用的自定义选项列表

  • 宽度
  • 高度
  • 裁剪:fit(默认),resize
  • 效果:greyscaleinvert
  • 质量:0 - 10090是默认值)
  • 模糊:0 - 100
  • 亮度:-100 - 100
  • 对比度:-100 - 100
  • 锐化:0 - 100
  • 像素化:(像素大小)
  • 旋转:-360 - 360
  • 翻转:h(水平),v(垂直)

使用UploadedFile上传

使用Laravel UploadedFile类和请求实例接收到的上传示例。

// create new eloquent model object
$car = new Car();

// get UploadedFile from request Illuminate\Http\Request
$file = $request->file('specification');

// create new uploadify instance, set file, model and field name
$uploadify = UploadifyManager::create($file, $car, 'upload_specification'); // or set($file, new Car(), 'upload_specification')

// additional options
$uploadify->setName('custom file name'); // set custom file name

// upload file
$uploadify->upload();

// get uploaded file name with extension (without path), so you can save value in database
$car->upload_specification = $uploadify->getName();
$car->save();

从路径或URL上传

使用从pathurl接收到的文件的上传示例。

// path to file
$path = '/path-to-file/file.pdf';

// create new uploadify instance, set path, model and field name
$uploadify = UploadifyManager::create($path, $car, 'upload_specification'); // or set($file, new Car(), 'upload_specification')

使用InterventionImage上传

使用用户创建的Intervention Image类上传示例。首先,创建包含所有想要的图像处理(调整大小、裁剪、旋转、灰度等)的Image实例,然后将该图像实例注入到UploadifyManager中。

// create new eloquent model object
$user = new User;

$file = $request->file('avatar');

// create new uploadify instance, set file, model and field name
$uploadify = UploadifyManager::create($file, $user, 'upload_avatar'); // or set($image, new User, 'upload_avatar');

// if you want additional image manipulation from Intervention Image package
$image = Image::make($file)->resize(800, null, function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
});

$uploadify->process($image);

// additional options
$uploadify->setName('custom image name'); // set custom file name
$uploadify->setQuality(80); // set image quality, default value is 90

// upload file
$uploadify->upload();

// get uploaded file name with extension (without path), so you can save value in database
$user->upload_avatar = $uploadify->getName();
$user->save();

删除

delete()方法从文件系统中删除文件

$car = Car::first();

// deletes file
$car->upload_specification->delete();

// you need to manually set field value to "null" after deletion
$car->upload_specification = null;
$car->save();

示例用法

控制器

<?php

namespace App\Http\Controllers

use App\Car;

class CarController
{
    public function index()
    {
        $cars = Car::get();

        $data = [
            'cars' => $cars,
        ];

        return view('index', $data);
    }
}

视图

<div class='row'>
    @foreach ($cars as $car)
        <div class='col-12 col-sm-6 col-md-4'>
            @if ($car->upload_cover)
                <p>
                    <img src='{{ $car->upload_cover->url(400, 300) }}'
                         alt='{{ $car->name }}' title='{{ $car->name }}'
                         width='400' height='300'
                         class='img-thumbnail img-fluid'>
                </p>
            @endif
            <h2><a href='{{ $car->url }}'>{{ $car->name }}</a></h2>
            <p>{{ str_limit($car->description, 200) }}</p>
            @if ($car->upload_specification)
                <p>
                    <a href='{{ $car->upload_specification->url() }}'>
                        <i class='fa fa-archive'></i>
                        {{ $car->upload_specification->name() }}
                    </a>
                    <br>
                    <span class='text-muted'>{{ $car->upload_specification->filesize() }} bytes</span>
                </p>
            @endif
        </div>
    @endforeach
</div>

许可证

MIT许可证。有关更多信息,请参阅LICENSE文件。

作者

Frano Šašvari

邮箱: sasvari.frano@gmail.com