浏览代码

图片生成

seven 5 天之前
父节点
当前提交
50adc42163
共有 3 个文件被更改,包括 190 次插入0 次删除
  1. 27 0
      app/Http/Controllers/admin/Lottery.php
  2. 161 0
      app/Services/LotteryImageService.php
  3. 2 0
      routes/admin.php

+ 27 - 0
app/Http/Controllers/admin/Lottery.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Http\Controllers\admin;
+
+use App\Services\LotteryImageService;
+use Illuminate\Http\Request;
+use App\Http\Controllers\Controller;
+
+class Lottery extends Controller
+{
+    public function generate(Request $request, LotteryImageService $service)
+    {
+        // 这里可以从接口传入数据,也可以先写死测试
+        $records = $request->input('records', [
+            ['period' => '3350007', 'numbers' => [5, 2, 0], 'sum' => 7, 'combo' => '小 单', 'extreme' => '-', 'tail' => '尾7'],
+            ['period' => '3350006', 'numbers' => [8, 9, 3], 'sum' => 20, 'combo' => '大 双', 'extreme' => '-', 'tail' => '尾0'],
+            ['period' => '3350005', 'numbers' => [2, 6, 1], 'sum' => 9, 'combo' => '小 单', 'extreme' => '-', 'tail' => '尾9'],
+        ]);
+
+        try {
+            $url = $service->generate($records);
+            return response()->json(['code' => 0, 'url' => asset($url)]);
+        } catch (\Throwable $e) {
+            return response()->json(['code' => 1, 'msg' => $e->getMessage()]);
+        }
+    }
+}

+ 161 - 0
app/Services/LotteryImageService.php

@@ -0,0 +1,161 @@
+<?php
+
+namespace App\Services;
+
+use Illuminate\Support\Facades\Storage;
+
+class LotteryImageService
+{
+    /**
+     * 生成开奖记录图片
+     * @param array $records 开奖记录数组
+     * @return string 图片的访问URL
+     */
+    public function generate(array $records): string
+    {
+        // ========= 1. 生成HTML内容 =========
+        $html = $this->buildHtml($records);
+
+        // ========= 2. 保存HTML到临时文件 =========
+        $htmlPath = storage_path('app/lottery_temp.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. 调用Chromium截图 =========
+        $cmd = sprintf(
+            'chromium --headless --disable-gpu --screenshot=%s --window-size=1200,2000 file://%s',
+            escapeshellarg($outputPath),
+            escapeshellarg($htmlPath)
+        );
+        exec($cmd, $output, $code);
+
+        if ($code !== 0) {
+            throw new \Exception('图片生成失败,请检查是否安装chromium命令');
+        }
+
+        // ========= 5. 返回访问URL =========
+        return Storage::url('lottery/' . $fileName);
+    }
+
+    /**
+     * 生成HTML内容
+     */
+    protected function buildHtml(array $records): string
+    {
+        $rows = '';
+        foreach ($records as $row) {
+            $rows .= '<tr>';
+            $rows .= '<td>' . htmlspecialchars($row['period']) . '</td>';
+            $rows .= '<td>';
+            foreach ($row['numbers'] as $i => $num) {
+                $color = match ($i) {
+                    0 => '#FF8A8A',
+                    1 => '#8AB8FF',
+                    2 => '#FFE699',
+                    default => '#000',
+                };
+                $rows .= "<span style='color: {$color};'>{$num}</span>";
+                if ($i < count($row['numbers']) - 1) $rows .= ' + ';
+            }
+            $rows .= ' = ' . $row['sum'] . '</td>';
+            $rows .= '<td>' . $row['combo'] . '</td>';
+            $rows .= '<td>' . $row['extreme'] . '</td>';
+            $rows .= '<td>' . $row['tail'] . '</td>';
+            $rows .= '</tr>';
+        }
+
+        $time = now()->format('Y-m-d H:i:s');
+
+        return <<<HTML
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8">
+<title>加拿大28开奖记录</title>
+<style>
+body {
+    font-family: "Microsoft YaHei", Arial, sans-serif;
+    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+    margin: 0;
+    padding: 20px;
+    min-height: 100vh;
+}
+.container {
+    max-width: 800px;
+    margin: 0 auto;
+    background: white;
+    border-radius: 15px;
+    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
+    overflow: hidden;
+}
+.header {
+    background: linear-gradient(45deg, #e74c3c, #c0392b);
+    color: white;
+    padding: 20px;
+    text-align: center;
+}
+.header h1 {
+    margin: 0;
+    font-size: 28px;
+    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
+}
+.results-table {
+    width: 100%;
+    border-collapse: collapse;
+}
+.results-table th {
+    background: #34495e;
+    color: white;
+    padding: 15px 10px;
+    text-align: center;
+}
+.results-table td {
+    padding: 12px 10px;
+    text-align: center;
+    border-bottom: 1px solid #e0e0e0;
+    font-weight: 900;
+}
+.footer {
+    background: #ecf0f1;
+    padding: 15px;
+    text-align: center;
+    color: #7f8c8d;
+    font-size: 12px;
+}
+</style>
+</head>
+<body>
+<div class="container">
+    <div class="header">
+        <h1>🎯 加拿大28开奖记录</h1>
+        <div class="subtitle">最近开奖记录</div>
+    </div>
+    <table class="results-table">
+        <thead>
+            <tr>
+                <th>回合</th>
+                <th>结果</th>
+                <th>组合</th>
+                <th>极值</th>
+                <th>尾数</th>
+            </tr>
+        </thead>
+        <tbody>
+            {$rows}
+        </tbody>
+    </table>
+    <div class="footer">
+        生成时间: {$time} | 数据仅供参考
+    </div>
+</div>
+</body>
+</html>
+HTML;
+    }
+}

+ 2 - 0
routes/admin.php

@@ -17,10 +17,12 @@ use App\Http\Controllers\admin\Game;
 use App\Http\Controllers\admin\GameplayRule;
 use App\Http\Controllers\admin\Issue;
 use App\Http\Controllers\admin\Keyboard;
+use App\Http\Controllers\admin\Lottery;
 
 
 Route::post('/login', [Admin::class, 'login']);
 Route::get('/test', [Wallet::class, 'test']);
+Route::get('/image', [Lottery::class, 'generate']);
 
 Route::prefix('/sync')->group(function () {
     Route::get('/collect', [Sync::class, 'collect']);