LotteryImageService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. $m= new Message();
  46. $m->json =Storage::url('lottery/' . $fileName);
  47. $m->save();
  48. return Storage::url('lottery/' . $fileName);
  49. }
  50. /**
  51. * 生成HTML内容
  52. */
  53. protected function buildHtml($records): string
  54. {
  55. $time = now()->format('Y-m-d H:i:s');
  56. $lang = App::getLocale();
  57. $group_language = Config::where('field', 'group_language')->first()->val;
  58. App::setLocale($group_language);
  59. $rows = '';
  60. foreach ($records as $row) {
  61. $rows .= '<tr>';
  62. $rows .= '<td>' . htmlspecialchars($row['issue_no']) ." ". lang("期").'</td>';
  63. $rows .= '<td>';
  64. foreach ($row['winning_numbers'] as $i => $num) {
  65. $color = match ($i) {
  66. 0 => '#FF8A8A',
  67. 1 => '#8AB8FF',
  68. 2 => '#FFDF80',
  69. default => '#000',
  70. };
  71. $rows .= "<span style='color: {$color};'>{$num}</span>";
  72. if ($i < count($row['winning_numbers']) - 1) $rows .= ' + ';
  73. }
  74. $rows .= ' = ' . array_sum($row['winning_numbers']) . '</td>';
  75. $rows .= '<td>' . $row['combo'] . '</td>';
  76. $rows .= '<td>' . $row['extreme'] . '</td>';
  77. $rows .= '<td>' . $row['tail'] . '</td>';
  78. $rows .= '</tr>';
  79. }
  80. $Canada28LotteryResults = lang("加拿大28开奖记录");
  81. $recentLotteryResults = lang("最近开奖记录");
  82. $th1 = lang("回合");
  83. $th2 = lang("结果");
  84. $th3 = lang("组合");
  85. $th4 = lang("极值");
  86. $th5 = lang("尾数");
  87. $createdTime = lang("生成时间");
  88. $dataForReferenceOnly = lang("数据仅供参考");
  89. App::setLocale($lang);
  90. return <<<HTML
  91. <!DOCTYPE html>
  92. <html>
  93. <head>
  94. <meta charset="UTF-8">
  95. <title>{$Canada28LotteryResults}</title>
  96. <style>
  97. body {
  98. font-family: "Microsoft YaHei", Arial, sans-serif;
  99. /* background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); */
  100. /* background: #667eea 0%; */
  101. margin: 0;
  102. padding: 5px;
  103. min-height: 100vh;
  104. }
  105. .container {
  106. max-width: 800px;
  107. margin: 0 auto;
  108. background: white;
  109. border-radius: 15px;
  110. box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  111. overflow: hidden;
  112. }
  113. .header {
  114. /* background: linear-gradient(45deg, #e74c3c, #c0392b); */
  115. background: #e74c3c;
  116. color: white;
  117. padding: 20px;
  118. text-align: center;
  119. }
  120. .header h1 {
  121. margin: 0;
  122. font-size: 28px;
  123. text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  124. }
  125. .results-table {
  126. width: 100%;
  127. border-collapse: collapse;
  128. }
  129. .results-table th {
  130. background: #34495e;
  131. color: white;
  132. padding: 15px 10px;
  133. text-align: center;
  134. }
  135. .results-table td {
  136. padding: 12px 10px;
  137. text-align: center;
  138. border-bottom: 1px solid #e0e0e0;
  139. font-weight: 900;
  140. }
  141. .footer {
  142. background: #ecf0f1;
  143. padding: 15px;
  144. text-align: center;
  145. color: #7f8c8d;
  146. font-size: 12px;
  147. }
  148. </style>
  149. </head>
  150. <body>
  151. <div class="container">
  152. <div class="header">
  153. <h1>{$Canada28LotteryResults}</h1>
  154. <div class="subtitle">{$recentLotteryResults}</div>
  155. </div>
  156. <table class="results-table">
  157. <thead>
  158. <tr>
  159. <th>{$th1}</th>
  160. <th>{$th2}</th>
  161. <th>{$th3}</th>
  162. <th>{$th4}</th>
  163. <th>{$th5}</th>
  164. </tr>
  165. </thead>
  166. <tbody>
  167. {$rows}
  168. </tbody>
  169. </table>
  170. <div class="footer">
  171. {$createdTime}: {$time} | {$dataForReferenceOnly}
  172. </div>
  173. </div>
  174. </body>
  175. </html>
  176. HTML;
  177. }
  178. }