SentenceInfoController.php 9.1 KB

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