LotteryImageService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(array $records): string
  43. {
  44. $rows = '';
  45. foreach ($records as $row) {
  46. $rows .= '<tr>';
  47. $rows .= '<td>' . htmlspecialchars($row['period']) . '</td>';
  48. $rows .= '<td>';
  49. foreach ($row['numbers'] as $i => $num) {
  50. $color = match ($i) {
  51. 0 => '#FF8A8A',
  52. 1 => '#8AB8FF',
  53. 2 => '#FFE699',
  54. default => '#000',
  55. };
  56. $rows .= "<span style='color: {$color};'>{$num}</span>";
  57. if ($i < count($row['numbers']) - 1) $rows .= ' + ';
  58. }
  59. $rows .= ' = ' . $row['sum'] . '</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. margin: 0;
  77. padding: 20px;
  78. min-height: 100vh;
  79. }
  80. .container {
  81. max-width: 800px;
  82. margin: 0 auto;
  83. background: white;
  84. border-radius: 15px;
  85. box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  86. overflow: hidden;
  87. }
  88. .header {
  89. background: linear-gradient(45deg, #e74c3c, #c0392b);
  90. color: white;
  91. padding: 20px;
  92. text-align: center;
  93. }
  94. .header h1 {
  95. margin: 0;
  96. font-size: 28px;
  97. text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  98. }
  99. .results-table {
  100. width: 100%;
  101. border-collapse: collapse;
  102. }
  103. .results-table th {
  104. background: #34495e;
  105. color: white;
  106. padding: 15px 10px;
  107. text-align: center;
  108. }
  109. .results-table td {
  110. padding: 12px 10px;
  111. text-align: center;
  112. border-bottom: 1px solid #e0e0e0;
  113. font-weight: 900;
  114. }
  115. .footer {
  116. background: #ecf0f1;
  117. padding: 15px;
  118. text-align: center;
  119. color: #7f8c8d;
  120. font-size: 12px;
  121. }
  122. </style>
  123. </head>
  124. <body>
  125. <div class="container">
  126. <div class="header">
  127. <h1>🎯 加拿大28开奖记录</h1>
  128. <div class="subtitle">最近开奖记录</div>
  129. </div>
  130. <table class="results-table">
  131. <thead>
  132. <tr>
  133. <th>回合</th>
  134. <th>结果</th>
  135. <th>组合</th>
  136. <th>极值</th>
  137. <th>尾数</th>
  138. </tr>
  139. </thead>
  140. <tbody>
  141. {$rows}
  142. </tbody>
  143. </table>
  144. <div class="footer">
  145. 生成时间: {$time} | 数据仅供参考
  146. </div>
  147. </div>
  148. </body>
  149. </html>
  150. HTML;
  151. }
  152. }