ExamReportShareImage.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Http\Controllers\Api\Video;
  3. use App\Facades\Servers\WechatMini\Mini;
  4. use App\Http\Controllers\Api\Api;
  5. use App\Models\Video\Course;
  6. use App\Models\Video\ExamRecord;
  7. use Intervention\Image\Facades\Image;
  8. use Intervention\Image\Gd\Font;
  9. class ExamReportShareImage extends Api
  10. {
  11. //通过课程id获取课程名称
  12. public function get_share_image(ExamRecord $examRecordModel, Course $courseModel)
  13. {
  14. // 登录信息
  15. $uid = $this->checkLogin();
  16. // 接收参数
  17. $courseId = request('course_id', 0);
  18. $fontPath = public_path() . '/fonts/msyh14.ttf'; // 指定字体文件路径
  19. //学习课程名称
  20. $courseName = $courseModel->query()->where([['id', '=', $courseId]])->value('name');
  21. $courseName = str_replace(' ', '', $courseName); //去除课程名称中的空格
  22. //获取总积分(客户)
  23. $totalScore = $examRecordModel->query()->where([['course_id', '=', $courseId], ['custom_uid', '=', $uid]])->sum('get_score');
  24. try {
  25. // 加载图片
  26. $image = Image::make(public_path('uploads/images/default/') . 'learn_report_share_base.png');
  27. $lineHeight = 30; // 行高
  28. $fontSize = 24;
  29. $maxWidth = 330;
  30. // 课程名称过长的时候需要截断
  31. $lines = $this->truncateText($courseName, $fontPath, $fontSize, $maxWidth);
  32. $y = 170; // 初始 y 坐标
  33. // 处理数据
  34. foreach ($lines as $line) {
  35. $image->text($line, 30, $y, function ($font) use ($fontPath, $fontSize) {
  36. $font->file($fontPath);
  37. $font->size($fontSize);
  38. $font->color('#2B3D7c');
  39. $font->align('left');
  40. });
  41. $y += $lineHeight; // 下一行的 y 坐标
  42. }
  43. // 给图片写入文字(总积分)
  44. $image->text($totalScore, 90, 310, function (Font $font) use ($fontPath) {
  45. $font->file($fontPath); // 字体文件地址
  46. $font->size(30); // 字体大小
  47. $font->color('#E86D8F');
  48. $font->align('center');
  49. });
  50. // 基础路径
  51. $basepath = 'images/video_report_exam/'.date('Ymd').'/';
  52. // 保存路径
  53. $path = public_path('uploads/'.$basepath);
  54. // 如果路劲不存在
  55. if (!is_dir($path)) mkdir($path, 0755, true);
  56. // 路径组合
  57. $filename = $courseId.'_'.$uid.'.jpg';
  58. // 保存图片
  59. $image->encode('jpg', 90)->save($path.$filename);
  60. // 下发路径
  61. $path = path_compat($basepath.$filename);
  62. // 返回路径
  63. return json_send(['code' => 'success', 'msg' => '生成成功', 'data' => ['image_url' => $path.'?t='.time(),'title'=>$courseName]]);
  64. } catch (\Throwable $th) {
  65. // 返回
  66. return json_send(['code' => 'error', 'msg' => $th->getMessage()]);
  67. }
  68. }
  69. //获取课后分析的答题数量
  70. public function get_after_study_report_image_share(ExamRecord $examRecordModel)
  71. {
  72. // 登录信息
  73. $uid = $this->checkLogin();
  74. // 接收参数
  75. $courseId = request('course_id', 0);
  76. $recordId = request('record_id', 0);
  77. $url = request('page', 'pages/video/index');
  78. $size = request('size', 150);
  79. $scene = request('scene', '');
  80. $position = 'bottom-left';
  81. $fontPath = public_path() . '/fonts/msyh14.ttf'; // 指定字体文件路径
  82. //获取总积分(客户)
  83. $reportData = $examRecordModel->query()->where([['id','=',$recordId]])->first(['question_total','isanswer_total']);
  84. // 如果没有数据
  85. if( !$reportData ) return json_send(['code' => 'error', 'msg' => '没有答题记录']);
  86. // 返回结果
  87. $totalQuestionNum = $reportData['question_total'];
  88. $correctNum = $reportData['isanswer_total'];
  89. $inCorrectNum = $totalQuestionNum - $correctNum;
  90. $accuracy = $totalQuestionNum ? ($correctNum / $totalQuestionNum) * 100 : 100;
  91. try {
  92. // 加载图片
  93. $image = Image::make(public_path('uploads/images/default/') . 'analysis_report_share_base.png');
  94. // 生成小程序二维码
  95. $qrcode = Mini::getUnlimit($scene, ['page' => $url, 'width' => $size, 'is_hyaline' => true]);
  96. // 错误提示
  97. if (isset($qrcode['error'])) return json_send(['code' => 'error', 'msg' => $qrcode['error']]);
  98. // 加载图片 压缩
  99. $qrcode = $size < 280 ? Image::make($qrcode)->resize($size, $size) : Image::make($qrcode);
  100. $image->insert($qrcode, $position, 20, 20);
  101. // 给图片写入文字总题数:
  102. $image->text($totalQuestionNum . '题', 260, 300, function (Font $font) use ($fontPath) {
  103. $font->file($fontPath); // 字体文件地址
  104. $font->size(24); // 字体大小
  105. $font->color('#FF7B01');
  106. $font->align('left');
  107. });
  108. // 给图片写入文字正确题数
  109. $image->text($correctNum . '题', 290, 388, function (Font $font) use ($fontPath) {
  110. $font->file($fontPath); // 字体文件地址
  111. $font->size(24); // 字体大小
  112. $font->color('#0E670F');
  113. $font->align('left');
  114. });
  115. // 给图片写入文字错误题数
  116. $image->text($inCorrectNum . '题', 290, 476, function (Font $font) use ($fontPath) {
  117. $font->file($fontPath); // 字体文件地址
  118. $font->size(24); // 字体大小
  119. $font->color('#FF0101');
  120. $font->align('left');
  121. });
  122. // 给图片写入文字(正确率)
  123. $image->text($accuracy . '%', 100, 582, function (Font $font) use ($fontPath) {
  124. $font->file($fontPath); // 字体文件地址
  125. $font->size(36); // 字体大小
  126. $font->color('#FA9517');
  127. $font->align('left');
  128. });
  129. //插入二维码图片
  130. $image = $image->encode('jpg', 90)->__toString();
  131. // 转base64
  132. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  133. // 返回
  134. return json_send(['code' => 'success', 'msg' => '生成成功', 'data' => ['base64_image' => $base64]]);
  135. } catch (\Throwable $th) {
  136. dd($th);
  137. // 返回
  138. return json_send(['code' => 'error', 'msg' => $th->getMessage()]);
  139. }
  140. }
  141. // 截断文字并添加省略号
  142. function truncateText($text, $fontFile, $fontSize, $maxWidth)
  143. {
  144. $lines = [];
  145. $textLength = mb_strlen($text);
  146. $charIndex = 0;
  147. // 第一行
  148. $firstLine = '';
  149. while ($charIndex < $textLength) {
  150. $firstLine .= mb_substr($text, $charIndex, 1);
  151. $testBox = imagettfbbox($fontSize, 0, $fontFile, $firstLine);
  152. $testWidth = abs($testBox[2] - $testBox[0]);
  153. if ($testWidth > $maxWidth) {
  154. // 第一行超出宽度,截断
  155. $firstLine = mb_substr($firstLine, 0, -1);
  156. break;
  157. }
  158. $charIndex++;
  159. }
  160. // 第一行没有超出宽度,直接添加到结果中
  161. $lines[] = $firstLine;
  162. // 第二行
  163. if ($charIndex < $textLength) {
  164. $secondLine = mb_substr($text, $charIndex);
  165. $testBox = imagettfbbox($fontSize, 0, $fontFile, $secondLine);
  166. $testWidth = abs($testBox[2] - $testBox[0]);
  167. if ($testWidth > $maxWidth) {
  168. // 第二行超出宽度,截断并添加省略号
  169. while ($charIndex < $textLength) {
  170. $secondLine = mb_substr($secondLine, 0, -1);
  171. $testBox = imagettfbbox($fontSize, 0, $fontFile, $secondLine);
  172. $testWidth = abs($testBox[2] - $testBox[0]);
  173. if ($testWidth <= $maxWidth) {
  174. $secondLine .= '...';
  175. break;
  176. }
  177. }
  178. }
  179. $lines[] = $secondLine;
  180. }
  181. return $lines;
  182. }
  183. }