123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Handlers;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- class ImageUploadHandler
- {
- protected $allowed_ext = ["png", "jpg", "gif", 'jpeg'];
- public function save($file, $folder, $disk, $file_prefix = '')
- {
- // 获取文件的后缀名,因图片从剪贴板里黏贴时后缀名为空,所以此处确保后缀一直存在
- $extension = strtolower($file->getClientOriginalExtension()) ?: 'png';
- // 拼接文件名,加前缀是为了增加辨析度,前缀可以是相关数据模型的 ID
- // 值如:1_1493521050_7BVc9v9ujP.png
- if ($file_prefix) {
- $filename = $file_prefix . '_' . time() . '_' . Str::random(10) . '.' . $extension;
- } else {
- $filename = time() . '_' . Str::random(10) . '.' . $extension;
- }
- // 如果上传的不是图片将终止操作
- if (!in_array($extension, $this->allowed_ext)) {
- return false;
- }
- // 将图片移动到我们的目标存储路径中
- $path = Storage::disk($disk)->putFileAs($folder, $file, $filename);
- return $path;
- }
- /**
- * 创建邀请新用户微信二维码 createWechatQrcode
- *
- * @param $user_id
- * @return bool
- * @author Fx
- *
- */
- public function createWechatQrcode($folder,$user_id,$endfilename){
- // easywechat 生成二维码
- $easyWechat = app('wechat.mini_program');
- $response = $easyWechat->app_code->getUnlimit($user_id, [
- 'page' => 'pages/index/index',
- 'width' => 340,
- ]);
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- // 保存二维码
- $response->saveAs($folder, $user_id . $endfilename);
- return true;
- } else {
- Log::error('微信生成二维码失败' . $response);
- return false;
- }
- }
- }
|