todsto / image-crop-resizer
Laravel 4 的图像处理包
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
- orchestra/imagine: *
This package is not auto-updated.
Last update: 2024-09-18 04:25:18 UTC
README
您可以通过标准方式安装此包。将
"todsto/image-crop-resizer": "dev-master"
添加到您的 composer.json
中。运行 composer update
发布配置
php artisan config:publish todsto/image-crop-resizer
打开您的 app/config/app.php
并将 'Todsto\ImageCropResizer\ImageCropResizerServiceProvider',
添加到 providers 数组中。然后添加 'ICR' => 'Todsto\ImageCropResizer\ImageCropResizer'
到 aliases 数组中。这将注册 'ICR' 作为基础包类的别名。
基本用法
该包将图像存储在 public/uploads/context/size
文件夹中。
要设置自己的上下文,请使用 app/config/packages/todsto/image-crop-resizer/contexts.php
。该文件如下所示
return [
'default' => [
'small' => [
'width' => 100,
'height' => 100,
'action' => 'crop-resize'
],
'medium' => [
'width' => 200,
'height' => 200,
'action' => 'crop-resize'
],
'large' => [
'width' => 300,
'height' => 300,
'action' => 'crop-resize'
]
]
];
"default" 是默认图像上下文。上下文有不同的尺寸,具有宽度、高度和动作。动作定义了图像将被处理的方式。"crop" 从图像中间裁剪提供尺寸的区域。"resize" 将图像缩放到给定的尺寸。"crop-resize" 从图像中裁剪一个区域,但首先将其缩放,因此图像将以无变形和最小损失的方式裁剪。
要使用此包,只需在控制器中调用 ImageCropResizer 类的 process 方法。由于您有一个别名,您的代码可能看起来像这样
class TestController extends Controller {
public function getTest() {
return View::make('test');
}
public function postTest() {
$image_name = ICR::process(Input::file('image'), 'default');
// Some database actions here
return Redirect::back()->with('message', 'Success');
}
}
在模板中
@if(Session::has('message'))
{{ Session::get('message') }}
@endif
{{ Form::open(['url' => 'test/test', 'method' => 'POST', 'enctype' => 'multipart/form-data']) }}
{{ Form::token() }}
{{ Form::file('image') }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
process 方法将输入字段的文件对象作为第一个参数。第二个参数是上下文。process 方法返回生成的唯一图像名称。