SentenceInfoController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use App\Models\PaliSentence;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. class SentenceInfoController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. //
  18. }
  19. /**
  20. * Show the form for creating a new resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function create()
  25. {
  26. //
  27. }
  28. /**
  29. * Store a newly created resource in storage.
  30. *
  31. * @param \Illuminate\Http\Request $request
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function store(Request $request)
  35. {
  36. //
  37. }
  38. private function getSentProgress(Request $request,$date=''){
  39. $channel = $request->get('channel');
  40. $from = $request->get('from');
  41. $to = $request->get('to');
  42. #默认完成度显示字符数
  43. # strlen
  44. # page
  45. # percent
  46. $view = 'strlen';
  47. if(!empty($request->get('view'))){
  48. $view =$request->get('view');
  49. }
  50. if(!empty($request->get('type'))){
  51. $view =$request->get('type');
  52. }
  53. #一页书中的字符数
  54. $pageStrLen = 2000;
  55. if(!empty($request->get('strlen'))){
  56. $pageStrLen =$request->get('strlen');
  57. }
  58. if(!empty($request->get('pagelen'))){
  59. $pageStrLen =$request->get('pagelen');
  60. }
  61. # 页数
  62. $pageNumber = 300;
  63. if(!empty($request->get('pages'))){
  64. $pageNumber =$request->get('pages');
  65. }
  66. $db = Sentence::where('channel_uid',$request->get('channel'))
  67. ->where('book_id','>=',$request->get('book'))
  68. ->where('paragraph','>=',$request->get('from'))
  69. ->where('paragraph','<=',$request->get('to'));
  70. if(!empty($date)){
  71. $db = $db->whereDate('created_at','=',$date);
  72. }
  73. $strlen =$db->sum('strlen');
  74. #计算已完成百分比
  75. $percent = 0;
  76. if(($view==='page' && !empty($request->get('pages'))) || $view==='percent' ){
  77. #计算完成的句子在巴利语句子表中的字符串长度百分比
  78. $db = Sentence::select(['paragraph','word_start'])
  79. ->where('channel_uid',$request->get('channel'))
  80. ->where('book_id','>=',$request->get('book'))
  81. ->where('paragraph','>=',$request->get('from'))
  82. ->where('paragraph','<=',$request->get('to'));
  83. if(!empty($date)){
  84. $db = $db->whereDate('created_at','=',$date);
  85. }
  86. $sentFinished = $db->get();
  87. #查询这些句子的总共等效巴利语字符数
  88. $allStrLen = PaliSentence::where('book',$request->get('book'))
  89. ->where('paragraph','>=',$request->get('from'))
  90. ->where('paragraph','<=',$request->get('to'))
  91. ->sum('length');
  92. $para_strlen = 0;
  93. foreach ($sentFinished as $sent) {
  94. # code...
  95. $para_strlen += PaliSentence::where('book',$request->get('book'))
  96. ->where('paragraph',$sent->paragraph)
  97. ->where('word_begin',$sent->word_start)
  98. ->value('length');
  99. }
  100. $percent = $para_strlen / $allStrLen;
  101. }
  102. switch ($view) {
  103. case 'page':
  104. # 输出已经完成的页数
  105. if(!empty($request->get('pages'))){
  106. #给了页码,用百分比计算
  107. $resulte = $percent * $request->get('pages');
  108. }else{
  109. #没给页码,用每页字符数计算
  110. $resulte = $strlen / $pageStrLen;
  111. }
  112. break;
  113. case 'percent':
  114. $resulte = $percent;
  115. break;
  116. case 'strlen':
  117. default:
  118. # code...
  119. $resulte = $strlen;
  120. break;
  121. }
  122. #保留小数点后两位
  123. $resulte = sprintf('%.2f',$resulte);
  124. return $resulte;
  125. }
  126. /**
  127. * 输出一张图片显示进度
  128. * Display the specified resource.
  129. *
  130. * @param \App\Models\Sentence $sentence
  131. * @return \Illuminate\Http\Response
  132. * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page
  133. */
  134. public function showprogress(Request $request)
  135. {
  136. ob_clean();
  137. ob_start();
  138. $resulte = $this->getSentProgress($request);
  139. $img = imagecreate(strlen($resulte)*10,22) or die('create image fail ');
  140. imagecolorallocate($img,255,255,255);
  141. $color = imagecolorallocate($img,0,0,0);
  142. imagestring($img,5,0,0,$resulte,$color);
  143. imagegif($img);
  144. imagedestroy($img);
  145. $content = ob_get_clean();
  146. return response($content,200,[
  147. 'Content-Type'=>'image/gif'
  148. ]);
  149. }
  150. //http://127.0.0.1:8000/api/sentence/progress/daily/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page
  151. public function showprogressdaily(Request $request)
  152. {
  153. $imgWidth = 300;
  154. $imgHeight = 100;
  155. $xAxisOffset = 16;
  156. $yAxisOffset = 16;
  157. $maxDay = 10;
  158. $maxPage = 20;
  159. $yLineSpace = 5;
  160. #默认完成度显示字符数
  161. # strlen
  162. # page
  163. # percent
  164. $view = 'strlen';
  165. if(!empty($request->get('view'))){
  166. $view =$request->get('view');
  167. }
  168. if(!empty($request->get('type'))){
  169. $view =$request->get('type');
  170. }
  171. $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
  172. $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
  173. ob_clean();
  174. ob_start();
  175. $channel = $request->get('channel');
  176. $from = $request->get('from');
  177. $to = $request->get('to');
  178. $img = imagecreate($imgWidth,$imgHeight) or die('create image fail ');
  179. #颜色定义
  180. //background color
  181. imagecolorallocate($img,255,255,255);
  182. $color = imagecolorallocate($img,0,0,0);
  183. $gray = imagecolorallocate($img,180,180,180);
  184. $dataLineColor = imagecolorallocate($img,50,50,255);
  185. //绘制坐标轴
  186. imageline($img,0,$imgHeight-$xAxisOffset,$imgWidth,$imgHeight-$xAxisOffset,$color);
  187. imageline($img,$yAxisOffset,$imgHeight,$yAxisOffset,0,$color);
  188. //绘制y轴网格线
  189. for($i=1;$i<$maxPage/$yLineSpace;$i++){
  190. $space= ($imgHeight-$xAxisOffset)/$maxPage*$yLineSpace;
  191. $y=$imgHeight-$yAxisOffset-$i*$space;
  192. imageline($img,$yAxisOffset,$y,$imgWidth,$y,$gray);
  193. imagestring($img,5,0,$y-5,$i*$yLineSpace,$color);
  194. }
  195. //绘制x轴网格线
  196. for($i=0; $i<$maxDay; $i++){
  197. $space= ($imgWidth-$yAxisOffset)/$maxDay;
  198. $x=$imgWidth-$yAxisOffset-$i*$space;
  199. $dayOffset = $maxDay-$i;
  200. $date = strtotime("today -{$i} day");
  201. $day = date("d",$date);
  202. imageline($img,$x,($imgHeight-$xAxisOffset),$x,($imgHeight-$xAxisOffset+5),$gray);
  203. imagestring($img,5,$x,($imgHeight-$xAxisOffset),$day,$color);
  204. }
  205. $last=0;
  206. for($i = 1; $i <= $maxDay; $i++){
  207. $day = strtotime("today -{$i} day");
  208. $date = date("Y-m-d",$day);
  209. $resulte = $this->getSentProgress($request,$date)*$pagePix;
  210. if($i>0){
  211. imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
  212. }
  213. $last = $resulte;
  214. }
  215. imagegif($img);
  216. imagedestroy($img);
  217. $content = ob_get_clean();
  218. return response($content,200,[
  219. 'Content-Type'=>'image/gif'
  220. ]);
  221. }
  222. /**
  223. * Display the specified resource.
  224. *
  225. * @param \App\Models\Sentence $sentence
  226. * @return \Illuminate\Http\Response
  227. */
  228. public function show(Sentence $sentence)
  229. {
  230. //
  231. }
  232. /**
  233. * Show the form for editing the specified resource.
  234. *
  235. * @param \App\Models\Sentence $sentence
  236. * @return \Illuminate\Http\Response
  237. */
  238. public function edit(Sentence $sentence)
  239. {
  240. //
  241. }
  242. /**
  243. * Update the specified resource in storage.
  244. *
  245. * @param \Illuminate\Http\Request $request
  246. * @param \App\Models\Sentence $sentence
  247. * @return \Illuminate\Http\Response
  248. */
  249. public function update(Request $request, Sentence $sentence)
  250. {
  251. //
  252. }
  253. /**
  254. * Remove the specified resource from storage.
  255. *
  256. * @param \App\Models\Sentence $sentence
  257. * @return \Illuminate\Http\Response
  258. */
  259. public function destroy(Sentence $sentence)
  260. {
  261. //
  262. }
  263. }