Local.php 3.2 KB

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