php - 从字符串中提取base64图像并以表格形式发布
问题描述
我正在尝试在我的Laravel
项目中使用我的表单上传图像。我有一个图像裁剪器,可以将图像保存为data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/..
裁剪器发送一个JSON
包含base64
文件编码字符串的字符串,我需要将JSON
字符串解析为一个对象,然后提取base64
字符串并将其转换为文件对象。
我Image Intervention
用于上传过程
控制器功能
public function store(Request $request)
{
// save post in db
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->user_id = auth()->id();
if ($request->hasFile('featimage')) {
$image = $request->file('featimage');
$filename = time() . '.' . $image->getCLientOriginalExtension();
$image = trim($image);
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace('data:image/jpg;base64,', '', $image);
$image = str_replace('data:image/jpeg;base64,', '', $image);
$image = str_replace('data:image/gif;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imagedata = base64_decode($image);
//Set image whole path here
$location = public_path('images/uploads/' . $filename);
Image::make($image)->save($location);
$post->image = $filename;
}
$post->save();
$post->tags()->sync($request->tags, false);
Session::flash('success', 'The blog post was saved successfully!');
return redirect()->route('posts.show', $post->id);
}
看法
<form class="form-horizontal" action="{{route('posts.store')}}" method="POST" enctype="multipart/form-data">{{ csrf_field() }}
<fieldset class="form-group">
<label for="featimage">Upload Image</label>
<input type="file" class="form-control slim" data-ratio="4:3" name="featimage">
<label class="col-md-2 col-form-label">Post Body</label>
<textarea type="textarea" class="form-control" rows="10" name="body"></textarea>
</fieldset>
<button class="btn btn-primary btn-block" name="submit">Save</button>
<button class="btn btn-danger btn-block" name="submit">Cancel</button>
</form>
我读过其他类似的帖子,我显然遗漏了一些东西,但我只是不知道是什么。
解决方案
你可以试试这个功能。
function uploadImage($base_64_string,$image_name)
{
preg_match("/^data:image\/(.*);base64/i", $base_64_string, $match);
$extension_array = explode('/', (explode(';', $base_64_string))[0]);
$extension = end($extension_array);
$img = $base_64_string;
if ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg'){
$img = str_replace('data:image/' . $extension . ';base64,', '', $img);}
elseif ($extension == 'mp4' || $extension == 'mov'){
$img = str_replace('data:video/' . $extension . ';base64,', '', $img);
}
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$fileNameI = $image_name.'_'.rand().time() . '.' . $extension;
//Uploading Image To S3 Bucket
$s3 = Storage::disk('s3-image');
$filePath = Config::get('common.Image_Folder') . $fileNameI;
$s3->put($filePath, $data, 'public');
return $fileNameI;
}
如果您没有 Bucket,您可以将图像存储在本地目录中。
推荐阅读
- r - 如何将 R 数据框上传到谷歌驱动器?
- scala - zipPartitions : 强制将两个分区持久化在同一个执行器上
- android - 始终在条带上获取状态“不完整”
- python-3.x - wxpython安装失败
- python - 字典键和值结合 django 查询
- angularjs - browser-sync 不更新 ngModel
- javascript - 我应该如何避免这种“警告:道具类型失败:您在没有 `onChange` 处理程序的情况下向表单字段提供了 `value` 道具。”
- html - 我应该/以及如何创建我的“目录结构”
- php - 使用未定义的常量“产品” - laravel 5.6
- excel - 用 0 替换空