Qiniu.php 6.5 KB

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