regulus / upstream
一个用于 Laravel 5 的简单 Composer 包,帮助进行文件上传和图片缩放/裁剪。
v0.6.10
2018-06-25 17:24 UTC
Requires
- php: >=5.4.0
- intervention/image: 2.4.*
- laravel/framework: 5.*
README
一个用于 Laravel 5 的简单 Composer 包,帮助进行文件上传和图片缩放/裁剪。
安装
要安装 Upstream,请确保在 Laravel 5 的 composer.json
文件中添加了 regulus/upstream
。
"require": {
"regulus/upstream": "0.6.*"
},
然后从命令行运行 php composer.phar update
。Composer 将安装 Upstream 包。现在,您只需要在 config/app.php
中注册服务提供者并设置 Upstream 的别名。将以下内容添加到 providers
数组
Regulus\Upstream\UpstreamServiceProvider::class,
并将以下内容添加到 aliases
数组
'Upstream' => Regulus\Upstream\Facade::class,
最后,运行 php artisan vendor:publish
以发布配置文件和语言文件。
上传文件
$config = [
'path' => 'uploads/pdfs', // the path to upload to
'field' => 'file', // name of field (use "fields" for an array with multiple fields)
'filename' => 'temp', // the basename of the file (extension will be added automatically)
'fileTypes' => ['png', 'jpg'], // the file types to allow
'createDirectory' => true, // automatically creates directory if it doesn't exist
'overwrite' => true, // whether or not to overwrite existing file of the same name
'maxFileSize' => '5MB', // the maximum filesize of file to be uploaded
];
$upstream = Upstream::make($config);
$result = $upstream->upload();
注意:特别提供了以下
filename
字符串:[LOWERCASE]
、[UNDERSCORED]
、[LOWERCASE-UNDERSCORED]
、[DASHED]
、[LOWERCASE-DASHED]
和[RANDOM]
。前五个用于对原始文件名进行格式调整。后者可以用来将文件名设置为随机字符串(连同其原始扩展名)。
缩放图片和创建缩略图
$config = [
'path' => 'uploads/images',
'fields' => ['picture', 'picture2'],
'filename' => 'temp',
'fileTypes' => 'images',
'createDirectory' => true,
'overwrite' => true,
'maxFileSize' => '5MB',
'imageResize' => true,
'imageResizeQuality' => 60,
'imageCrop' => true,
'imageDimensions' => [
'w' => 720, // image width
'h' => 360, // image height
'tw' => 180, // thumbnail image width
'th' => 180, // thumbnail image height
],
];
$upstream = Upstream::make($config);
$result = $upstream->upload();
在 src/config/config.php
配置文件中提供了一个完整的配置数组。您可以使用以下代码在上传图片后裁剪图片
$config = [
'path' => 'uploads/images',
'filename' => 'temp-'.$id.'.jpg',
'newFilename' => $id.'.jpg',
'createDirectory' => true,
'overwrite' => true,
'imageThumb' => true,
'imageDimensions' => [
'w' => 260, // image width
'h' => 260, // image height
'tw' => 120, // thumbnail image width
'th' => 120, // thumbnail image height
],
'cropPosition' => [
'x' => $input['x'], // X position
'y' => $input['y'], // Y position
'w' => $input['width'], // width of cropped area
'h' => $input['height'], // height of cropped area
],
];
$result = Upstream::cropImage($config);
您可以使用 JavaScript 图片裁剪器(如 Cropper)来设置 cropPosition
。如果将 cropPosition
设置为 null 或未设置,则图片将首先缩放到目标尺寸的 140%,然后从中心裁剪。