LotteryImageService.php 4.5 KB

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