Qiniu.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. $file = $this->file;
  30. if ($this->config['cover'] == 3) {
  31. $file = false;
  32. }
  33. list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
  34. if (isset($ret['hash']) && isset($ret['key'])) {
  35. $this->file = $ret['key'];
  36. $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
  37. if (!$this->limit) {
  38. //$this->limit = getimagesize($this->output['file']);
  39. }
  40. if (!$this->size && isset($this->data['file']['size'])) {
  41. $this->size = $this->data['file']['size'];
  42. }
  43. if (isset($file) && $file) {
  44. $this->update($file['id']);
  45. } else {
  46. $this->insert();
  47. }
  48. return $this->output['url'];
  49. }
  50. }
  51. public function getName()
  52. {
  53. $this->path()->name($this->data['file']['name'])->ext()->file();
  54. return $this->file;
  55. }
  56. private function path()
  57. {
  58. $date = explode('-', date("Y-m-d"));
  59. $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  60. return $this;
  61. }
  62. private function name($name)
  63. {
  64. $this->name = md5($name);
  65. return $this;
  66. }
  67. private function ext()
  68. {
  69. $this->ext = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
  70. return $this;
  71. }
  72. private function file()
  73. {
  74. $this->file = $this->path . $this->name . $this->ext;
  75. $this->output['file'] = $this->file;
  76. $this->output['url'] = $this->host . $this->file;
  77. return $this->output['file'];
  78. }
  79. private function img()
  80. {
  81. $this->img = isset($this->img) ? $this->img : new Img();
  82. $this->img->setType('im');
  83. return $this->img;
  84. }
  85. /**
  86. * water
  87. *
  88. * @return mixed
  89. */
  90. public function handle_w($id)
  91. {
  92. $config = Dever::load('upload/pic_water-one', $id);
  93. if ($config) {
  94. $this->name .= '_w' . $id;
  95. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  96. $this->img()->mark($this->output['file'], $water, true, $this->file());
  97. }
  98. }
  99. /**
  100. * compress
  101. *
  102. * @return mixed
  103. */
  104. public function handle_p($num, $source = false, $dest = false)
  105. {
  106. if (!$source) {
  107. $source = $this->output['file'];
  108. }
  109. if (!$dest) {
  110. if (strpos($this->name, '_p') !== false) {
  111. $temp = explode('_p', $this->name);
  112. $this->name = $temp[0];
  113. }
  114. $file = $this->output['file'];
  115. $url = $this->output['url'];
  116. $this->name .= '_p' . $num;
  117. $dest = $this->file();
  118. $this->output['file'] = $file;
  119. $this->output['url'] = $url;
  120. }
  121. $this->img()->setType('im');
  122. $this->img()->compress($source, $num, $dest);
  123. }
  124. /**
  125. * webp
  126. *
  127. * @return mixed
  128. */
  129. public function handle_wp($num, $source = false, $dest = false)
  130. {
  131. if (!$source) {
  132. $source = $this->output['file'];
  133. }
  134. if (!$dest) {
  135. if (strpos($this->name, '_wp') !== false) {
  136. $temp = explode('_wp', $this->name);
  137. $this->name = $temp[0];
  138. }
  139. $file = $this->output['file'];
  140. $url = $this->output['url'];
  141. $this->ext = '.webp';
  142. $this->name .= '_wp' . $num;
  143. $dest = $this->file();
  144. $this->output['file'] = $file;
  145. $this->output['url'] = $url;
  146. }
  147. $this->img()->setType('im');
  148. $this->img()->webp($source, $num, $dest);
  149. }
  150. /**
  151. * thumb
  152. *
  153. * @return mixed
  154. */
  155. public function handle_t($id, $source = false, $dest = false)
  156. {
  157. $config = Dever::load('upload/pic_thumb-one', $id);
  158. if ($config) {
  159. if (!$source) {
  160. $source = $this->output['file'];
  161. }
  162. if (!$dest) {
  163. if (strpos($this->name, '_t') !== false) {
  164. $temp = explode('_t', $this->name);
  165. $this->name = $temp[0];
  166. }
  167. $this->name .= '_t' . $id;
  168. $dest = $this->file();
  169. }
  170. $dest = $source . '?imageView2/1/w/'.$config['width'].'/h/'.$config['height'].'/q/75';
  171. if (isset($config['compress']) && $config['compress'] > 0) {
  172. $dest = $dest . '/q/' . $config['compress'];
  173. }
  174. return $dest;
  175. }
  176. }
  177. /**
  178. * crop
  179. *
  180. * @return mixed
  181. */
  182. public function handle_c($id, $source = false, $dest = false)
  183. {
  184. $config = Dever::load('upload/pic_crop-one', $id);
  185. if ($config) {
  186. if (!$source) {
  187. $source = $this->output['file'];
  188. }
  189. if (!$dest) {
  190. if (strpos($this->name, '_c') !== false) {
  191. $temp = explode('_c', $this->name);
  192. $this->name = $temp[0];
  193. }
  194. $this->name .= '_c' . $id;
  195. $dest = $this->file();
  196. }
  197. $size = $config['width'] . '_' . $config['height'] . '_2';
  198. $this->img()->crop($source, $size, false, true, $dest);
  199. }
  200. }
  201. }