i-ismail/laravel-file-upload
Composer 安装命令:
composer require i-ismail/laravel-file-upload
包简介
Laravel file upload is a package that allows to you upload a single file or multiple files with auto-optimization and resizing
README 文档
README
Description
Laravel file upload is a package that allows to you upload a single file or multiple files with auto-optimization, resizing, uses a smart combination of the best optimization and lossy compression algorithms to shrink images to the minimum possible size while keeping the required level of quality.
Installation
composer require i-ismail/laravel-file-upload
Publish Config File
php artisan vendor:publish --tag=file-upload-config
Packages dependency
1- FileUploadService class
This class store file and return file name to store it in your model.
- it provide all methods in intervention image package.
- it provide auto-optimization and resizing.
Here are a few short examples of what you can do:
$post = new Post(); //... $post->image = FileUpload::make(request('image'))->store(); $post->save();
You can add folder path.
$post = new Post(); //... $post->image = FileUpload::make(request('image')) ->store('posts'); $post->save();
You can add disk, by default disk is public.
$post = new Post(); //... $post->image = FileUpload::make(request('image')) ->disk('s3') ->store('posts'); $post->save();
You can add custom file name.
$post = new Post(); //... $post->image = FileUpload::make(request('image')) ->fileName('my-image') ->store('posts'); $post->save();
You can add custom extension, By default, we will set this value to "webp" for images.
$post = new Post(); //... $post->image = FileUpload::make(request('image')) ->extension('jpg') ->store('posts'); $post->save();
You can use all methods in intervention image package.
$post = new Post(); //... $post->image = FileUpload::make(request('image')) ->resize(400, 400) ->crop(100, 100, 25, 25) ->store('posts'); $post->save();
You can delete file
$post = Post::find(1); //... $post->image = FileUpload::delete($post->image);
You can use delete old file, for example in update.
$post = Post::find(1); //... $post->image = FileUpload::make(request('image')) ->delete($post->image) ->store('posts'); $post->save();
You can get path.
$post = new Post(); //... $fileUpload = FileUpload::make(request('image')); $post->image = $fileUpload->store('posts'); $filePath = $fileUpload->getFilePath(); $post->save();
Note: you can clean code by Accessors & Mutators
$post = Post::create([ //... ]); //In Post Model public function setImageAttribute($image) { $this->attributes['image'] = FileUpload::make($image)->store('posts'); }
To get image.
//In Post Model public function getImgAttribute() { return $this->image ? asset('storage/'. $this->image) : asset('images/post.jpg'); }
Dont forget run this command.
php artisan storage:link
2- MediaUploadService class
This class use media-library package to store media files for your model.
- it provide all methods in intervention image package.
- it provide all methods in media-library package.
- it provide auto-optimization and resizing.
Preparing the database
You need to publish the migration to create the media table:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
After that, you need to run migrations.
php artisan migrate
Publishing the config file
Publishing the config file is optional:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
Preparing your model
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; class Post extends Model implements HasMedia { use InteractsWithMedia; ... }
Here are a few short examples of what you can do:
$post = new Post(); //... $post->save(); MediaUpload::make(request('images'))->setModel($post)->store();
You can add collection.
$post = new Post(); //... $post->save(); MediaUpload::make(request('images')) ->setModel($post) ->store('posts');
You can add disk, by default disk is public.
$post = new Post(); //... $post->save(); MediaUpload::make(request('images')) ->setModel($post) ->disk('s3') ->store('posts');
You can use all methods in intervention image package before setModel method
$post = new Post(); //... $post->save(); MediaUpload::make(request('images')) ->resize(500, 200) ->crop(100, 100, 25, 25) ->setModel($post) ->store('posts');
You can use all methods in media library package after setModel method.
$post = new Post(); //... $post->save(); MediaUpload::make(request('images')) ->resize(500, 200) ->setModel($post) ->usingName('my-image-name') ->withCustomProperties([ 'primaryColor' => 'red', 'image-code' => '12458558', ]) ->store('posts');
Retrieving media
$post->getMedia('posts'); // posts is collection name.
This method returns a collection of Media-objects.
retrieving the first media and the URL for the first media.
$post->getFirstMedia('posts'); $post->getFirstMediaUrl('posts');
Delete media
You can remove something from the library by simply calling delete on an instance of Media:
$mediaItem->delete();
Delete Model
$post->delete(); // all associated files will be deleted as well. $post->deletePreservingMedia(); // all associated files will be preserved.
recommend: read Laravel-media library documentation.
i-ismail/laravel-file-upload 适用场景与选型建议
i-ismail/laravel-file-upload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「upload」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 i-ismail/laravel-file-upload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 i-ismail/laravel-file-upload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 i-ismail/laravel-file-upload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
Mapper for accessing resources in KWCMS
Text file manipulation
KCFinder web file manager
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-12
