定制 kingofcode/laravel-uploadable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kingofcode/laravel-uploadable

Composer 安装命令:

composer require kingofcode/laravel-uploadable

包简介

Laravel Uploadable trait to automatically upload images and files with minimum configuration

README 文档

README

Laravel Uploadable trait to automatically upload images and files with minimum configuration.

Introduction

This package can help you to easily make uploads, using only one field in the database, and some simple attributes in your model class. With the default configuration, the package makes thumb, medium and normal image uploads. It can be used for other files upload too, like PDF, etc.

Installation

composer require kingofcode/laravel-uploadable

This package includes Intervention Image to resize and manage the images.

For Laravel version less than 5.8.10, please use 0.1.7 version, as 0.1.8 added model replication that is supported only for Laravel > 5.8.10

About Upload

This package uses the Laravel File Storage to keep the file management. The files will be stored inside the default disk. For example, if you are using the public disk, to access the images or files, you need to create a symbolic link inside your project:

php artisan storage:link

And then, configure your default filesystem, inside config/filesystems.php to point the public disk:

'default' => env('FILESYSTEM_DRIVER', 'public'),

Usage

To use this package, import the Uploadable trait in your model:

use KingOfCode\Upload\Uploadable;

And then, configure your uploadable fields for images and files inside your model.

class Product extends Model
{
    use Uploadable;

    protected $fillable = [
        'name',
        'type',
        // Avoid adding file fields in the fillable array, it can break the correct upload
    ];
    
    // Array of uploadable images. These fields need to be existent in your database table
    protected $uploadableImages = [
        'image',
        'perfil_image',
        'test_image'
    ];
    
    // Array of uploadable files. These fields need to be existent in your database table
    protected $uploadableFiles = [
        'pdf'
    ];

}

That's all! After this configuration, you can send file data from the client side with the same name of each file field of the model. The package will make the magic!

Change Image Upload Size

The default sizes (width) for images are:

  • thumb - 100px
  • medium - 300px
  • normal - Image width

You can modify these values directly in the $uploadableImages array. The accepted values are: image_width or a number specifying the quantity of pixels.

protected $uploadableImages = [
  'image' => ['thumb' => 150, 'medium' => 500, 'normal' => 700],
  'perfil_image' => ['thumb' => 120, 'medium' => 'image_width', normal => 2000],
  'test_image'
];

Change Image Upload Types

By default, this package saves thumb, medium and normal images. To disable some upload type, add this array to your model:

protected $imageResizeTypes = [
  'medium' => false,
  // ... You can disable medium and normal images upload too
];

Setting the upload folder name

The upload will be made inside a specific folder with the name of the model, but in the plural mode. Eg: for the Product model, the images will be uploaded to the 'images/products' directory, and the other files, inside 'files/products'. If you want to modify this directory, add this code to your model:

public $uploadFolderName = 'products'; // Name of your folder

Getting the File Path inside Views, etc

To get an image file path, you can call getImagePath($imageField, $type) from your object. Eg:

$product = Product::find(1);

$normal_image = $product->getImagePath('image');
$thumb_image = $product->getImagePath('image', 'thumb');
$medium_perfil_image = $product->getImagePath('perfil_image', 'medium');

And to get a simple file path, call getFilePath($fileField):

$product = Product::find(1);

$pdf = $product->getFilePath('pdf');

Inspiration

The basic structure of this package is inspired in the Laravel Auto Upload package

Contributing

Feel free to comment, open issues and send PR's. Enjoy it!!

License

MIT

kingofcode/laravel-uploadable 适用场景与选型建议

kingofcode/laravel-uploadable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.49k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2017 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「upload」 「laravel」 「uploadable」 「image-upload」 「file-upload」 「auto-upload」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kingofcode/laravel-uploadable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kingofcode/laravel-uploadable 我们能提供哪些服务?
定制开发 / 二次开发

基于 kingofcode/laravel-uploadable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 3
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-29