Local.php 5.4 KB

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