Local.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace Upload\Lib\Store;
  3. use Dever;
  4. //use Upload\Lib\Lib\Img;
  5. use Dever\Support\Img;
  6. class Local extends Core implements Config
  7. {
  8. /**
  9. * 创建文件和目录
  10. *
  11. * @return mixed
  12. */
  13. public function save()
  14. {
  15. $this->root();
  16. $this->path()->name($this->data['file']['name'])->ext()->file();
  17. # 不覆盖
  18. if (is_file($this->output['file']) && $this->config['cover'] == 2) {
  19. $this->output['status'] = -1;
  20. $this->output['message'] = '文件已经存在';
  21. } else {
  22. # 不覆盖 生成新文件
  23. if (is_file($this->output['file']) && $this->config['cover'] == 3) {
  24. // . microtime() . rand(0,1000)
  25. $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
  26. } else {
  27. $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
  28. }
  29. if (!$this->limit) {
  30. //$this->limit = getimagesize($this->output['file']);
  31. }
  32. if (!$this->size) {
  33. $this->size = filesize($this->output['file']);
  34. }
  35. if (isset($file) && $file) {
  36. $this->update($file['id']);
  37. } else {
  38. $this->insert();
  39. }
  40. if (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
  41. $this->output['status'] = -1;
  42. $this->output['message'] = '文件已经存在';
  43. } else {
  44. //unlink($this->data['file']['tmp_name']);
  45. }
  46. @chmod($this->output['file'], 0755);
  47. @system('chmod -R 777 ' . $this->output['file']);
  48. return $this->output['url'];
  49. }
  50. }
  51. public function getName()
  52. {
  53. print_r($this->data['file']['name']);die;
  54. $this->path()->name($this->data['file']['name'])->ext()->file();
  55. return $this->file;
  56. }
  57. private function path()
  58. {
  59. $date = explode('-', date("Y-m-d"));
  60. $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  61. return $this;
  62. }
  63. private function name($name)
  64. {
  65. $this->name = md5($name);
  66. return $this;
  67. }
  68. private function ext()
  69. {
  70. $this->ext = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
  71. return $this;
  72. }
  73. private function file()
  74. {
  75. $this->file = $this->path . $this->name . $this->ext;
  76. $this->output['file'] = is_file($this->base . $this->file) ? $this->base . $this->file : Dever::path($this->base, $this->file);
  77. $this->output['url'] = Dever::config('host')->uploadRes . $this->file;
  78. return $this->output['file'];
  79. }
  80. private function img()
  81. {
  82. $this->img = isset($this->img) ? $this->img : new Img();
  83. $this->img->setType('im');
  84. return $this->img;
  85. }
  86. /**
  87. * water
  88. *
  89. * @return mixed
  90. */
  91. public function handle_w($id)
  92. {
  93. $config = Dever::load('upload/pic_water-one', $id);
  94. if ($config) {
  95. $this->name .= '_w' . $id;
  96. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  97. $this->img()->mark($this->output['file'], $water, true, $this->file());
  98. }
  99. }
  100. /**
  101. * compress
  102. *
  103. * @return mixed
  104. */
  105. public function handle_p($num, $source = false, $dest = false)
  106. {
  107. if (is_array($num)) {
  108. return;
  109. }
  110. if (!$source) {
  111. $source = $this->output['file'];
  112. }
  113. if (!$dest) {
  114. if (strpos($this->name, '_p') !== false) {
  115. $temp = explode('_p', $this->name);
  116. $this->name = $temp[0];
  117. }
  118. $file = $this->output['file'];
  119. $url = $this->output['url'];
  120. $this->name .= '_p' . $num;
  121. $dest = $this->file();
  122. $this->output['file'] = $file;
  123. $this->output['url'] = $url;
  124. }
  125. $this->img()->setType('im');
  126. $this->img()->compress($source, $num, $dest);
  127. return $dest;
  128. }
  129. /**
  130. * webp
  131. *
  132. * @return mixed
  133. */
  134. public function handle_wp($num, $source = false, $dest = false)
  135. {
  136. if (is_array($num)) {
  137. return;
  138. }
  139. if (!$source) {
  140. $source = $this->output['file'];
  141. }
  142. if (!$dest) {
  143. if (strpos($this->name, '_wp') !== false) {
  144. $temp = explode('_wp', $this->name);
  145. $this->name = $temp[0];
  146. }
  147. $file = $this->output['file'];
  148. $url = $this->output['url'];
  149. $this->ext = '.webp';
  150. $this->name .= '_wp' . $num;
  151. $dest = $this->file();
  152. $this->output['file'] = $file;
  153. $this->output['url'] = $url;
  154. }
  155. $this->img()->setType('im');
  156. $this->img()->webp($source, $num, $dest);
  157. return $dest;
  158. }
  159. /**
  160. * thumb
  161. *
  162. * @return mixed
  163. */
  164. public function handle_t($id, $source = false, $dest = false)
  165. {
  166. $config = Dever::load('upload/pic_thumb-one', $id);
  167. if ($config) {
  168. if (!$source) {
  169. $source = $this->output['file'];
  170. }
  171. if (!$dest) {
  172. if (strpos($this->name, '_t') !== false) {
  173. $temp = explode('_t', $this->name);
  174. $this->name = $temp[0];
  175. }
  176. $this->name .= '_t' . $id;
  177. $dest = $this->file();
  178. }
  179. $size = $config['width'] . '_' . $config['height'] . '_2';
  180. $this->img()->thumb($source, $size, true, $dest);
  181. # 同时进行压缩
  182. if (isset($config['compress']) && $config['compress'] > 0) {
  183. $this->img()->setType('im');
  184. $this->img()->compress($dest, $config['compress'], $dest);
  185. }
  186. }
  187. return $dest;
  188. }
  189. /**
  190. * crop
  191. *
  192. * @return mixed
  193. */
  194. public function handle_c($id, $source = false, $dest = false)
  195. {
  196. $config = Dever::load('upload/pic_crop-one', $id);
  197. if ($config) {
  198. if (!$source) {
  199. $source = $this->output['file'];
  200. }
  201. if (!$dest) {
  202. if (strpos($this->name, '_c') !== false) {
  203. $temp = explode('_c', $this->name);
  204. $this->name = $temp[0];
  205. }
  206. $this->name .= '_c' . $id;
  207. $dest = $this->file();
  208. }
  209. $size = $config['width'] . '_' . $config['height'] . '_2';
  210. $this->img()->crop($source, $size, false, true, $dest);
  211. }
  212. return $dest;
  213. }
  214. }