LotteryImageService.php 5.1 KB

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