SentenceInfoController.php 8.5 KB

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