PublicUploadService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\service\upload;
  4. use app\common\library\Image;
  5. use app\common\model\Attachment;
  6. use app\common\service\UploadService;
  7. use think\facade\Filesystem;
  8. use app\common\library\Imgcompress;
  9. class PublicUploadService extends UploadService{
  10. protected $config=[];
  11. protected $disks='local_public';
  12. private $imagetype=null;
  13. private $imagesize=null;
  14. private $imagewidth=null;
  15. private $imageheight=null;
  16. protected function putFile():array
  17. {
  18. $url=Filesystem::disk($this->disks)->putFile('',$this->file,function (){
  19. $fileName = date('Ymd'). DIRECTORY_SEPARATOR .md5(microtime(true) . rand(10000,99999));
  20. return $fileName;
  21. });
  22. if($this->isImage()) {
  23. $filepath = $this->file->getRealPath();
  24. $imageinfo = getimagesize($filepath);
  25. $this->imagesize = filesize($filepath);
  26. $this->imagewidth = $imageinfo[0];
  27. $this->imageheight = $imageinfo[1];
  28. $this->imagetype = $imageinfo['mime'];
  29. }
  30. $fullurl=request()->domain().'/upload/'.$url;
  31. return ['public/upload/'.$url,$fullurl];
  32. }
  33. /*
  34. * 生成缩略图
  35. * @param string $url 原图片的物理路径
  36. * @param string $fullurl 原图片的网络路径
  37. */
  38. protected function thumb(string $url,string $fullurl): string
  39. {
  40. if($this->imagewidth<=200 && $this->imageheight<=200){
  41. return $fullurl;
  42. }
  43. //计算缩放比例,保证缩略图的宽高都不超过200px
  44. $percent=min(200/$this->imagewidth,200/$this->imageheight);
  45. $filepath=root_path().$url;
  46. $ext=$this->file->extension();
  47. $writepath=substr($filepath,0,strrpos($filepath,'.'.$ext)).'_thumb.'.$ext;
  48. (new Imgcompress($filepath,$percent))->compressImg($writepath);
  49. $thumburl=substr($fullurl,0,strrpos($fullurl,'.'.$ext)).'_thumb.'.$ext;
  50. return $thumburl;
  51. }
  52. protected function compress(string $url,string $fullurl)
  53. {
  54. $filepath=root_path().$url;
  55. $percent=1;
  56. if($this->imagesize>1024*1024*5){
  57. $percent = 0.4;
  58. }else if($this->imagesize>1024*1024*4){
  59. $percent = 0.5;
  60. }else if($this->imagesize>1024*1024*3){
  61. $percent = 0.6;
  62. }else if($this->imagesize>1024*1024*2){
  63. $percent = 0.7;
  64. }else if($this->imagesize>1024*1024*1){
  65. $percent = 0.8;
  66. }else if($this->imagesize>1024*1024*0.8){
  67. $percent = 0.9;
  68. }
  69. if($percent!=1){
  70. $this->imagewidth=intval($this->imagewidth*$percent);
  71. $this->imageheight=intval($this->imageheight*$percent);
  72. (new Imgcompress($filepath,$percent))->compressImg($filepath);
  73. $this->imagesize=filesize($filepath);
  74. }
  75. }
  76. protected function imageFileinfo($url,$fullurl): array
  77. {
  78. return [$this->imagesize,$this->imagetype,$this->imagewidth,$this->imageheight];
  79. }
  80. protected function watermark(string $url,string $fullurl)
  81. {
  82. }
  83. public static function deleteFile(Attachment $attachment)
  84. {
  85. $filepath=root_path().$attachment->url;
  86. if(file_exists($filepath)){
  87. unlink($filepath);
  88. }
  89. if($attachment->is_image){
  90. $exten=strtolower(pathinfo($filepath,PATHINFO_EXTENSION));
  91. $thumbpath=str_replace('.'.$exten,'_thumb.'.$exten,$filepath);
  92. if(file_exists($thumbpath)){
  93. unlink($thumbpath);
  94. }
  95. }
  96. }
  97. }