SentenceInfoController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\DB;
  7. class SentenceInfoController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index()
  15. {
  16. //
  17. }
  18. /**
  19. * Show the form for creating a new resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function create()
  24. {
  25. //
  26. }
  27. /**
  28. * Store a newly created resource in storage.
  29. *
  30. * @param \Illuminate\Http\Request $request
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function store(Request $request)
  34. {
  35. //
  36. }
  37. /**
  38. * Display the specified resource.
  39. *
  40. * @param \App\Models\Sentence $sentence
  41. * @return \Illuminate\Http\Response
  42. * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&type=page
  43. */
  44. public function showprogress(Request $request)
  45. {
  46. ob_clean();
  47. ob_start();
  48. $channel = $request->get('channel');
  49. $from = $request->get('from');
  50. $to = $request->get('to');
  51. $strlen = Sentence::where('channel_uid',$request->get('channel'))
  52. ->where('book_id','>=',$request->get('book'))
  53. ->where('paragraph','>=',$request->get('from'))
  54. ->where('paragraph','<=',$request->get('to'))
  55. ->whereDate('created_at','2020-11-07')
  56. ->sum('strlen');
  57. if($request->get('type')==='page'){
  58. if(empty($request->get('strlen'))){
  59. $pageStrLen = 500;
  60. }else{
  61. $pageStrLen = $request->get('strlen');
  62. }
  63. $resulte = $strlen / $pageStrLen;
  64. }else{
  65. $resulte = $strlen;
  66. }
  67. $img = imagecreate(strlen($resulte)*10,22) or die('create image fail ');
  68. imagecolorallocate($img,255,255,255);
  69. $color = imagecolorallocate($img,0,0,0);
  70. imagestring($img,5,0,0,$resulte,$color);
  71. imagegif($img);
  72. imagedestroy($img);
  73. $content = ob_get_clean();
  74. return response($content,200,[
  75. 'Content-Type'=>'image/gif'
  76. ]);
  77. }
  78. //http://127.0.0.1:8000/api/sentence/progress/daily/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&type=page
  79. public function showprogressdaily(Request $request)
  80. {
  81. $imgWidth = 300;
  82. $imgHeight = 100;
  83. $xAxisOffset = 16;
  84. $yAxisOffset = 16;
  85. $maxDay = 10;
  86. $maxPage = 20;
  87. $yLineSpace = 5;
  88. $pageStrLen = 500;
  89. $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
  90. $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
  91. ob_clean();
  92. ob_start();
  93. $channel = $request->get('channel');
  94. $from = $request->get('from');
  95. $to = $request->get('to');
  96. $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
  97. #颜色定义
  98. //background color
  99. imagecolorallocate($img,255,255,255);
  100. $color = imagecolorallocate($img,0,0,0);
  101. $gray = imagecolorallocate($img,180,180,180);
  102. $dataLineColor = imagecolorallocate($img,50,50,255);
  103. //绘制坐标轴
  104. imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
  105. imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
  106. //绘制y轴网格线
  107. for($i=1;$i<$maxPage/$yLineSpace;$i++){
  108. $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
  109. $y=$imgHeight-$yAxisOffset-$i*$space;
  110. imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
  111. imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
  112. }
  113. //绘制x轴网格线
  114. for($i=0; $i<$maxDay; $i++){
  115. $space= ($imgWidth-$yAxisOffset)/$maxDay;
  116. $x=$imgWidth-$yAxisOffset-$i*$space;
  117. $dayOffset = $maxDay-$i;
  118. $date = strtotime("today -{$i} day");
  119. $day = date("d",$date);
  120. imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
  121. imagestring($img,5,$x,($imgHeight-$xAxisOffset),$day,$color);
  122. }
  123. $last=0;
  124. for($i = 0; $i < $maxDay; $i++){
  125. $day = strtotime("today -{$i} day");
  126. $date = date("Y-m-d",$day);
  127. $strlen = Sentence::where('channel_uid',$request->get('channel'))
  128. ->where('book_id','>=',$request->get('book'))
  129. ->where('paragraph','>=',$request->get('from'))
  130. ->where('paragraph','<=',$request->get('to'))
  131. ->whereDate('created_at','=',$date)
  132. ->sum('strlen');
  133. if($request->get('type')==='page'){
  134. if(!empty($request->get('strlen'))){
  135. $pageStrLen = $request->get('strlen');
  136. }
  137. $resulte = $strlen / $pageStrLen * $pagePix;
  138. }else{
  139. $resulte = $strlen / 50;
  140. }
  141. if($i>0){
  142. imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
  143. }
  144. $last = $resulte;
  145. }
  146. imagegif($img);
  147. imagedestroy($img);
  148. $content = ob_get_clean();
  149. return response($content,200,[
  150. 'Content-Type'=>'image/gif'
  151. ]);
  152. }
  153. /**
  154. * Display the specified resource.
  155. *
  156. * @param \App\Models\Sentence $sentence
  157. * @return \Illuminate\Http\Response
  158. */
  159. public function show(Sentence $sentence)
  160. {
  161. //
  162. }
  163. /**
  164. * Show the form for editing the specified resource.
  165. *
  166. * @param \App\Models\Sentence $sentence
  167. * @return \Illuminate\Http\Response
  168. */
  169. public function edit(Sentence $sentence)
  170. {
  171. //
  172. }
  173. /**
  174. * Update the specified resource in storage.
  175. *
  176. * @param \Illuminate\Http\Request $request
  177. * @param \App\Models\Sentence $sentence
  178. * @return \Illuminate\Http\Response
  179. */
  180. public function update(Request $request, Sentence $sentence)
  181. {
  182. //
  183. }
  184. /**
  185. * Remove the specified resource from storage.
  186. *
  187. * @param \App\Models\Sentence $sentence
  188. * @return \Illuminate\Http\Response
  189. */
  190. public function destroy(Sentence $sentence)
  191. {
  192. //
  193. }
  194. }