Qiniu.php 6.3 KB

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