Qiniu.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Upload\Lib\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. $file = $this->file;
  30. if ($this->config['cover'] == 3) {
  31. $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
  32. $file = $this->file;
  33. }
  34. list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
  35. if (isset($ret['hash']) && isset($ret['key'])) {
  36. $this->file = $ret['key'];
  37. $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
  38. if (!$this->limit) {
  39. //$this->limit = getimagesize($this->output['file']);
  40. }
  41. if (!$this->size && isset($this->data['file']['size'])) {
  42. $this->size = $this->data['file']['size'];
  43. }
  44. if (isset($file) && $file) {
  45. $this->update($file['id']);
  46. } else {
  47. $this->insert();
  48. }
  49. return $this->output['url'];
  50. }
  51. }
  52. public function getName()
  53. {
  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'] = $this->file;
  77. $this->output['url'] = $this->host . $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 (!$source) {
  108. $source = $this->output['file'];
  109. }
  110. if (!$dest) {
  111. if (strpos($this->name, '_p') !== false) {
  112. $temp = explode('_p', $this->name);
  113. $this->name = $temp[0];
  114. }
  115. $file = $this->output['file'];
  116. $url = $this->output['url'];
  117. $this->name .= '_p' . $num;
  118. $dest = $this->file();
  119. $this->output['file'] = $file;
  120. $this->output['url'] = $url;
  121. }
  122. $this->img()->setType('im');
  123. $this->img()->compress($source, $num, $dest);
  124. }
  125. /**
  126. * webp
  127. *
  128. * @return mixed
  129. */
  130. public function handle_wp($num, $source = false, $dest = false)
  131. {
  132. if (!$source) {
  133. $source = $this->output['file'];
  134. }
  135. if (!$dest) {
  136. if (strpos($this->name, '_wp') !== false) {
  137. $temp = explode('_wp', $this->name);
  138. $this->name = $temp[0];
  139. }
  140. $file = $this->output['file'];
  141. $url = $this->output['url'];
  142. $this->ext = '.webp';
  143. $this->name .= '_wp' . $num;
  144. $dest = $this->file();
  145. $this->output['file'] = $file;
  146. $this->output['url'] = $url;
  147. }
  148. $this->img()->setType('im');
  149. $this->img()->webp($source, $num, $dest);
  150. }
  151. /**
  152. * thumb
  153. *
  154. * @return mixed
  155. */
  156. public function handle_t($id, $source = false, $dest = false, $path = '')
  157. {
  158. $config = Dever::load('upload/pic_thumb-one', $id);
  159. if ($config) {
  160. if (!$source) {
  161. $source = $this->output['file'];
  162. }
  163. $dest = $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  164. if (isset($config['compress']) && $config['compress'] > 0) {
  165. $dest = $dest . '/q/' . $config['compress'];
  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, false, true, $dest);
  192. }
  193. }
  194. }