first()->val; App::setLocale($group_language); $html = $this->buildHtml($records); App::setLocale($lang); // ========= 2. 保存HTML到临时文件 ========= $htmlPath = storage_path("app/lottery_temp.html"); // $htmlPath = base_path() . "/public/static/html/lottery_temp.html"; // $htmlPath = base_path() . "/public/static/html/lottery_temp_{$group_language}.html"; file_put_contents($htmlPath, $html); // ========= 3. 输出图片路径 ========= $fileName = 'lottery_' . time() . '.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'); $rows = ''; foreach ($records as $row) { $rows .= '
| {$th1} | {$th2} | {$th3} | {$th4} | {$th5} |
|---|