Local.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Upload\Src\Store;
  3. use Dever;
  4. use Dever\Img\Handle;
  5. class Local extends Core implements Config
  6. {
  7. /**
  8. * 创建文件和目录
  9. *
  10. * @return mixed
  11. */
  12. public function save()
  13. {
  14. $this->root();
  15. $this->path()->name($this->data['file']['name'])->ext()->file();
  16. # 不覆盖
  17. if (is_file($this->output['file']) && $this->config['cover'] == 2) {
  18. $this->output['status'] = -1;
  19. $this->output['message'] = '文件已经存在';
  20. } else {
  21. # 不覆盖 生成新文件
  22. if (is_file($this->output['file']) && $this->config['cover'] == 3) {
  23. // . microtime() . rand(0,1000)
  24. $this->name($this->output['file'])->file();
  25. } else {
  26. $file = Dever::load('upload/file-name', array('where_name' => $this->name));
  27. }
  28. if (!$this->limit) {
  29. //$this->limit = getimagesize($this->output['file']);
  30. }
  31. if (!$this->size) {
  32. $this->size = filesize($this->output['file']);
  33. }
  34. if (isset($file) && $file) {
  35. $this->update($file['id']);
  36. } else {
  37. $this->insert();
  38. }
  39. if (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
  40. $this->output['status'] = -1;
  41. $this->output['message'] = '文件已经存在';
  42. } else {
  43. unlink($this->data['file']['tmp_name']);
  44. }
  45. return $this->output['url'];
  46. }
  47. }
  48. private function path()
  49. {
  50. $date = explode('-', date("Y-m-d"));
  51. $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  52. return $this;
  53. }
  54. private function name($name)
  55. {
  56. $this->name = md5($name);
  57. return $this;
  58. }
  59. private function ext()
  60. {
  61. $this->ext = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
  62. return $this;
  63. }
  64. private function file()
  65. {
  66. $this->file = $this->path . $this->name . $this->ext;
  67. $this->output['file'] = is_file($this->base . $this->file) ? $this->base . $this->file : Dever::path($this->base, $this->file);
  68. $this->output['url'] = Dever::config('host')->uploadRes . $this->file;
  69. return $this->output['file'];
  70. }
  71. private function handle()
  72. {
  73. $this->handle = isset($this->handle) ? $this->handle : new Handle();
  74. return $this->handle;
  75. }
  76. /**
  77. * water
  78. *
  79. * @return mixed
  80. */
  81. public function handle_w($id)
  82. {
  83. $config = Dever::load('upload/pic_water-one', $id);
  84. if ($config) {
  85. $this->name .= '_w' . $id;
  86. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  87. $this->handle()->mark($this->output['file'], $water, true, $this->file());
  88. }
  89. }
  90. /**
  91. * thumb
  92. *
  93. * @return mixed
  94. */
  95. public function handle_t($id)
  96. {
  97. $config = Dever::load('upload/pic_thumb-one', $id);
  98. if ($config) {
  99. if (strpos($this->name, '_t') !== false) {
  100. $temp = explode('_t', $this->name);
  101. $this->name = $temp[0];
  102. }
  103. $this->name .= '_t' . $id;
  104. $size = $config['width'] . '_' . $config['height'] . '_2';
  105. $this->handle()->thumb($this->output['file'], $size, true, $this->file());
  106. }
  107. }
  108. /**
  109. * crop
  110. *
  111. * @return mixed
  112. */
  113. public function handle_c($id)
  114. {
  115. $config = Dever::load('upload/pic_crop-one', $id);
  116. if ($config) {
  117. $this->name .= '_c' . $id;
  118. $size = $config['width'] . '_' . $config['height'] . '_2';
  119. //false position
  120. $this->handle()->crop($this->output['file'], $size, false, true, $this->file());
  121. }
  122. }
  123. }