SentenceInfoController.php 6.3 KB

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