Local.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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->limit && ($this->ext == '.jpg' || $this->ext == '.gif' || $this->ext == '.png')) {
  31. $this->limit = getimagesize($this->output['file']);
  32. }
  33. if (!$this->size) {
  34. $this->size = filesize($this->output['file']);
  35. }
  36. if (isset($file) && $file) {
  37. $this->update($file['id']);
  38. } else {
  39. $this->insert();
  40. }
  41. if ($this->base64) {
  42. file_put_contents($this->output['file'], $this->data['file']['tmp_name']);
  43. } elseif (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
  44. $this->output['status'] = -1;
  45. $this->output['message'] = '文件已经存在';
  46. } else {
  47. //unlink($this->data['file']['tmp_name']);
  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. }
  79. }
  80. /**
  81. * compress
  82. *
  83. * @return mixed
  84. */
  85. public function handle_p($num, $source = false, $dest = false)
  86. {
  87. if (is_array($num)) {
  88. return;
  89. }
  90. if (!$source) {
  91. $source = $this->output['file'];
  92. }
  93. if (!$dest) {
  94. if (strpos($this->name, '_p') !== false) {
  95. $temp = explode('_p', $this->name);
  96. $this->name = $temp[0];
  97. }
  98. $file = $this->output['file'];
  99. $url = $this->output['url'];
  100. $this->name .= '_p' . $num;
  101. $dest = $this->file();
  102. $this->output['file'] = $file;
  103. $this->output['url'] = $url;
  104. }
  105. $this->img()->setType('im');
  106. $this->img()->compress($source, $num, $dest);
  107. return $dest;
  108. }
  109. /**
  110. * webp
  111. *
  112. * @return mixed
  113. */
  114. public function handle_wp($num, $source = false, $dest = false)
  115. {
  116. if (is_array($num)) {
  117. return;
  118. }
  119. if (!$source) {
  120. $source = $this->output['file'];
  121. }
  122. if (!$dest) {
  123. if (strpos($this->name, '_wp') !== false) {
  124. $temp = explode('_wp', $this->name);
  125. $this->name = $temp[0];
  126. }
  127. $file = $this->output['file'];
  128. $url = $this->output['url'];
  129. $this->ext = '.webp';
  130. $this->name .= '_wp' . $num;
  131. $dest = $this->file();
  132. $this->output['file'] = $file;
  133. $this->output['url'] = $url;
  134. }
  135. $this->img()->setType('im');
  136. $this->img()->webp($source, $num, $dest);
  137. return $dest;
  138. }
  139. /**
  140. * thumb
  141. *
  142. * @return mixed
  143. */
  144. public function handle_t($id, $source = false, $dest = false)
  145. {
  146. $config = Dever::load('upload/pic_thumb-one', $id);
  147. if ($config) {
  148. if (!$source) {
  149. $source = $this->output['file'];
  150. }
  151. if (!$dest) {
  152. if (strpos($this->name, '_t') !== false) {
  153. $temp = explode('_t', $this->name);
  154. $this->name = $temp[0];
  155. }
  156. $this->name .= '_t' . $id;
  157. $dest = $this->file();
  158. }
  159. $size = $config['width'] . '_' . $config['height'] . '_2';
  160. $this->img()->thumb($source, $size, true, $dest);
  161. # 同时进行压缩
  162. if (isset($config['compress']) && $config['compress'] > 0) {
  163. $this->img()->setType('im');
  164. $this->img()->compress($dest, $config['compress'], $dest);
  165. }
  166. }
  167. return $dest;
  168. }
  169. /**
  170. * crop
  171. *
  172. * @return mixed
  173. */
  174. public function handle_c($id, $source = false, $dest = false)
  175. {
  176. $config = Dever::load('upload/pic_crop-one', $id);
  177. if ($config) {
  178. if (!$source) {
  179. $source = $this->output['file'];
  180. }
  181. if (!$dest) {
  182. if (strpos($this->name, '_c') !== false) {
  183. $temp = explode('_c', $this->name);
  184. $this->name = $temp[0];
  185. }
  186. $this->name .= '_c' . $id;
  187. $dest = $this->file();
  188. }
  189. $size = $config['width'] . '_' . $config['height'] . '_2';
  190. $this->img()->crop($source, $size, false, true, $dest);
  191. }
  192. return $dest;
  193. }
  194. }