LotteryImageService.php 4.4 KB

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