ankitpokhrel / laravel-image
简单的图像上传和缩略图管理
Requires
- php: >=5.6.4
- laravel/framework: 5.3.*
- league/glide-laravel: ^1.0
- phpunit/phpunit: 5.5.*
Requires (Dev)
- mockery/mockery: dev-master
- orchestra/testbench: ~3.3
- squizlabs/php_codesniffer: 2.*
This package is auto-updated.
Last update: 2021-01-06 22:08:28 UTC
README
Laravel 5+ 图像上传包
概述
Laravel Image 是一个用于 Laravel 5+ 的图像上传和缩略图管理包。此包使用来自 php league 的 Glide 库进行按需图像处理。
安装
使用 composer 拉取包
composer require ankitpokhrel/laravel-image
在 config/app.php
中包含服务提供者。
AnkitPokhrel\LaravelImage\ImageUploadServiceProvider::class
最后发布配置
php artisan vendor:publish --provider="AnkitPokhrel\LaravelImage\ImageUploadServiceProvider"
配置
您可以将默认配置添加到 config/laravelimage.php
。
用法
上传图像
在您的控制器中,通过依赖注入获取图像上传服务
class ContentsController extends Controller { ... protected $image; /** * @param ImageUploadService $image */ public function __construct(ImageUploadService $image) { ... //set properties for file upload $this->image = $image; $this->image->setUploadField('image'); //default is image $this->image->setUploadFolder('contents'); //default is public/uploads/contents } ...
然后,在您的存储或更新方法中执行图像上传
/** * Store method */ public function store(ContentRequest $request) { $input = $request->all(); if (Input::hasFile($this->image->getUploadField()) && $this->image->upload()) { //image is uploaded, get uploaded image info $uploadedFileInfo = $this->image->getUploadedFileInfo(); ... //do whatever you like with the information $input = array_merge($input, $uploadedFileInfo); } else { //get validator object $validator = $this->image->getValidationErrors(); //get error messages $errors = $validator->messages()->all(); } ... } /** * Update method */ public function update(Request $request, $id) { ... if (Input::hasFile($this->image->getUploadField()) && $this->image->upload()) { ... //remove old files if (!empty(trim($model->file_path))) { $this->image->clean(public_path($model->file_path), true); } } ... }
上传文件信息
您可以使用 $this->image->getUploadedFileInfo()
获取上传文件信息。它将返回如下数组
image : Saved image name upload_dir : Image upload path original_image_name : Real name of uploaded image size : Size of the uploaded image extension : Extension of the uploaded image mime_type : Mime type of the uploaded image
要更改 image
字段,您可以使用 setUploadField()
方法。如果您希望更改 upload_dir
字段,您可以使用 setUploadDir
方法。通常您会将 image
、upload_dir
和 original_image_name
保存到数据库中。
要获取验证错误,您可以使用 getValidationErrors()
方法。
自定义上传路径
有时您可能想要将上传的图像分组,甚至将图像存储在除了公共文件夹之外的其它地方。您可以通过设置基本路径来实现。例如,以下设置将图像存储在 public/uploads/user-images/users/
目录中。
//set base path $this->image->setBasePath('uploads/user-images/'); //set upload folder $this->image->setUploadFolder('users');
如果您想将图像上传到除了 public
文件夹之外的其它地方,您可以将第二个参数作为 false
提供给基本路径方法。
//set absolute base path $this->image->setBasePath('/absolute/path/to/your/folder/uploads/', true); //set upload folder $this->image->setUploadFolder('users');
这将把图像上传到 /absolute/path/to/your/folder/uploads/users
文件夹。请确保该文件夹有适当的权限来存储图像。
使用 blade 指令显示图像
显示完整图像
@laravelImage($uploadDir, $imageName)
在运行时创建自定义尺寸的图像
<-- @laravelImage(uploadDir, imageName, width, height) --> @laravelImage($uploadDir, $imageName, 300, 200)
选项 & 属性
<-- @laravelImage(uploadDir, imageName, width, height, options, attributes) --> @laravelImage($uploadDir, $imageName, 300, 200, [ 'fit' => 'crop-top-left', 'filt' => 'sepia' ], [ 'alt' => 'Alt text of an image', 'class' => 'custom-class' ])
选项可以是任何 glide 选项。有关选项的更多信息,请参阅 thephpleague/glide。
不使用 blade 显示图像
图像源应采用格式 laravelimage.routePath/uploadDir/image?options
,其中 laravelimage.routePath
来自配置文件。因此,如果您将 routePath
设置为 cache
,则图像URL将类似于以下内容。
<img src="/cache/{{ $uploadDir . $image }}?w=128&fit=crop-center" alt="" />
生成的图像存储在 storage/laravel-image-cache
中。