LotteryImageService.php 4.3 KB

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