Qiniu.php 6.5 KB

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