MakeUserChengJiuJob.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs\Dwbs;
  11. use App\Jobs\Job;
  12. use App\Repositories\Enums\ModelStatusEnum;
  13. use App\Repositories\Models\Base\Log;
  14. use App\Repositories\Models\Base\Setting;
  15. use App\Repositories\Models\Base\User;
  16. use App\Repositories\Models\Dwbs\Chengjiu;
  17. use Carbon\Carbon;
  18. use Illuminate\Support\Facades\Storage;
  19. use Illuminate\Support\Str;
  20. use Intervention\Image\Facades\Image;
  21. class MakeUserChengJiuJob extends Job
  22. {
  23. public $timeout = 1200;
  24. public $cj_id = 0;
  25. public $tries = 1;
  26. /**
  27. * Create a new job instance.
  28. */
  29. public function __construct($cj_id)
  30. {
  31. $this->cj_id = $cj_id;
  32. }
  33. /**
  34. * Execute the job.
  35. */
  36. public function handle()
  37. {
  38. $chengjiu = Chengjiu::query()->where('id', $this->cj_id)->first();
  39. if (!$chengjiu) return false;
  40. $day = $chengjiu->day;
  41. $user = User::query()->where('id', $chengjiu->user_id)->select(['id', 'nickname'])->first();
  42. if (!$user) return false;
  43. try {
  44. $bg = Setting::byCodeGetSetting('h5_base_img_huojian') ?? base_path('Data/huojian_bg.png');
  45. $image = Image::make($bg)->resize(375, 646);
  46. $nickname = self::filterEmoji($user['nickname'], 7);
  47. self::addText($image, $nickname, 375 / 2, 210, 40, 'F00');
  48. self::addText($image, Carbon::parse($day)->format('Y年m月d日'), 375 / 2, 458, 16, '3d3d3d');
  49. $dir = "huojianImg/" . $day;
  50. $filename = Str::random() . '.png';
  51. Storage::disk('public')->put("{$dir}/{$filename}.txt", 1);
  52. $path = Storage::disk('public')->path("{$dir}/{$filename}");
  53. $image->save($path);
  54. Storage::disk('public')->delete("{$dir}/{$filename}.txt");
  55. $chengjiu->path = "{$dir}/{$filename}";
  56. $chengjiu->status = ModelStatusEnum::OK;
  57. $chengjiu->save();
  58. } catch (\Exception $exception) {
  59. log_record('成就图生成', ['chengjiu' => $chengjiu['id']]);
  60. }
  61. return true;
  62. }
  63. public static function addText(&$bgImage, $text, $x, $y, $size, $color, $align = 'center', $valign = 'middle', $ziti = '')
  64. {
  65. if (!$ziti) $ziti = base_path('Data/ziti/ht.otf');
  66. $bgImage->text($text, $x, $y, function ($font) use ($size, $color, $ziti, $align, $valign) {
  67. $font->file($ziti);
  68. $font->size($size);
  69. $font->color($color);
  70. $font->angle(0);
  71. $font->align($align);
  72. $font->valign($valign);
  73. });
  74. }
  75. private static function filterEmoji($str, $maxLen = 5)
  76. {
  77. $str = json_encode($str);
  78. $str = json_decode(preg_replace("#(\\\ud[0-9a-f]{3})#i", "", $str));
  79. if (mb_strlen($str) > $maxLen) {
  80. $str = mb_substr($str, 0, $maxLen);
  81. }
  82. return $str;
  83. }
  84. }