ProgressImgController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. class ProgressImgController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. //
  16. }
  17. /**
  18. * Store a newly created resource in storage.
  19. *
  20. * @param \Illuminate\Http\Request $request
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function store(Request $request)
  24. {
  25. //
  26. }
  27. /**
  28. * Display the specified resource.
  29. *
  30. * @param string $id
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function show($id)
  34. {
  35. //
  36. return response()->stream(function () use ($id) {
  37. $key = str_replace('-','/',$id);
  38. $svg = Cache::get('svg/'.$key, function () use ($key) {
  39. $viewHeight = 60;
  40. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 300 60'>";
  41. $data = Cache::get($key);
  42. if(is_array($data)){
  43. $point = [];
  44. foreach ($data as $key => $value) {
  45. $point[] = ($key*10) . ',' . $viewHeight-($value/20)-3;
  46. }
  47. $svg .= '<polyline points="'. implode(' ',$point) . '"';
  48. $svg .= ' style="fill:none;stroke:green;stroke-width:3" /></svg>';
  49. }else{
  50. $svg .= '<polyline points="0,0 1,0" /></svg>';
  51. }
  52. return $svg;
  53. } , env('CACHE_EXPIRE',3600*24) );
  54. echo $svg;
  55. }, 200, ['Content-Type' => 'image/svg+xml']);
  56. /*
  57. ————————————————
  58. 原文作者:Summer
  59. 转自链接:https://learnku.com/laravel/wikis/25600
  60. 版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。
  61. */
  62. }
  63. /**
  64. * Update the specified resource in storage.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param int $id
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function update(Request $request, $id)
  71. {
  72. //
  73. }
  74. /**
  75. * Remove the specified resource from storage.
  76. *
  77. * @param int $id
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function destroy($id)
  81. {
  82. //
  83. }
  84. }