我在Laravel 5.1中安装了intervention,我正在使用图片上传并调整大小如下:
<?php
if(isset($_GET['query'])) {
$data_id= $_GET['query'];
if($data_id== 1) { //here I want to change the visibility of a html element
echo "
<script>
document.getElementById('box1').style.display = 'none';
</script>
";
}
else { //here I want to redirect user to add_data.php
header("Location:add_data.php");
}
}
?>
我不理解的是,干预如何处理上传图像的验证?我的意思是,干预是否已经在其中进行了内置图像验证检查,或者是我需要使用Laravel Route::post('/upload', function()
{
Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');
});
手动添加来检查文件格式,文件大小等的内容。我已阅读干预文档,但在使用laravel干预时,我无法找到有关图像验证如何工作的信息。
请有人指出我正确的方向..
答案 0 :(得分:16)
感谢@maytham的评论,他指出了我正确的方向。
我发现图像干预本身不进行任何验证。所有图像验证必须在传递到图像干预以进行上载之前完成。感谢Laravel的内置验证器,如image
和mime
类型,这使得图像验证非常简单。这就是我现在所处的文件输入,然后再将其传递给Image Intervention。
验证员检查处理干预前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');
};
});
希望这有帮助。
答案 1 :(得分:4)
简单地说,将其集成以获得验证
$this->validate($request, ['file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);
答案 2 :(得分:1)
我有custum表格,这个变种不起作用。所以我使用了regexp验证
像这样: sqlStr.Append("SELECT count(CustomerRace) as CustomerCount, CustomerRace");
sqlStr.Append("FROM Customer");
sqlStr.Append("WHERE DATEDIFF(yy,CustomerDOB,GETDATE()) > 64");
sqlStr.Append("GROUP BY CustomerRace");
可能对某人有帮助
答案 3 :(得分:1)
图像支持的格式 http://image.intervention.io/getting_started/formats
可读图像格式取决于所选的驱动程序(GD或 imagick)和您的本地配置。默认情况下,干预图像 当前支持以下主要格式。
JPEG PNG GIF TIF BMP ICO PSD WebP
GD✔️✔️✔️----✔️*
想象一下✔️✔️✔️✔️✔️✔️✔️✔️*
请参阅make方法的文档,以了解如何从不同来源读取图像格式,分别进行编码和保存以了解如何输出图像。
注意:( Intervention Image是一个开源的PHP图像处理和操作库 http://image.intervention.io/)。该库不验证任何验证规则,由Larval完成 验证器类
Laravel文档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();
}
答案 4 :(得分:0)
public function store(Request $request)
{
$room = 1;
if ($room == 1) {
//image upload start
if ($request->hasfile('photos')) {
foreach ($request->file('photos') as $image) {
// dd($request->file('photos'));
$rand = mt_rand(100000, 999999);
$name = time() . "_" . $rand . "." . $image->getClientOriginalExtension();
$name_thumb = time() . "_" . $rand . "_thumb." . $image->getClientOriginalExtension();
//return response()->json(['a'=>storage_path() . '/app/public/postimages/'. $name]);
//move image to postimages folder
//dd('ok');
// public_path().'/images/
$image->move(public_path() . '/postimages/', $name);
// 1280
$resizedImage = Image::make(public_path() . '/postimages/' . $name)->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
// save file as jpg with medium quality
$resizedImage->save(public_path() . '/postimages/' . $name, 60);
// $resizedImage_thumb->save(public_path() . '/postimages/' . $name_thumb, 60);
$data[] = $name;
//insert into picture table
$pic = new Photo();
$pic->name = $name;
$room->photos()->save($pic);
}
}
return response()->json(['success' => true, 'message' => 'Room Created!!'], 200);
} else {
return response()->json(['success' => false, 'message' => 'Error!!']);
}
}
}