LotteryImageService.php 5.1 KB

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