验证 Laravel 中的 base64 解码图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39042731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate a base64 decoded image in laravel
提问by Jorge Almonacid
Im trying to get a image from a PUT request for update a user picture(using postman), and make it pass through a validation in Laravel 5.2, for making the call in postman use the following url:
我试图从 PUT 请求中获取图像以更新用户图片(使用邮递员),并使其通过 Laravel 5.2 中的验证,以便在邮递员中使用以下网址进行调用:
http://localhost:8000/api/v1/users?_method=PUT
http://localhost:8000/api/v1/users?_method=PUT
and send the image string in the body, using a json like this:
并在正文中发送图像字符串,使用这样的 json:
{
"picture" : "data:image/png;base64,this-is-the-base64-encode-string"
}
In the controller try a lot of differents ways for decode the image and try to pass the validation:
在控制器中尝试多种不同的方式解码图像并尝试通过验证:
First I tried this:
$data = request->input('picture'); $data = str_replace('data:image/png;base64,', '', $data); $data = str_replace(' ', '+', $data); $image = base64_decode($data); $file = app_path() . uniqid() . '.png'; $success = file_put_contents($file, $image);
Then I tried this:
list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); $typeFile = explode(':', $type); $extension = explode('/', $typeFile[1]); $ext = $extension[1]; Storage::put(public_path() . '/prueba.' . $ext, $data); $contents = Storage::get(base_path() . '/public/prueba.png');
Try to use the intervention image library (http://image.intervention.io/) and don't pass:
$image = Image::make($data); $image->save(app_path() . 'test2.png'); $image = Image::make(app_path() . 'test1.png');
首先我试过这个:
$data = request->input('picture'); $data = str_replace('data:image/png;base64,', '', $data); $data = str_replace(' ', '+', $data); $image = base64_decode($data); $file = app_path() . uniqid() . '.png'; $success = file_put_contents($file, $image);
然后我尝试了这个:
list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); $typeFile = explode(':', $type); $extension = explode('/', $typeFile[1]); $ext = $extension[1]; Storage::put(public_path() . '/prueba.' . $ext, $data); $contents = Storage::get(base_path() . '/public/prueba.png');
尝试使用干预图像库(http://image.intervention.io/)并且不要通过:
$image = Image::make($data); $image->save(app_path() . 'test2.png'); $image = Image::make(app_path() . 'test1.png');
This is the validation in the controller:
这是控制器中的验证:
$data = [
'picture' => $image,
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name')
];
$validator = Validator::make($data, User::rulesForUpdate());
if ($validator->fails()) {
return $this->respondFailedParametersValidation('Parameters failed validation for a user picture');
}
this is the validation in the User-model:
这是用户模型中的验证:
public static function rulesForUpdate() {
return [
'first_name' => 'max:255',
'last_name' => 'max:255',
'picture' => 'image|max:5000|mimes:jpeg,png'
];
}
回答by Mike McLin
If you are using Intervention anyways, then you can leverage it for a custom validation rule. I have one called "imageable". It basically makes sure that the input given will be able to be converted to an Intervention image. Base64 data image strings will pass. A string of "foo" will not.
如果您无论如何都在使用干预,那么您可以将其用于自定义验证规则。我有一个叫做“可成像”的。它基本上确保给定的输入能够转换为干预图像。Base64 数据图像字符串将通过。一串“foo”不会。
Validator::extend('imageable', function ($attribute, $value, $params, $validator) {
try {
ImageManagerStatic::make($value);
return true;
} catch (\Exception $e) {
return false;
}
});
This obviously just checks that the input is able to be converted to an image. However, it should show you as a use-case how you can leverage Intervention and any of it's methods to create a custom rule.
这显然只是检查输入是否能够转换为图像。但是,它应该作为用例向您展示如何利用 Intervention 及其任何方法来创建自定义规则。
回答by jnieto
You can extend the Validator class of Laravel.
您可以扩展 Laravel 的 Validator 类。
But anyway try this
但无论如何试试这个
Validator::extend('is_png',function($attribute, $value, $params, $validator) {
$image = base64_decode($value);
$f = finfo_open();
$result = finfo_buffer($f, $image, FILEINFO_MIME_TYPE);
return $result == 'image/png';
});
Don't forget the rules:
不要忘记规则:
$rules = array(
'image' => 'is_png'
);
回答by Hamodea Net
inside extend function add this
在扩展功能内添加这个
$res= mime_content_type($value);
if ($res == 'image/png' || $res == 'image/jpeg') {
return $res;
}