LotteryImageService.php 4.3 KB

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