PrivateUploadService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\service\upload;
  4. use app\common\library\Imgcompress;
  5. use app\common\model\Attachment;
  6. use app\common\service\UploadService;
  7. use think\facade\Filesystem;
  8. class PrivateUploadService extends UploadService{
  9. protected $config=[];
  10. protected $disks='local_private';
  11. private $imagetype=null;
  12. private $imagesize=null;
  13. private $imagewidth=null;
  14. private $imageheight=null;
  15. protected function putFile():array
  16. {
  17. $url=Filesystem::disk($this->disks)->putFile(date('Ymd'),$this->file,'md5');
  18. if($this->isImage()) {
  19. $filepath = $this->file->getRealPath();
  20. $imageinfo = getimagesize($filepath);
  21. $this->imagesize = filesize($filepath);
  22. $this->imagewidth = $imageinfo[0];
  23. $this->imageheight = $imageinfo[1];
  24. $this->imagetype = $imageinfo['mime'];
  25. }
  26. $alismodel=get_module_alis('admin');
  27. $sha1=$this->file->sha1();
  28. $fullurl=request()->domain().'/'.$alismodel.'/ajax/readfile?sha1='.$sha1;
  29. return ['storage/'.$url,$fullurl];
  30. }
  31. public static function deleteFile(Attachment $attachment)
  32. {
  33. $filepath=root_path().$attachment->url;
  34. if(file_exists($filepath)){
  35. unlink($filepath);
  36. }
  37. }
  38. protected function imageFileinfo($url,$fullurl): array
  39. {
  40. return [$this->imagesize,$this->imagetype,$this->imagewidth,$this->imageheight];
  41. }
  42. protected function thumb(string $url,string $fullurl): string
  43. {
  44. return request()->domain().'/assets/img/fileicon/image.png';
  45. }
  46. protected function compress(string $url,string $fullurl)
  47. {
  48. $filepath=root_path().$url;
  49. $percent=1;
  50. if($this->imagesize>1024*1024*5){
  51. $percent = 0.4;
  52. }else if($this->imagesize>1024*1024*4){
  53. $percent = 0.5;
  54. }else if($this->imagesize>1024*1024*3){
  55. $percent = 0.6;
  56. }else if($this->imagesize>1024*1024*2){
  57. $percent = 0.7;
  58. }else if($this->imagesize>1024*1024*1){
  59. $percent = 0.8;
  60. }else if($this->imagesize>1024*1024*0.8){
  61. $percent = 0.9;
  62. }
  63. if($percent!=1){
  64. $this->imagewidth=intval($this->imagewidth*$percent);
  65. $this->imageheight=intval($this->imageheight*$percent);
  66. (new Imgcompress($filepath,$percent))->compressImg($filepath);
  67. $this->imagesize=filesize($filepath);
  68. }
  69. }
  70. protected function watermark(string $url,string $fullurl)
  71. {
  72. }
  73. }