buildHtml($records); // ========= 2. 保存HTML到临时文件 ========= // $htmlPath = storage_path("app/lottery_temp.html"); $group_language = Config::where('field', 'group_language')->first()->val; $htmlPath = base_path() . "/public/static/html/lottery_temp.html"; file_put_contents($htmlPath, $html); // ========= 3. 输出图片路径 ========= $fileName = 'lottery_' . $records[0]['issue_no'] . '.png'; $outputPath = storage_path('app/public/lottery/' . $fileName); if (!is_dir(dirname($outputPath))) { mkdir(dirname($outputPath), 0755, true); } // ========= 4. 调用 wkhtmltoimage ========= // --quality 100 提高图片质量,可选 $cmd = sprintf( 'wkhtmltoimage --quality 100 %s %s', escapeshellarg($htmlPath), escapeshellarg($outputPath) ); exec($cmd, $output, $code); if ($code !== 0 || !file_exists($outputPath)) { throw new \Exception('图片生成失败,请检查是否已安装 wkhtmltoimage 并可在命令行执行'); } // ========= 5. 返回访问URL ========= return Storage::url('lottery/' . $fileName); } /** * 生成HTML内容 */ protected function buildHtml($records): string { $time = now()->format('Y-m-d H:i:s'); $lang = App::getLocale(); $group_language = Config::where('field', 'group_language')->first()->val; App::setLocale($group_language); $rows = ''; foreach ($records as $row) { $rows .= ''; $rows .= '' . htmlspecialchars($row['issue_no']) ." ". lang("期").''; $rows .= ''; foreach ($row['winning_numbers'] as $i => $num) { $color = match ($i) { 0 => '#FF8A8A', 1 => '#8AB8FF', 2 => '#FFDF80', default => '#000', }; $rows .= "{$num}"; if ($i < count($row['winning_numbers']) - 1) $rows .= ' + '; } $rows .= ' = ' . array_sum($row['winning_numbers']) . ''; $rows .= '' . $row['combo'] . ''; $rows .= '' . $row['extreme'] . ''; $rows .= '' . $row['tail'] . ''; $rows .= ''; } $Canada28LotteryResults = lang("加拿大28开奖记录"); $recentLotteryResults = lang("最近开奖记录"); $th1 = lang("回合"); $th2 = lang("结果"); $th3 = lang("组合"); $th4 = lang("极值"); $th5 = lang("尾数"); $createdTime = lang("生成时间"); $dataForReferenceOnly = lang("数据仅供参考"); App::setLocale($lang); return << {$Canada28LotteryResults}

{$Canada28LotteryResults}

{$recentLotteryResults}
{$rows}
{$th1} {$th2} {$th3} {$th4} {$th5}
HTML; } }