LotteryImageService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Storage;
  4. class LotteryImageService
  5. {
  6. /**
  7. * 生成开奖记录图片
  8. * @param array $records 开奖记录数组
  9. * @return string 图片的访问URL
  10. */
  11. public function generate(array $records): string
  12. {
  13. // ========= 1. 生成HTML内容 =========
  14. $html = $this->buildHtml($records);
  15. // ========= 2. 保存HTML到临时文件 =========
  16. $htmlPath = storage_path('app/lottery_temp.html');
  17. file_put_contents($htmlPath, $html);
  18. // ========= 3. 输出图片路径 =========
  19. $fileName = 'lottery_' . time() . '.png';
  20. $outputPath = storage_path('app/public/lottery/' . $fileName);
  21. if (!is_dir(dirname($outputPath))) {
  22. mkdir(dirname($outputPath), 0755, true);
  23. }
  24. // ========= 4. 调用Chromium截图 =========
  25. $cmd = sprintf(
  26. 'chromium --headless --disable-gpu --screenshot=%s --window-size=1200,2000 file://%s',
  27. escapeshellarg($outputPath),
  28. escapeshellarg($htmlPath)
  29. );
  30. exec($cmd, $output, $code);
  31. if ($code !== 0) {
  32. throw new \Exception('图片生成失败,请检查是否安装chromium命令');
  33. }
  34. // ========= 5. 返回访问URL =========
  35. return Storage::url('lottery/' . $fileName);
  36. }
  37. /**
  38. * 生成HTML内容
  39. */
  40. protected function buildHtml(array $records): string
  41. {
  42. $rows = '';
  43. foreach ($records as $row) {
  44. $rows .= '<tr>';
  45. $rows .= '<td>' . htmlspecialchars($row['period']) . '</td>';
  46. $rows .= '<td>';
  47. foreach ($row['numbers'] as $i => $num) {
  48. $color = match ($i) {
  49. 0 => '#FF8A8A',
  50. 1 => '#8AB8FF',
  51. 2 => '#FFE699',
  52. default => '#000',
  53. };
  54. $rows .= "<span style='color: {$color};'>{$num}</span>";
  55. if ($i < count($row['numbers']) - 1) $rows .= ' + ';
  56. }
  57. $rows .= ' = ' . $row['sum'] . '</td>';
  58. $rows .= '<td>' . $row['combo'] . '</td>';
  59. $rows .= '<td>' . $row['extreme'] . '</td>';
  60. $rows .= '<td>' . $row['tail'] . '</td>';
  61. $rows .= '</tr>';
  62. }
  63. $time = now()->format('Y-m-d H:i:s');
  64. return <<<HTML
  65. <!DOCTYPE html>
  66. <html>
  67. <head>
  68. <meta charset="UTF-8">
  69. <title>加拿大28开奖记录</title>
  70. <style>
  71. body {
  72. font-family: "Microsoft YaHei", Arial, sans-serif;
  73. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  74. margin: 0;
  75. padding: 20px;
  76. min-height: 100vh;
  77. }
  78. .container {
  79. max-width: 800px;
  80. margin: 0 auto;
  81. background: white;
  82. border-radius: 15px;
  83. box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  84. overflow: hidden;
  85. }
  86. .header {
  87. background: linear-gradient(45deg, #e74c3c, #c0392b);
  88. color: white;
  89. padding: 20px;
  90. text-align: center;
  91. }
  92. .header h1 {
  93. margin: 0;
  94. font-size: 28px;
  95. text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  96. }
  97. .results-table {
  98. width: 100%;
  99. border-collapse: collapse;
  100. }
  101. .results-table th {
  102. background: #34495e;
  103. color: white;
  104. padding: 15px 10px;
  105. text-align: center;
  106. }
  107. .results-table td {
  108. padding: 12px 10px;
  109. text-align: center;
  110. border-bottom: 1px solid #e0e0e0;
  111. font-weight: 900;
  112. }
  113. .footer {
  114. background: #ecf0f1;
  115. padding: 15px;
  116. text-align: center;
  117. color: #7f8c8d;
  118. font-size: 12px;
  119. }
  120. </style>
  121. </head>
  122. <body>
  123. <div class="container">
  124. <div class="header">
  125. <h1>🎯 加拿大28开奖记录</h1>
  126. <div class="subtitle">最近开奖记录</div>
  127. </div>
  128. <table class="results-table">
  129. <thead>
  130. <tr>
  131. <th>回合</th>
  132. <th>结果</th>
  133. <th>组合</th>
  134. <th>极值</th>
  135. <th>尾数</th>
  136. </tr>
  137. </thead>
  138. <tbody>
  139. {$rows}
  140. </tbody>
  141. </table>
  142. <div class="footer">
  143. 生成时间: {$time} | 数据仅供参考
  144. </div>
  145. </div>
  146. </body>
  147. </html>
  148. HTML;
  149. }
  150. }