ImageUploadHandler.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Handlers;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Str;
  6. class ImageUploadHandler
  7. {
  8. protected $allowed_ext = ["png", "jpg", "gif", 'jpeg'];
  9. public function save($file, $folder, $disk, $file_prefix = '')
  10. {
  11. // 获取文件的后缀名,因图片从剪贴板里黏贴时后缀名为空,所以此处确保后缀一直存在
  12. $extension = strtolower($file->getClientOriginalExtension()) ?: 'png';
  13. // 拼接文件名,加前缀是为了增加辨析度,前缀可以是相关数据模型的 ID
  14. // 值如:1_1493521050_7BVc9v9ujP.png
  15. if ($file_prefix) {
  16. $filename = $file_prefix . '_' . time() . '_' . Str::random(10) . '.' . $extension;
  17. } else {
  18. $filename = time() . '_' . Str::random(10) . '.' . $extension;
  19. }
  20. // 如果上传的不是图片将终止操作
  21. if (!in_array($extension, $this->allowed_ext)) {
  22. return false;
  23. }
  24. // 将图片移动到我们的目标存储路径中
  25. $path = Storage::disk($disk)->putFileAs($folder, $file, $filename);
  26. return $path;
  27. }
  28. /**
  29. * 创建邀请新用户微信二维码 createWechatQrcode
  30. *
  31. * @param $user_id
  32. * @return bool
  33. * @author Fx
  34. *
  35. */
  36. public function createWechatQrcode($folder,$user_id,$endfilename){
  37. // easywechat 生成二维码
  38. $easyWechat = app('wechat.mini_program');
  39. $response = $easyWechat->app_code->getUnlimit($user_id, [
  40. 'page' => 'pages/index/index',
  41. 'width' => 340,
  42. ]);
  43. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  44. // 保存二维码
  45. $response->saveAs($folder, $user_id . $endfilename);
  46. return true;
  47. } else {
  48. Log::error('微信生成二维码失败' . $response);
  49. return false;
  50. }
  51. }
  52. }