我已经在Laravel5.1中安装了干预,我正在使用图像上传并调整大小,如下所示:
Route::post('/upload', function()
{
Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');
});我不明白的是,干预是如何对上传的图像进行验证的?我的意思是,干预中是否已经有内置图像验证检查,还是需要使用Laravel Validation手动添加一些内容来检查文件格式、文件大小等?我已经阅读了干预文档,我找不到图像验证如何工作时,使用干预与拉里。
谁能给我指明正确的方向吗..。
发布于 2015-08-09 13:31:02
感谢@maytham,他的评论为我指明了正确的方向。
我发现图像干预本身并没有进行任何验证。所有的图像验证都必须在其传递到图像干预上传之前完成。多亏了Laravel的内置验证器(如image和mime类型),这使得图像验证变得非常容易。这就是我现在要先验证文件输入的地方,然后再将它传递给图像干预。
处理干预前的Image 类:
Route::post('/upload', function()
{
$postData = $request->only('file');
$file = $postData['file'];
// Build the input for validation
$fileArray = array('image' => $file);
// Tell the validator that this file should be an image
$rules = array(
'image' => 'mimes:jpeg,jpg,png,gif|required|max:10000' // max 10000kb
);
// Now pass the input and rules into the validator
$validator = Validator::make($fileArray, $rules);
// Check to see if validation fails or passes
if ($validator->fails())
{
// Redirect or return json to frontend with a helpful message to inform the user
// that the provided file was not an adequate type
return response()->json(['error' => $validator->errors()->getMessages()], 400);
} else
{
// Store the File Now
// read image from temporary file
Image::make($file)->resize(300, 200)->save('foo.jpg');
};
});希望这能有所帮助。
发布于 2017-12-28 04:35:11
简单地说,将其集成以获得验证。
$this->validate($request, ['file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);发布于 2019-02-12 07:00:53
图像支持格式 已启动/格式
可读图像格式取决于选择的驱动程序(GD或Imagick)和本地配置。默认情况下,干预图像当前支持以下主要格式。
JPEG PNG GIF TIF BMP ICO PSD WebPGD✔️----✔️*
Imagick✔️✔️*
请参阅make方法的文档,以了解如何从不同来源读取图像格式,分别编码和保存以了解如何输出图像。
注意:(干预映像是一个开源的PHP映像处理和操作库 http://image.intervention.io/)。此库不验证任何验证规则,它是由幼体Validator类完成的。
Laravel Doc https://laravel.com/docs/5.7/validation
提示1: (请求验证)
$request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
'publish_at' => 'nullable|date',
]);
// Retrieve the validated input data...
$validated = $request->validated(); //laravel 5.7技巧2: (控制器验证)
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}https://stackoverflow.com/questions/31893439
复制相似问题