LotteryImageService.php 5.2 KB

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