Local.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php namespace Upload\Lib\Tool;
  2. use Dever;
  3. use Image\Lib\Tool;
  4. class Local
  5. {
  6. private $config;
  7. public function init($config)
  8. {
  9. $this->config = $config;
  10. }
  11. public function upload($type, $source, $dest, $chunk, $upload)
  12. {
  13. $file = Dever::file('upload/' . $dest);
  14. if ($chunk) {
  15. $chunk['file'] = $file;
  16. $file = Dever::file('upload/' . $dest . '_chunk/' . $chunk['cur'] . '.blob');
  17. }
  18. if ($type == 1) {
  19. if (!copy($source, $file)) {
  20. Dever::error('上传失败');
  21. }
  22. } elseif ($type == 2) {
  23. file_put_contents($file, $source);
  24. } else {
  25. Dever::error('上传类型无效');
  26. }
  27. if (!is_file($file)) {
  28. Dever::error('上传失败');
  29. }
  30. if ($chunk) {
  31. $dir = dirname($file);
  32. if (is_dir($dir)) {
  33. $files = scandir($dir);
  34. $num = count($files) - 2;
  35. if ($num == $chunk['total']) {
  36. $out = fopen($chunk['file'], 'ab');
  37. natsort($files);
  38. foreach ($files as $v) {
  39. if (pathinfo($v, PATHINFO_EXTENSION) === 'blob') {
  40. $temp = $dir . '/' . $v;
  41. $in = fopen($temp, 'rb');
  42. stream_copy_to_stream($in, $out); // 边读边写
  43. fclose($in);
  44. @unlink($temp); // 删除分片
  45. }
  46. }
  47. fclose($out);
  48. @rmdir($dir);
  49. $file = $chunk['file'];
  50. } else {
  51. return '';
  52. }
  53. } else {
  54. return '';
  55. }
  56. }
  57. $after = $upload->after();
  58. if ($after) {
  59. $tool = Dever::load(Tool::class)->get()->cover(true);
  60. foreach ($after as $k => $v) {
  61. if (isset($v['table'])) {
  62. $method = 'handle_' . $v['table'];
  63. $file = $this->$method($tool, $file, $v['param']);
  64. }
  65. }
  66. }
  67. return $this->url($file);
  68. }
  69. private function url($file)
  70. {
  71. $dest = str_replace(Dever::data() . DEVER_PROJECT . '/', '', $file);
  72. if (isset($this->config['host']) && $this->config['host']) {
  73. return $this->config['host'] . $dest;
  74. }
  75. return Dever::host() . 'data/' . DEVER_PROJECT . '/' . $dest;
  76. }
  77. public function handle($id, $file, $type = 'thumb')
  78. {
  79. $param = Dever::db('image/' . $type)->find($id);
  80. $tool = Dever::load(Tool::class)->get()->cover(true);
  81. $file = $this->$method($tool, $file, $param);
  82. return $this->url($file);
  83. }
  84. private function handle_thumb($tool, $file, $param)
  85. {
  86. $tool->source($file);
  87. $result = $tool->thumb($param['width'] . '_' . $param['height']);
  88. if ($result) {
  89. return $result[0];
  90. }
  91. return $file;
  92. }
  93. private function handle_crop($tool, $file, $param)
  94. {
  95. $tool->source($file);
  96. $result = $tool->crop($param['width'] . '_' . $param['height'], $param['position']);
  97. if ($result) {
  98. return $result[0];
  99. }
  100. return $file;
  101. }
  102. }