* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Jobs\Dwbs; use App\Jobs\Job; use App\Repositories\Enums\ModelStatusEnum; use App\Repositories\Models\Base\Log; use App\Repositories\Models\Base\Setting; use App\Repositories\Models\Base\User; use App\Repositories\Models\Dwbs\Chengjiu; use Carbon\Carbon; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Intervention\Image\Facades\Image; class MakeUserChengJiuJob extends Job { public $timeout = 1200; public $cj_id = 0; public $tries = 1; /** * Create a new job instance. */ public function __construct($cj_id) { $this->cj_id = $cj_id; } /** * Execute the job. */ public function handle() { $chengjiu = Chengjiu::query()->where('id', $this->cj_id)->first(); if (!$chengjiu) return false; $day = $chengjiu->day; $user = User::query()->where('id', $chengjiu->user_id)->select(['id', 'nickname'])->first(); if (!$user) return false; try { $bg = Setting::byCodeGetSetting('h5_base_img_huojian') ?? base_path('Data/huojian_bg.png'); $image = Image::make($bg)->resize(375, 646); $nickname = self::filterEmoji($user['nickname'], 7); self::addText($image, $nickname, 375 / 2, 210, 40, 'F00'); self::addText($image, Carbon::parse($day)->format('Y年m月d日'), 375 / 2, 458, 16, '3d3d3d'); $dir = "huojianImg/" . $day; $filename = Str::random() . '.png'; Storage::disk('public')->put("{$dir}/{$filename}.txt", 1); $path = Storage::disk('public')->path("{$dir}/{$filename}"); $image->save($path); Storage::disk('public')->delete("{$dir}/{$filename}.txt"); $chengjiu->path = "{$dir}/{$filename}"; $chengjiu->status = ModelStatusEnum::OK; $chengjiu->save(); } catch (\Exception $exception) { log_record('成就图生成', ['chengjiu' => $chengjiu['id']]); } return true; } public static function addText(&$bgImage, $text, $x, $y, $size, $color, $align = 'center', $valign = 'middle', $ziti = '') { if (!$ziti) $ziti = base_path('Data/ziti/ht.otf'); $bgImage->text($text, $x, $y, function ($font) use ($size, $color, $ziti, $align, $valign) { $font->file($ziti); $font->size($size); $font->color($color); $font->angle(0); $font->align($align); $font->valign($valign); }); } private static function filterEmoji($str, $maxLen = 5) { $str = json_encode($str); $str = json_decode(preg_replace("#(\\\ud[0-9a-f]{3})#i", "", $str)); if (mb_strlen($str) > $maxLen) { $str = mb_substr($str, 0, $maxLen); } return $str; } }