thinkphp实现图片在线转换base64编码
演示
demo
- <?php
- namespace appindexcontroller;
-
- use thinkexceptionValidateException;
- use thinkfacadeView;
- use thinkRequest;
-
- class Base641
- {
- /**
- * 显示上传页面
- */
- public function index()
- {
- return View::fetch('index');
- }
-
- /**
- * 处理上传请求
- */
- public function upload(Request $request)
-
- {
-
- // 获取上传文件
- $file = $request->file('image');
- if (empty($file)) {
- return json(['code' => 1, 'msg' => '请选择上传文件']);
- }
-
- // 验证上传文件
- try {
- validate(['image' => 'fileSize:102400|fileExt:jpg,jpeg,png,gif'])->check(['image' => $file]);
- } catch (ValidateException $e) {
- return json(['code' => 1, 'msg' => $e->getMessage()]);
- }
-
- // 转换成base64编码
- $base64 = base64_encode(file_get_contents($file->getRealPath()));
- $data = 'data:' . $file->getMime() . ';base64,' . $base64;
-
- // return json(['code' => 0, 'msg' => '上传成功', 'data' => $data]);
- return json(['code' => 0, 'msg' => '上传成功', 'data' => $base64]);
-
- }
- }
不会集成到自己网站的小伙伴可以联系我
演示地址:https://ssl.goolibao.com/base641
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!