Img.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Module\File;
  3. use App\Module\File\ModelsFileImg;
  4. use Illuminate\Support\Facades\Storage;
  5. class Img
  6. {
  7. static public function getModel($path)
  8. {
  9. $model = FileImg::where('path', $path)->first();
  10. return $model;
  11. }
  12. /**
  13. * 下载
  14. * @param FileImg $fileImg
  15. * @return resource|null
  16. */
  17. static public function download(FileImg $fileImg)
  18. {
  19. return Storage::disk($fileImg->storage_disk)->download($fileImg->path);;
  20. }
  21. static public function getPicUrl4Id($id)
  22. {
  23. $model = FileImg::find($id);
  24. if($model){
  25. return self::getPicUrl($model);
  26. }
  27. return '';
  28. }
  29. /**
  30. * 获取图片的可用访问地址
  31. *
  32. * @param FileImg $fileImg
  33. * @return string
  34. */
  35. static public function getPicUrl(FileImg $fileImg , $private= false)
  36. {
  37. if($fileImg->private ==1){
  38. // 私有的地址
  39. if($private){
  40. return Storage::disk('local')->url($fileImg->path);;
  41. }
  42. return '';
  43. }
  44. $src = Storage::disk($fileImg->storage_disk)->url($fileImg->path);
  45. return $src;
  46. }
  47. /**
  48. * 获取后台上传的图片的 访问地址
  49. * @param $path
  50. * @return string
  51. */
  52. static public function getAdminPicUrl($path)
  53. {
  54. $disk = config('admin.upload.disk');
  55. if (config("filesystems.disks.{$disk}")) {
  56. $src = Storage::disk($disk)->url($path);
  57. }
  58. return $src;
  59. }
  60. static public function getAdminPicSrc($path)
  61. {
  62. $disk = config('admin.upload.disk');
  63. if (config("filesystems.disks.{$disk}")) {
  64. $src = Storage::disk($disk)->url($path);
  65. }
  66. $ss = parse_url($src);
  67. // dump($ss);
  68. return $ss['path'];
  69. }
  70. static public function getReal4path($path)
  71. {
  72. \App\Module\System\Services\File::getInfo();
  73. }
  74. static public function saveAdminPic($path, $data)
  75. {
  76. $disk = config('admin.upload.disk');
  77. if (config("filesystems.disks.{$disk}")) {
  78. $src = Storage::disk($disk)->put($path, $data);
  79. }
  80. return $src;
  81. }
  82. static public function hasAdminPic($path)
  83. {
  84. $disk = config('admin.upload.disk');
  85. if (config("filesystems.disks.{$disk}")) {
  86. $src = Storage::disk($disk)->exists($path);
  87. }
  88. return $src;
  89. }
  90. /**
  91. * 图片处理为可访问的地址
  92. *
  93. * @param $arr
  94. * @return array
  95. */
  96. static public function imgArr2imgArrurl($arr)
  97. {
  98. $res = [];
  99. foreach ($arr as $img){
  100. $res[] = self::img2imgurl($img);
  101. }
  102. return $res;
  103. }
  104. static public function img2imgurl($img)
  105. {
  106. if(substr($img,0,8) == '/storage'){
  107. return self::getAdminPicUrl(substr($img,8));
  108. }
  109. return $img;
  110. }
  111. }