ExamReportShareImage.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. foreach ($lines as $line) {
  34. $image->text($line, 30, $y, function ($font) use ($fontPath, $fontSize) {
  35. $font->file($fontPath);
  36. $font->size($fontSize);
  37. $font->color('#2B3D7c');
  38. $font->align('left');
  39. });
  40. $y += $lineHeight; // 下一行的 y 坐标
  41. }
  42. // 给图片写入文字(总积分)
  43. $image->text($courseName, 20, 120, function (Font $font) use ($fontPath) {
  44. $font->file($fontPath); // 字体文件地址
  45. $font->size(24); // 字体大小
  46. $font->color('#2B3D7c');
  47. $font->align('left');
  48. });
  49. // 给图片写入文字
  50. $image->text($totalScore, 40, 260, function (Font $font) use ($fontPath) {
  51. $font->file($fontPath); // 字体文件地址
  52. $font->size(24); // 字体大小
  53. $font->color('#E86D8F');
  54. $font->align('center');
  55. });
  56. //TODO 测试的时候用,接口返回的时候可以关掉
  57. /////开始
  58. //$outputImagePath = public_path('uploads/images/default/output_exam.jpg'); // 替换为你希望保存的路径
  59. //$image->save($outputImagePath);
  60. /////结束
  61. $image = $image->encode('jpg', 90)->__toString();
  62. // 转base64
  63. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  64. return json_send(['code'=>'success','msg'=>'图片生成成功','data'=>$base64]);
  65. } catch (\Throwable $th) {
  66. // 返回路径
  67. //return ['error'=>$th->getMessage()];
  68. return json_send(['code'=>'error','msg'=>'图片生成失败', 'errorCode'=>$th->getMessage()]);
  69. }
  70. }
  71. //获取课后分析的答题数量
  72. public function get_after_study_report_image_share(ExamRecord $examRecordModel, $scene)
  73. {
  74. // 登录信息
  75. $uid = $this->checkLogin();
  76. // 接收参数
  77. $courseId = request('course_id',0);
  78. //获取总积分(客户)
  79. $totalQuestionNum = $examRecordModel->query()->where([['course_id','=',$courseId], ['custom_uid','=',$uid]])->sum('question_total');
  80. $correctNum = $examRecordModel->query()->where([['course_id','=',$courseId], ['custom_uid','=',$uid]])->sum('isanswer_total');
  81. $inCorrectNum = $totalQuestionNum - $correctNum;
  82. $accuracy = (int) $correctNum/$totalQuestionNum;
  83. $url = 'pages/index/index';
  84. $position = 'bottom-left';
  85. $size = request('size',150);
  86. $scene = request('scene',0);
  87. $fontPath = public_path().'/fonts/msyh14.ttf';// 指定字体文件路径
  88. try {
  89. // 加载图片
  90. $image = Image::make(public_path('uploads/images/default/') . 'analysis_report_share_base.jpg');
  91. // 生成小程序二维码
  92. $qrcode = Mini::getUnlimit($scene,['page'=>$url,'width'=>$size,'is_hyaline'=>true]);
  93. // 错误提示
  94. if( isset($qrcode['error']) ) return $qrcode;
  95. if ($size < 280){
  96. // 加载图片 压缩
  97. $qrcode = Image::make($qrcode)->resize($size,$size);
  98. }else{
  99. // 加载图片
  100. $qrcode = Image::make($qrcode);
  101. }
  102. $image->insert($qrcode, $position,20,20);
  103. // 给图片写入文字总题数:
  104. $image->text($totalQuestionNum.'题', 260, 300, function (Font $font) use ($fontPath) {
  105. $font->file($fontPath); // 字体文件地址
  106. $font->size(24); // 字体大小
  107. $font->color('#FF7B01');
  108. $font->align('left');
  109. });
  110. // 给图片写入文字正确题数
  111. $image->text($correctNum.'题', 290, 388, function (Font $font) use ($fontPath) {
  112. $font->file($fontPath); // 字体文件地址
  113. $font->size(24); // 字体大小
  114. $font->color('#0E670F');
  115. $font->align('left');
  116. });
  117. // 给图片写入文字错误题数
  118. $image->text($inCorrectNum.'题', 290, 476, function (Font $font) use ($fontPath) {
  119. $font->file($fontPath); // 字体文件地址
  120. $font->size(24); // 字体大小
  121. $font->color('#FF0101');
  122. $font->align('left');
  123. });
  124. // 给图片写入文字 正确率
  125. $image->text($accuracy.'%', 100, 582, function (Font $font) use ($fontPath) {
  126. $font->file($fontPath); // 字体文件地址
  127. $font->size(36); // 字体大小
  128. $font->color('#FA9517');
  129. $font->align('left');
  130. });
  131. //插入二维码图片
  132. $image = $image->encode('jpg', 90)->__toString();
  133. // 转base64
  134. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  135. return json_send(['code'=>'success','msg'=>'图片生成成功','data'=>$base64]);
  136. } catch (\Throwable $th) {
  137. // 返回路径
  138. //return ['error'=>$th->getMessage()];
  139. return json_send(['code'=>'error','msg'=>'图片生成失败', 'errorCode'=>$th->getMessage()]);
  140. }
  141. }
  142. // 截断文字并添加省略号
  143. function truncateText($text, $fontFile, $fontSize, $maxWidth) {
  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. $lines[] = $firstLine;
  161. // 第二行
  162. if ($charIndex < $textLength) {
  163. $secondLine = mb_substr($text, $charIndex);
  164. $testBox = imagettfbbox($fontSize, 0, $fontFile, $secondLine);
  165. $testWidth = abs($testBox[2] - $testBox[0]);
  166. if ($testWidth > $maxWidth) {
  167. // 第二行超出宽度,截断并添加省略号
  168. while ($charIndex < $textLength) {
  169. $secondLine = mb_substr($secondLine, 0, -1);
  170. $testBox = imagettfbbox($fontSize, 0, $fontFile, $secondLine);
  171. $testWidth = abs($testBox[2] - $testBox[0]);
  172. if ($testWidth <= $maxWidth) {
  173. $secondLine .= '...';
  174. break;
  175. }
  176. }
  177. }
  178. $lines[] = $secondLine;
  179. }
  180. return $lines;
  181. }
  182. }